shell if 否定判断
时间: 2024-03-21 15:36:54 浏览: 13
在Shell脚本中,可以使用否定判断来判断条件是否为假。否定判断使用`!`符号来表示。当条件为假时,否定判断为真;当条件为真时,否定判断为假。
以下是一个示例,演示了如何在Shell脚本中使用否定判断:
```shell
#!/bin/bash
num=10
if ! [ $num -eq 20 ]; then
echo "条件为假"
fi
```
在上面的示例中,`! [ $num -eq 20 ]`表示如果`$num`不等于20,则条件为真,输出"条件为假"。
相关问题
shell if否定
在Shell脚本中,使用if语句的否定形式可以通过在条件表达式前面加上感叹号"!"来实现。以下是一个示例:
```
if [ ! -n "$doiido" ]; then
echo "$doiido is empty"
exit 0
fi
```
在这个例子中,如果变量`$doiido`没有值,那么条件表达式`! -n "$doiido"`就会被满足,执行相应的代码块。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [shell中的if语句](https://blog.csdn.net/weixin_33850015/article/details/94109776)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
shell if多重判断
以下是shell中多重判断的示例:
1. 单层条件判断式:
```shell
if [ 条件判断式 ]; then
command1
fi
```
例如:
```shell
if [ "${Yn}" == "Y" ]; then
echo "yes"
exit 0
fi
```
2. 三层条件判断式:
```shell
if [ 条件判断式1 ]; then
command1
elif [ 条件判断式2 ]; then
command2
else
command3
fi
```
例如:
```shell
if [ 条件1 ]; then
command1
elif [ 条件2 ]; then
command2
else
command3
fi
```