linux-tutorial/codes/shell/demos/statement/if-demo.sh

36 lines
669 B
Bash
Raw Normal View History

2019-03-01 10:36:45 +08:00
#!/usr/bin/env bash
################### if 语句 ###################
# 写成一行
2019-10-10 08:56:31 +08:00
if [[ 1 -eq 1 ]]; then
echo "1 -eq 1 result is: true";
fi
2019-03-01 10:36:45 +08:00
# Output: 1 -eq 1 result is: true
# 写成多行
if [[ "abc" -eq "abc" ]]
then
2019-10-10 08:56:31 +08:00
echo ""abc" -eq "abc" result is: true"
2019-03-01 10:36:45 +08:00
fi
# Output: abc -eq abc result is: true
################### if else 语句 ###################
if [[ 2 -ne 1 ]]; then
2019-10-10 08:56:31 +08:00
echo "true"
2019-03-01 10:36:45 +08:00
else
2019-10-10 08:56:31 +08:00
echo "false"
2019-03-01 10:36:45 +08:00
fi
# Output: true
################### if elif else 语句 ###################
x=10
y=20
if [[ ${x} > ${y} ]]; then
2019-10-10 08:56:31 +08:00
echo "${x} > ${y}"
2019-03-01 10:36:45 +08:00
elif [[ ${x} < ${y} ]]; then
2019-10-10 08:56:31 +08:00
echo "${x} < ${y}"
2019-03-01 10:36:45 +08:00
else
2019-10-10 08:56:31 +08:00
echo "${x} = ${y}"
2019-03-01 10:36:45 +08:00
fi
# Output: 10 < 20