Linux Shell if/while/until/for 循环(当型/直到型)
if/then/elif/else/fi
#! /bin/sh
echo "Is it morning? Please answer yes or no."
read YES_OR_NO
if [ "$YES_OR_NO" = "yes" ]; then
echo "Good morning!"
elif [ "$YES_OR_NO" = "no" ]; then
echo "Good afternoon!"
else
echo "Sorry, $YES_OR_NO not recognized. Enter yes or no."
exit 1
fi
exit 0
1、while循环在工作中使用不多,一般是守护进程程序会使用 或 始终循环执行
语法:
while 条件
do
指令。。。。。。
done
#如:手机充值,发短信扣费,每次扣一毛,费用低于一毛就不能使用
#法1:
total=0
num=0
while((num<=100))
do
total=`expr $total + $num`
done
echo "the result is $total"
#法2:
total=0
num=0
while [ $num -le 100 ]
do
total=`expr $total + $num`
done
echo "the result is $total"
(1)登陆密码判断:
#!/bin/bash
echo "please enter your passwd:"
read PASSWD
COUNT=0
while [ "$COUNT" -le 3 ]; do
if [ "$PASSWD" == "hanmenghao" ]; then
echo "login suffessfully!!!"
exit 0
else
echo "passwd error, please enter your passwd again:"
read PASSWD
fi
done
(2)计算1+…100 = ?
#!/bin/bash
COUNT=1
SUM=0
while [ "$COUNT" -le 100 ]; do
let SUM=$SUM+$COUNT
let COUNT=$COUNT+1
done
echo "1+....+100 = $SUM"
2、until条件句:先执行后判断
语法:
until 条件
do
指令。。。。
done
# 使用不多,了解就好
total =0
num=0
until [$sum –gt 100]
do
total=’expor $total +$ num’
num=’expr $num + 1’
done
echo “The result is $total”
3、for循环
语法简介
for 变量 in 列表
do
操作
done
#变量要在循环内部用来指列表当中的对象。
列表是在for循环的内部要操作的对象,可以是字符串也可以是文件,如果是文件则为文件名。
(1)for循环注意事项
#! /bin/Bash
for i in a,b,c,e,I 2,4,6,8
do
echo $i
done
#注:in后面的列表,如果之间有空格间隔,就会按空格来换行
实例 12-13:删除垃圾箱中的所有文件。
分析:在本机中,垃圾箱的位置是在$HOME/.Trash中,因而是在删除$HOME/.Trash列表当中的所有文件,程序脚本如下。
[root@localhost bin]#gedit test12
#! /bin/Bash
for i in $HOME/.Trash/*
do
rm $i
echo “$ i has been deleted!”
done
#计算1~100的和
total=0
for i in {1..100}
do
total=`expr $total + $i`
done
echo "the result is : $total"
(2)计算任意个数的平均值
#!/bin/bash
sum=0
total=0
echo $@
for i in $@
do
let sum=$sum+$i;
let total=$total+1
done
echo "$(($sum/$total))"
(3)判断是文件还是目录
#!/bin/bash
echo "Please enter some files or dirs:"
for DIR in $@
do
if [ -d "$DIR" ]; then
ls -l $DIR
else
cat $DIR
fi
done
4、必须了解sleep
休息命令:1秒 === 1000 毫秒 === 》 1毫秒 === 1000 微秒
sleep 1
usleep 1000000 #同时表示休息1秒
# sleep :秒级别 usleep:微秒级别
案列:每隔2秒记录一次系统负载情况
#!/bin/bash
while true
do
uptime >> /tmp/uptime.log
sleep 2
done
写好后脚本在后台运行,让其成为守护进程 sh ./uptime.sh &
5、while计算1-100的值
法1:
#!/bin/bash
i=1
sum=0
while [ "$i" -le 100 ]
do
((sum=sum+i))
((i++))
done
echo $sum
注:大量运算使用(()),在脚本内建议使用此
法2:
#!/bin/bash
i=100
((sum=i*(i+1)/2))
echo $sum
6、shell脚本案列=====》手机充值
手机充值10元,每发一次短信(输出当前余额)花费1毛5,当余额低于1毛5就不能在发送短信,提示余额不足,请充值(可以允许用户充值继续发生短信),请使用while实现
解答:单位统一换算成分
#!/bin/bash
Total=1000
FEE=15
function Is_Num(){ # 判断用户充值钱数
expr $1 + 1 &>/dev/null
if [ $? -ne 0 -a "$1" != "-1" ];then
echo "Invalid input"
return 1
fi
return 0
}
while [ $Total -ge $FEE ]
do
read -p "Pls input your msg:" TXT
read -p "Are you sure to send?[Y/N]" OPTION
case $OPTION in
[yY]|[yY][eE]|[sS])
echo "Send successfully!"
((Total=Total-FEE))
echo "Your account has $Total"
;;
[nN]|[nN][oO])
echo "Canceled"
;;
*)
echo "Invalid input,pls enter right msg!"
;;
esac
if [ $Total -lt $FEE ];then
read -p "not ennough to send messages,Are U want to charge?[y|n]" OPT2
case $OPT2 in
y|Y)
while true
do
read -p "How much are you want to " Charge
Is_Num &Charge && break ||(
echo "Invalid input"
exit 100
)
done
((Total+=Charge))&&echo "you currently have $Total money"
;;
n|N)
exit 101
;;
*)
echo "Invalid input!!"
exit 102
;;
esac
fi
done
shell使用注意事项:
1)在Linux编辑中命令区分大小写字符。
2)在Shell语句中加入必要的注释,以便以后查询和维护,注释以#开头。
3)对Shell变量进行数字运算时,使用乘法符号“*”时,要用转义字符“\”进行转义。
4)由于Shell对命令中多余的空格不进行任何处理,因此程序员可以利用这一特性调整程序缩进,达到增强程序可读性效果。
5)在对函数命名时最好能使用有含义且能容易理解的名字,即使函数名能够比较准确地表达函数所完成的任务。同时建议对于较大的程序要建立函数名和变量命名对照表。