shell中的if语法

字符串测试

= 两个字符串相等

!= 两个字符串不相等

-Z  空串

-n  非空串

示例:[ -Z $SHELL ]


数值测试

-eq 数值相等(equal to)

-ne 数值不相等

-gt 第一个数大于第二个数

-lt 第一个数小于第二个数

-le 第一个数小于等于第二个数

-ge 第一个数大于等于第二个数

示例:[ "10" -eq "12" ]


其他语言不同,Shell Script中if语句的条件部分要以分号来分隔。第三行中的[]表示条件测试,常用的条件测试有下面几种:

[ -f "$file" ] 判断$file是否是一个文件

[ $a -lt 3 ] 判断$a的值是否小于3,同样-gt和-le分别表示大于或小于等于

[ -x "$file" ] 判断$file是否存在且有可执行权限,同样-r测试文件可读性

[ -n "$a" ] 判断变量$a是否有值,测试空串用-z

[ "$a" = "$b" ] 判断$a和$b的取值是否相等

[ cond1 -a cond2 ] 判断cond1和cond2是否同时成立,-o表示cond1和cond2有一成立

要注意条件测试部分中的空格。在方括号的两侧都有空格,在-f、-lt、=等符号两侧同样也有空格。如果没有这些空格,Shell解释脚本的时候就会出错。

$#表示包括$0在内的命令行参数的个数。在Shell中,脚本名称本身是$0,剩下的依次是$0、$1、$2…、${10}、${11},等等。$*表示整个参数列表,不包括$0,也就是说不包括文件名的参数列表。


linux if语句内判断参数

–b 当file存在并且是块文件时返回真  
-c 当file存在并且是字符文件时返回真  
-d 当pathname存在并且是一个目录时返回真 
-e 当pathname指定的文件或目录存在时返回真  
-f 当file存在并且是正规文件时返回真  
-g 当由pathname指定的文件或目录存在并且设置了SGID位时返回为真  
-h 当file存在并且是符号链接文件时返回真,该选项在一些老系统上无效  
-k 当由pathname指定的文件或目录存在并且设置了“粘滞”位时返回真  
-p 当file存在并且是命令管道时返回为真  
-r 当由pathname指定的文件或目录存在并且可读时返回为真  
-s 当file存在文件大小大于0时返回真  
-u 当由pathname指定的文件或目录存在并且设置了SUID位时返回真  
-w 当由pathname指定的文件或目录存在并且可执行时返回真。一个目录为了它的内容被访问必然是可执行的。  
-o 当由pathname指定的文件或目录存在并且被子当前进程的有效用户ID所指定的用户拥有时返回真

Linux Shell 脚本中,`if` 语句用于条件判断,根据不同的条件执行不同的代码块。以下是一些常见的 `if` 语句语法示例: ### 基础语法结构 ```bash if [ condition ]; then # Commands to execute if the condition is true fi ``` 这是最基础的 `if` 语句结构,其中 `condition` 是一个测试表达式,如果表达式的结果为真,则执行 `then` 后面的命令块。 ### `if...then...else` 结构 ```bash if [ condition ]; then # Commands to execute if the condition is true else # Commands to execute if the condition is false fi ``` 此结构允许指定当条件为假时要执行的替代命令块。 ### `if...elif...else` 结构 ```bash if [ condition1 ]; then # Commands to execute if condition1 is true elif [ condition2 ]; then # Commands to execute if condition2 is true else # Commands to execute if all conditions are false fi ``` 这种结构可以检查多个条件,并且在某个条件满足时停止检查后续条件。 ### 使用 `[[ ... ]]` 进行更复杂的条件判断 ```bash if [[ $var == "value" && $num -gt 10 ]]; then # Commands to execute if both conditions are true fi ``` 相比 `[ ... ]`,`[[ ... ]]` 提供了更多的功能,比如支持正则表达式和逻辑运算符。 ### 检查文件是否存在 ```bash if [ -f "/path/to/file" ]; then echo "File exists." else echo "File does not exist." fi ``` 这里使用了 `-f` 测试选项来检查指定路径是否为一个存在的文件。 ### 比较字符串 ```bash if [ "$str1" = "$str2" ]; then echo "Strings are equal." else echo "Strings are not equal." fi ``` 此示例展示了如何比较两个字符串是否相等。 ### 比较数字 ```bash if [ $num1 -lt $num2 ]; then echo "$num1 is less than $num2" else echo "$num1 is greater than or equal to $num2" fi ``` 这里使用 `-lt`(小于)、`-le`(小于等于)、`-gt`(大于)、`-ge`(大于等于)、`-eq`(等于)和 `-ne`(不等于)来进行数字比较。 ### 使用 `case` 进行多重选择 ```bash case $var in pattern1) # Commands to execute if pattern1 matches ;; pattern2) # Commands to execute if pattern2 matches ;; *) # Default case ;; esac ``` `case` 语句提供了一种简洁的方式来处理多个可能的值,其中 `*` 表示默认情况。 ### 示例:检查用户输入 ```bash read -p "Enter a number: " num if [ $num -gt 0 ]; then echo "You entered a positive number." elif [ $num -lt 0 ]; then echo "You entered a negative number." else echo "You entered zero." fi ``` 这个例子演示了如何读取用户的输入并对其进行条件判断。 ### 示例:检查文件权限 ```bash if [ -r "filename" ]; then echo "File is readable." fi if [ -w "filename" ]; then echo "File is writable." fi if [ -x "filename" ]; then echo "File is executable." fi ``` 这些测试选项分别检查文件是否可读、可写或可执行。 ### 示例:检查变量是否设置 ```bash if [ -z "${var+x}" ]; then echo "Variable is not set." else echo "Variable is set to: $var" fi ``` 此示例利用了 `${parameter+x}` 的特性来检查变量是否已经被设置。 ### 示例:使用 `test` 命令 ```bash if test $num -gt 0; then echo "Number is positive." fi ``` `test` 命令与 `[ ... ]` 是等价的,都可以用来进行条件测试。 以上示例涵盖了 Linux Shell 脚本中 `if` 语句的基本用法和一些常见场景。通过这些示例,可以根据具体需求编写更加复杂的条件判断逻辑。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值