linux-tutorial/codes/shell/逻辑控制/if-elif-else示例.sh

36 lines
648 B
Bash
Raw Normal View History

2019-10-15 14:17:17 +08:00
#!/usr/bin/env bash
################### if 语句 ###################
# 写成一行
if [[ 1 -eq 1 ]]; then
2019-10-29 18:22:19 +08:00
echo "1 -eq 1 result is: true";
2019-10-15 14:17:17 +08:00
fi
# Output: 1 -eq 1 result is: true
# 写成多行
if [[ "abc" -eq "abc" ]]
then
2019-10-29 18:22:19 +08:00
echo ""abc" -eq "abc" result is: true"
2019-10-15 14:17:17 +08:00
fi
# Output: abc -eq abc result is: true
################### if else 语句 ###################
if [[ 2 -ne 1 ]]; then
2019-10-29 18:22:19 +08:00
echo "true"
2019-10-15 14:17:17 +08:00
else
2019-10-29 18:22:19 +08:00
echo "false"
2019-10-15 14:17:17 +08:00
fi
# Output: true
################### if elif else 语句 ###################
x=10
y=20
if [[ ${x} > ${y} ]]; then
2019-10-29 18:22:19 +08:00
echo "${x} > ${y}"
2019-10-15 14:17:17 +08:00
elif [[ ${x} < ${y} ]]; then
2019-10-29 18:22:19 +08:00
echo "${x} < ${y}"
2019-10-15 14:17:17 +08:00
else
2019-10-29 18:22:19 +08:00
echo "${x} = ${y}"
2019-10-15 14:17:17 +08:00
fi
# Output: 10 < 20