[shell基础]——if/for/while/until/case 语句

时间:2021-10-01 09:32:04

for语句

for loop in
do
echo $loop
done for loop in `seq `
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 ]
do
echo "warning:your / user 80%" #向用户提出警告,你的/使用率已经大于或等于80%
exit
done #而是执行循环外的这条,
#告诉用户,你的/使用率是多少,还少于80%
echo "now your / use $use%,less then 80%"

case语句

function directory()
{
[ -d $ ]
if [ $? -eq ]
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
)
directory /opt/aa
;;
)
directory /opt/cc
;;
)
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后面的语句,执行下一次循环,直到条件为真