bash 语法的特殊性

1,条件语句的判断

#!/bin/bash
echo "try bash if."

read input
string2="y"
if [ $input = $string2 ] 
then
echo "yes"
else
echo "no"
fi

read input
string2="y"  #=之间不能有空格
if [ $input = $string2 ]  #if与[]之间有空格 ,条件判断之间有空格
then  #这个then是一个必须品,而且必须单起一行

echo "yes"
else
echo "no"
fi


特别注意:

1),string2=”y“ =号两边不能有空格

2),if [ * ]   []中间的条件必须用两个空格隔开,if与[]也必须有空格隔开

3),if ["$inbput" = "string2"]  判断表达式两边必须有空格

4),then 这一行必须单起一行,不能与if同行,否则语法错误,凡是有if, elif字样的都要有换行的then

holy。。。

bash语法和tcl一样让人难受。


2,test命令(即条件判断语句,可以使用[] 代替)

1),数值比较(和perl有点类似)

n1 -eq n2 equal

n1 -ge n2 great or equal

n1 -gt n2 great than

n1 -le n2 less equal

n1 -lt n2 less than

n1 -ne n2 not equal

2),字符串比较(如果是变量,可以之间引用,也可以待双引号,如果是常量,可以直接引用,也可以待引号引用)

str1 = str2

str1 != str2

str1 < str2

str1 > str2

-n str1 长度是否非0  none zero

-z str1 长度是否为0 zero

3),文件比较(这块要注意是绝对路径还是相对路径)

-d file 检查file是否为一个目录

-e file 检查file是否存在

-f file 检测file存在并且是一个文件

-r file 检测file是否可读

-s file 检测file是否为空

-w file 检测file是否可写

-x file 检测file是否可执行

-0 file 检测file是否属于当前用户

-G file 检测file时候属于当前用户组

file1 -nt file2 检测file1 new than file2

file1 -ot file2 检测file1 old than file2

4),符合条件

if [ condition1 ] && [ condition2 ]  #与,两个条件都满足

if [ condition1 ] ||  [ condition2 ]  #或,有一个条件满足就行


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值