📙
📙
📙
📙
Shell
搜索文档…
流程控制

IF-ELSE

if 语句语法格式:
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
也可以写为
if condition1;then
command1
elif condition2;then
command2
else
commandN
fi

示例

a=10
b=20
if [ $a == $b ];then
echo "a 等于 b"
elif [ $a -gt $b ];then
echo "a 大于 b"
elif [ $a -lt $b ];then
echo "a 小于 b"
else
echo "没有符合的条件"
fi

for 循环

for循环一般格式为:
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
也可以写为
for var in item1 item2 ... itemN;do
command1
command2
...
commandN
done
写成一行:
for var in item1 item2 ... itemN; do command1; command2… done;

示例

for loop in 1 2 3 4 5
do
echo "The value is: $loop"
done
# 输出
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
for str in This is a string
do
echo $str
done
# 输出
This
is
a
string