[shell基础]——if/for/while/until/case 语句
for语句
for loop in 1 2 3 4 5 do echo $loop done for loop in `seq 1 100` do echo $loop done for loop in `ls /tmp` do echo $loop done
while语句
while true do read -p "请输入你的密码:" passwd if [ $passwd = "aixocm" ] then echo "密码输入正确,welcome" break else echo "密码输入错误" continue fi done
until语句
use=`df -lh | sed -n '/\/$/{p}' | awk '{print $5}' |sed 's/%//g'` #如果/使用率满足小于80%的条件,则不执行循环, #反之若/大于80%(即不满足条件),则执行循环 until [ $use -lt 80 ] do echo "warning:your / user 80%" #向用户提出警告,你的/使用率已经大于或等于80% exit 1 done #而是执行循环外的这条, #告诉用户,你的/使用率是多少,还少于80% echo "now your / use $use%,less then 80%"
case语句
function directory() { [ -d $1 ] if [ $? -eq 0 ] then echo "$1存在" else echo "$1不存在" fi } tput bold --->#加粗 echo "===查询菜单===" tput sgr0 echo "1、查询/opt/aa 目录是否存在?" echo "2、查询/opt/cc 目录是否存在?" echo "3、查询/opt/dd 目录是否存在?" read -p "你想查询啥?:" n case $n in 1) directory /opt/aa ;; 2) directory /opt/cc ;; 3) directory /opt/dd ;; *) echo error ;; esac
if语句
#if/else结构
if expression then command else command fi
#if/elif/else结构
if expression1
then command
elif expression2
then command
elif expression3
then command
else command
fi
循环控制符:break和continue
break:忽略循环体中任何语句和条件的限制,强制退出当前循环
continue:跳过continue后面的语句,执行下一次循环,直到条件为真