if条件语句
if condition
then
# do sth.
elif condition
then
# do another.
else
# do others.
fi
# 有些人喜欢这样写,看起来更紧凑一些
if condition; then
# do sth.
elif condition; then
# do another.
else
# do others.
fi
样例:
a=10;b=20;
# 下面这一句,方括号是有用的,并且方括号内,前后两个空格不能省略
if [ $a -eq $b ];then
echo "$a -eq $b: a 等于 b"
else
echo "$a -eq $b: a 不等于 b"
fi
switch语句
value=3;
case $value in
1)
# do sth.
;;
2)
# do sth.
;;
*)
# do others.
;;
esac
样例
a = 4
case $a in
1)
echo ${a}等于1
;;
2)
echo ${a}等于2
;;
3)
echo ${a}等于3
;;
*)
echo 其他
;;
esac
while语句
while condition
do
# do sth.
done
样例:
# 打印 1 之后退出循环
while true; do echo 1; break; done;
# 递增
cnt=0; while (($cnt < 10)); do echo $cnt; cnt=$((cnt + 1)); done;
for语句
# for in,这里需要注意,in与花括号之间的空格不能省略
for i in {a..z}
do
echo $i
done
# for i
for ((i=1;i<=10;i++))
do
# do sth.
done
样例:
# 常规用法
for((i=1; i<=10; i++)); do echo i; done;
# 这个脚本挺常用的,循环调用 n 次脚本
for a in {1..10}; do echo a; done;
# 遍历所有文件
for f in ./*; echo $f; done;
until语句
until 的功能:除非满足条件,否则就……
与 while 写法一致,但是判断条件是相反的,while 的功能是:如果满足条件,就……
until condition
do
# do sth.
done
样例:
# until 和 while 相反,条件为真的时候退出。
cnt = 0; until (($cnt > 10));do [cmd];cnt = $((cnt + 1));done;
标签:语句,do,cnt,shell,流程,sth,echo,while,condition
From: https://www.cnblogs.com/chenss15060100790/p/18258144