1. 变量最好加上双引号
# undefind augments,error prompt:unary operator expected"
if [ $string = "some_string" ];then
echo "ok"
else :
echo "no"
fi
2. 字符串中含有空格要加双引号
# unreferenced string, error prompt: "too many arguments"
if [ "$string" = some string ];then
echo "ok"
else :
echo "no"
fi
3. 相关提示
# unreferenced string&undefind augments, error prompt: "binary operator expected"
if [ $string = some ];then
echo "ok"
else :
echo "no"
fi
4.正确语句
# normal statement
string="some"
if [ "$string" = "some" ];then
echo "ok"
else :
echo "no"
fi
5. test 数值比较
# /usr/bin/test same with "[]"
# Comparing strings: -eq -ne -lt -gt -le -ge (= != < > <= >=)
debug=1
if test $debug -le 1;then
echo "ok"
else :
echo "no"
fi
6. test 简单方式
# simple way of test
debug=1
test $debug -eq 1 && echo "ok"||echo "no"
# Combination of command
debug=1
test $debug -eq 1 && {
echo $debug
echo "ok"
}
7. 复杂的test语句
# Complex expressions, too complex logic to recommend frequently used
txt1="begin"
txt2="end"
[ "$txt1" ] && [ "$txt1" != "$txt2" ] && bound_text="$txt1 $txt2"||bound_text="$txt1"
echo "$bound_text"
8. if的复杂语句
# Complex expressions, too complex logic to recommend frequently used
txt1="begin"
txt2="end"
if [ "$txt1" -a "$txt1" != "$txt2" ];then
bound_text="$txt1 $txt2"
else
bound_text="$txt1"
fi
echo "$bound_text"
9. 内嵌shell语句
# Consider performance
if [ "`grep ftp /etc/hosts`" ];then
echo "you have available ftp "
else
echo "you have unavailable ftp"
fi
10. 内嵌shell语句
# Check status of network
if ping -c 3 www.baidu.com>/dev/null 2>&1;then
echo "network is ok"
else
echo "network has problem"
fi
11. /usr/bin/test -l 可测试字符串长度
num_file="I love linux shell"
if /usr/bin/test -l "$num_file" -gt 10 ;then
echo "It's a longer than 10 chars string"
else
echo "null"
fi
学习中..., 欢迎拍砖。