回顾
脚本编程控制结构:
顺序
选择
if
case
循环
for
while
until
#条件满足进入循环;条件不满足退出循环
while CONDITION; do
statement
...
done
until循环
#和while相反;条件不满足进入循环;条件满足退出循环
until CONDITION; do
statement
...
done
练习和while对比
#!/bin/bash
#showdisk.sh
#while版本
cat <<EOF
D|d)show disk usages;
M|m)show memory usages;
S|s)show Swap usages;
*)quit
EOF
read -p "Your choice:" CHOICE
while [ $CHOICE != 'quit' ]; do
case $CHOICE in
D | d)
echo "Disk usage:"
df -Ph ;;
M | m)
echo "Memory usages:"
free -m | grep -B 1 Mem ;;
S | s)
echo "Swap usage:"
free -m | grep -B 1 Swap;;
*)
echo "Unknown."
esac
read -p "Again,your choice:" CHOICE
done