Bash中if语句的使用

1、if-then结构

使用方法:

if command

then

command       #次区域可以是多个命令行

fi

2、if-then-else

if command

then

commands

else

commands

fi

else部分同then一样,可以使用多命令行,同时,else中还可以嵌套if-then-fi子模块

else的另一种写法,使用elif,elif-then可以多次使用来进行条件判断

if  command1

then

commands

elif  command2

then

more commands

[

elif  command

then

more commands

elif  command

then

more commands

]

fi

3、test判断

判断条件在if模块中处于中括号[]当中

数字类型判断

-eq ==

-ge >=

-gt >

-le <=

-lt <

-ne !=

 

字符串比较

=,!=,<,>,-n 长度大于0的判断,-z 长度为0的判断

其中大于小于号的使用需要使用\进行转义

 

4、文件比较

-d 文件是否存在并且是文件夹

-e 是否存在

-f 是否存在并是文件

-r 是否存在并可读

-s 是否存在并非空

-w 是否存在并可写

-x 是否存在并可执行

-O 是否存在并属于当前用户

-G 是否存在并默认组与当前用户相同

file1 -nt file2 文件1新于文件2

file1 -ot file2 文件1旧于文件2

也支持 ++,--,!,~(按位非),**(幂),<<,>>, &,|,&&,||运算

 5、case语句

case variable in

pattern1 | pattern2) commands1;;

pattern3) commands2;;

*) default commands;;

 

esac

5、命令行参数

  $#: 命令传入参数的个数

  ${!#}: 最后的参数值,没有参数时,显示脚本名,置于变量中显示0 

  last=$#     $last显示为0

  $*: 参数为单值

  $@: 参数为多值

  shift: 参数左移 ,shift 2 将第三个参数作为第一个

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值