shell中的-z判断 在shell脚本中,-z用于判断紧随其后的字符串长度是否为零。如果字符串长度为零(即,字符串为空),则条件判断为真。 使用if语句结合-z判断命令输出 当你想根据某个命令的输出是否为空来执行不同的操作时,可以结合if语句和-z来实现。这里是一个基本的使用模式: bash if [ -z "$(command)" ...
exit 2 退出shell,用法不当 2、if (1) if [-z "$name"] 判断name是否为空字符串,如果空,则为真,执行if的内容 等同于 if ["$name" = ""] 等同于[! "$name"] (2) -z字符串是否为空,空为真 -n 指定字符串是否不空,不空为真 -a 某东西不存在,则为真。不限定为字符串 (3) -f 普通文件是...
1、判断字符串为空 if [ -z "$str" ]; then echo "empty string" fi 2、判断文件是否存在 if [ -f /home/builder/.profile ]; then echo "File exists;" fi 3、逻辑非 if [ ! -f /home/builder/.bash_profile ]; then echo "here!" else echo "test is ok" fi 逻辑非在语句前加“!”...
[root@hadoop01 ~]# data1="ni hao ma"[root@hadoop01 ~]# data2="yong wo ba "[root@hadoop01 ~]# echo ${data1:-${data2}}ni hao ma[root@hadoop01 ~]#[root@hadoop01 ~]# echo ${data1:+${data2}}yong wo ba[root@hadoop01 ~]# 2.if条件中的-a到-z含义 # 1.文件类判断[-a...
shell下if命令中-a~-z意思 喵~~~ shell中条件判断if中的-a到-z的意思 [ -a FILE ] 如果 FILE 存在则为真。 [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真。 [ -c FILE ] 如果 FILE 存在且是一个字特殊文件则为真。 [ -d ...
if[ -d"fold"];thenecho"fold exist"fiif[ ! -d"fold"];thenecho"fold no exist"fi 判断字符串相等 == 字符串相等 != 字符串不相等 if["$1"=="-h"];thenecho"show help"fi 判断字符串长度是否为0 -z 字符串长度为0 -n 字符串长度不为0 ...
if [ -z "$str1" ] || [ -z "$str2" ]; then echo "字符串不能为空" exit 1 elif [ "$str1" != "$str2" ]; then echo "两个字符串不相等" exit 2 else echo "字符串相等" fi 逻辑运算 最后,test命令还支持逻辑运算,这包括逻辑与、逻辑或和逻辑非。
shell条件测试 if [ -z] 在shell中的while,if进行条件测试的时候用<,>,=,!=这中数学符号好像行不通,在网上查到结果如下:大多数情况下,可以使用测试命令来对条件进行测试。比如可以比较字符串、判断文件是否存在及是否可读等等… 通常用” [ ] “来表示条件测试。注意这里的空格很重要。要确保方括号的空格。
ifcommand then commands fi 1. 2. 3. 4. 在要执行的命令结尾加个分号,就能在同一行使用then语句了,格式如下 ifcommand;then commands fi 1. 2. 3. 11.2 if-then-else语句 格式如下; ifcommand then commands else commands fi 1. 2. 3.
if command then commands else commands fi 当if语句中的命令返回退出状态码0时,then部分中的命令会被执行,这跟普通的if-then语句一样。当if语句中的命令返回非零退出状态码时,bash shell会执行else部分中的命令。 12.3 嵌套if 检查脚本代码中的多种条件 ...