条件测试:
执行命令: 命令成功,条件测试成功;否则为失败;
根据$?, 状态返回值;
表达式:[ expression ] [[ expression ]] test expression
while 条件测试; do
循环体
done
while循环:条件测试成功,则循环;失败,则退出;
如何退出? 必须有时刻:条件测试不成功 ?: 条件控制变量
练习:求100以内所有正整数的和;
declare -i sum=0
declare -i i=1
while [ $i -le 100 ]; do
let sum+=$i
let i++
done
echo $sum
练习:求100以内所有偶数之和
declare -i evenSum=0
declare -i i=1
while [ $i -le 100 ]; do
if [ $[$i%2] -eq 0 ]; then
let evenSum+=$i
fi
let i++
done
echo $evenSum
declare -i evenSum=0
declare -i i=2
while [ $i -le 100 ]; do
let evenSum+=$i
let i+=2
done
echo $evenSum
如何让while循环退出:在循环体中改变测试条件中用于控制循环次数的变量的值;
练习:通过键盘提示用户输入字符,将用户输入的小写字母转换为大写,转换一次之后,再次提醒,再输再转换;但输入quit表示退出;
#!/bin/bash
read -p "Enter a word: " word
while [[ "$word" != "quit" ]]; do
echo $word | tr 'a-z' 'A-Z'
read -p "Enter a word again: " word
done
练习:显示如下菜单给用户
cpu) print cpu information;
mem) print memory information;
disk) print disk infomation;
quit) quit
Enter your option:
根据用户的选择输出相应信息;
每次执行后,不退出,而由用户再次指定新的选项;
#!/bin/bash
#
cat << EOF
cpu) print cpu information;
mem) print memory information;
disk) print disk infomation;
quit) quit
EOF
read -p "Enter your option: " option
option=`echo $option | tr 'A-Z' 'a-z'`
while [[ "$option" != "quit" ]]; do
if [[ "$option" == "cpu" ]]; then
cat /proc/cpuinfo
elif [[ "$option" == "mem" ]]; then
free -m
elif [[ "$option" == "disk" ]]; then
df -h
else
echo "Wrong option..."
fi
read -p "Enter your option again: " option
option=`echo $option | tr 'A-Z' 'a-z'`
done
循环体中:不断地修改控制循环次数的变量,以使得其在某一时刻导致测试条件不能满足从而退出循环;
练习:提示用户输入一个用户名,如果存在:显示用户UID和SHELL信息;否则,则显示无此用户;
显示完成之后,提示用户再次输入;如果是quit则退出;
#!/bin/bash
#
read -p "Enter a user name: " userName
while [[ "$userName" != "quit" ]]; do
if [ -z "$userName" ]; then
echo "User name null."
elif id $userName &> /dev/null; then
grep "^$userName\>" /etc/passwd | cut -d: -f3,7
else
echo "No such user."
fi
read -p "Enter a user name again(quit to exit): " userName
done
if [ -z "$userName" ]; then
echo "User name null."
else
if id $userName &> /dev/null; then
grep "^$userName\>" /etc/passwd | cut -d: -f3,7
else
echo "No such user."
fi
fi
read -p "Enter a user name again(quit to exit): " userName
练习:提示用户输入一个用户名,判断用户是否登录了当前系统;
如果没有登录,则停止5秒钟之后,再次判断;直到用户登录系统,显示“用户来了”,而后退出;
#!/bin/bash
#
read -p "Enter a user name: " userName
while ! id $userName &> /dev/null; do
read -p "Enter a user name again: " userName
done
who | grep "^$userName" &> /dev/null
retVal=$?
while [ $retVal -ne 0 ]; do
sleep 5
who | grep "^$userName" &> /dev/null
retVal=$?
done
echo "$userName is on."
#!/bin/bash
#
read -p "Enter a user name: " userName
while ! id $userName &> /dev/null; do
read -p "Enter a user name again: " userName
done
while ! who | grep "^$userName" &> /dev/null; do
echo "$userName is not here."
sleep 5
done
echo "$userName is on."
#!/bin/bash
#
read -p "Enter a user name: " userName
until [ -n "$userName" ] && id $userName &> /dev/null; do
read -p "Enter a user name again: " userName
done
until who | grep "^$userName" &> /dev/null; do
echo "$userName is not here."
sleep 5
done
echo "$userName is on."
bash编程之until循环:
until 测试条件; do
循环体
done
条件不满足,则循环;否则,退出;
练习:用until循环,求100以内所有正整数之和;
#!/bin/bash
declare -i sum=0
declare -i i=1
until [ $i -gt 100 ]; do
let sum+=$i
let i++
done
echo $sum
bash编程之组合测试条件深入探讨:
逻辑与:多个条件同时满足
[ CONDITION1 ] && [ CONDITION2 ]
[ CONDITION1 -a CONDITION2 ]
[[ CONDITION1 && CONDITION2 ]]
注意:前两个使用单或双中括号都可,但,&&不允许用于单中括号中,所以第三种只能用于双中括号中;
逻辑或:多个条件中有一个满足即为真;
[ CONDITION1 ] || [ CONDITION2 ]
[ CONDITION1 -o CONDITION2 ]
[[ CONDITION1 || CONDITION2 ]]
注意:||不允许用于单中括号中;
德 摩根 定律
!(条件1 或 条件2) = !条件1 并且 !条件2
!(条件1 并且 条件2) = !条件1 或 !条件2
使用while循环一次读取文件的一行,直到文件尾部:
while read line; do
循环体
done < /path/to/somefile
练习:取出当前系统上,默认shell为bash的用户
#!/bin/bash
#
while read line; do
[[ `echo $line | cut -d: -f7` == "/bin/bash" ]] && echo $line | cut -d: -f1
done < /etc/passwd
练习:显示所有其ID号为偶数的用户;
#!/bin/bash
#
while read line; do
userID=`echo $line | cut -d: -f3`
if [ $[$userID%2] -eq 0 ]; then
echo -n "$userID: "
echo $line | cut -d: -f1
fi
done < /etc/passwd
练习:显示/etc/rc.d/rc.sysinit文件中,其总字符个数大于30的行;
#!/bin/bash
#
while read line; do
charCounts=`echo $line | wc -c`
if [ $charCounts -gt 30 ]; then
echo -n "$charCounts: "
echo $line
fi
done < /etc/rc.d/rc.sysinit
练习:显示所有其UID和GID均为偶数的用户;
#!/bin/bash
#
while read line; do
userID=`echo $line | cut -d: -f3`
groupID=`echo $line | cut -d: -f4`
if [ $[$userID%2] -eq 0 ] && [ $[$groupID%2] -eq 0 ]; then
echo -n "$userID, $groupID: "
echo $line | cut -d: -f1
fi
done < /etc/passwd
练习:显示/etc/rc.d/rc.sysinit文件中,其总字符个数大于30且非以“#”开头的行;
#!/bin/bash
#
while read line; do
charCounts=`echo $line | wc -c`
if [ $charCounts -gt 30 ] && [[ "$line" =~ ^[^#] ]] ; then
echo -n "$charCounts: "
echo $line
fi
done < /etc/rc.d/rc.sysinit
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com