Chinaunix首页 | 论坛 | 博客
  • 博客访问: 657367
  • 博文数量: 96
  • 博客积分: 2005
  • 博客等级: 上尉
  • 技术积分: 1061
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-21 13:59
文章分类

全部博文(96)

文章存档

2013年(11)

2012年(30)

2011年(55)

分类: LINUX

2011-08-06 09:32:32

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


学习中..., 欢迎拍砖。
阅读(752) | 评论(0) | 转发(0) |
0

上一篇:shell中的条件表达

下一篇:shell文本处理

给主人留下些什么吧!~~