首页 > 代码库 > while循环
while循环
适用于循环次数未知的情况,要有退出条件。
while CONDITION; do
statement
...
done
例如:
1.写一个脚本,输入任何字符,小写转换为大写。输入quit退出。
#!/bin/bash
read -p "Input Something" STRING
while [ $STRING != ‘quit‘ ]; do
echo $STRING|tr ‘a-z‘ ‘A-Z ‘
read -p "Input Something" STRING
done
2.写一个脚本,每个5秒检测用户是否登陆,如果登陆则显示已经登陆。
#!/bin/bash
who|grep "hadoop" &> /dev/null
LOG=$?
while [ $LOG -ne 0 ];do
echo `date ,hadoop is not log`
sleep 5
who|grep "hadoop" &> /dev/null
LOG=$?
done
echo "hadoop is logged in"
3.写一个脚本:
显示一个菜单给用户,当用户给定相应选项后显示形影的内容。
#!/bin/bash
cat << EOF
d|D) show disk usages.
m|M) show memory usages.
s|S) show swap usages.
*) quit.
EOF
read -p "your choice:" CHO
while [ $CHO != ‘quit‘ ];do
case $CHO in
d|D) show disk usages.
echo "disk usages:"
df -h
;;
m|M) show memory usages.
echo "memory usage:"
free- m|grep "Mem"
;;
s|S) show swap usages.
echo "swap usagee:"
free -m| grep "swap"
;;
*) quit.
echo "Unkown"
;;
esac
read -p "your choice again:" CHO
done
本文出自 “小私的blog” 博客,请务必保留此出处http://ggvylf.blog.51cto.com/784661/1606398
while循环