shell入门学习笔记-07-运算符

版权声明:本文为博主hanchao5272原创文章,转载请注明来源,并留下原文链接地址,谢谢! https://blog.csdn.net/hanchao5272/article/details/86430498

系列目录与参考文献传送门: shell入门学习笔记-序章

运算符

算数运算符

  • 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用。
admindeMacBook-Pro:myshell admin$ val=$(expr 1 + 2)
admindeMacBook-Pro:myshell admin$ echo $val
3
admindeMacBook-Pro:myshell admin$ echo $(expr 1 + 2)
3
admindeMacBook-Pro:myshell admin$ echo $(expr 1 - 2)
-1
admindeMacBook-Pro:myshell admin$ echo $(expr 2 \* 2)
4
admindeMacBook-Pro:myshell admin$ echo $(expr 6 / 3)
2

admindeMacBook-Pro:myshell admin$ if [ 2 == 2 ]; then  echo true; fi
true
admindeMacBook-Pro:myshell admin$ if [2==2 ]; then  echo true; fi
-bash: [2==2: command not found
admindeMacBook-Pro:myshell admin$ if [ 2==2]; then  echo true; fi
-bash: [: missing `]'

admindeMacBook-Pro:myshell admin$ if [ 2==1 ];then echo true;fi
true
admindeMacBook-Pro:myshell admin$ if [ 2 == 1 ];then echo true;fi
admindeMacBook-Pro:myshell admin$
  • 支持的算数运算符:+ - * / = == !=
  • 注意在使用乘号*时,需要进行转义.
  • [ a == b ]表达式与中括号之间一定要有空格。
  • [ 2==1 ]错误的写法,等号两边一定要有空格,否则将2==1当成了一个字符串,其肯定不为空,此条件永远为true。

关系运算符

admindeMacBook-Pro:myshell admin$ if [ 2 -gt 1 ]; then echo "2 > 1";fi
2 > 1
admindeMacBook-Pro:myshell admin$ if [ 2 -ne 1 ]; then echo "2 != 1";fi
2 != 1
admindeMacBook-Pro:myshell admin$ if [ 1 -le 1 ];then echo "1 <= 1";fi
1 <= 1
  • 支持的关系运算符:-eq -ne -lt -gt -le -ge

布尔运算符

admindeMacBook-Pro:myshell admin$ if [ 1 == 2 -o 2 == 2]; then echo true; fi
-bash: [: missing `]'
admindeMacBook-Pro:myshell admin$ if [ 1 == 2 -o 2 == 2 ]; then echo true; fi
true
admindeMacBook-Pro:myshell admin$ if [ 1 == 1 -a 2 == 2 ]; then echo true; fi
true
  • -oOR
  • -aAND

逻辑运算符

admindeMacBook-Pro:myshell admin$ if [[ 1 == 2 || 2 == 2 ]]; then echo true; fi
true
admindeMacBook-Pro:myshell admin$ if [[ 1 == 1 && 2 == 2 ]]; then echo true; fi
true
  • &&AND
  • ||OR
  • 逻辑运算符需要使用双中括号[[ expr ]]

字符串运算符

admindeMacBook-Pro:myshell admin$ if[ "abc" == "abc" ]; then echo true; fi
-bash: syntax error near unexpected token `then'
admindeMacBook-Pro:myshell admin$ if [ "abc" == "abc" ]; then echo true; fi
true
admindeMacBook-Pro:myshell admin$ if [ "abc" != "abcd" ]; then echo true; fi
true
admindeMacBook-Pro:myshell admin$ if [ -z "" ]; then echo true; fi
true
admindeMacBook-Pro:myshell admin$ if [ -n "aaa" ]; then echo true; fi
true
admindeMacBook-Pro:myshell admin$ if [ "abc" ]; then echo true; fi
true
  • 字符串运算符:== != -z -n
  • -zlength = zero,字符串长度为空。
  • -nlength not zero,字符串非空。
  • 通过if [ string ]本身也可以判断字符串非空。

文件运算符

操作符 说明 举例
-d file 检测文件是否是目录,如果是,则返回 true。 [ -d $file ] 返回 false。
-f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 [ -f $file ] 返回 true。
-r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。
-w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。
-x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。
-s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。
-e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true。
-b file 检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
-c file 检测文件是否是字符设备文件,如果是,则返回 true。 [ -c $file ] 返回 false。
-g file 检测文件是否设置了 SGID 位,如果是,则返回 true。 [ -g $file ] 返回 false。
-k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 [ -k $file ] 返回 false。
-p file 检测文件是否是有名管道,如果是,则返回 true。 [ -p $file ] 返回 false。
-u file 检测文件是否设置了 SUID 位,如果是,则返回 true。 [ -u $file ] 返回 false。
admindeMacBook-Pro:myshell admin$ if [ -d a.sh ]; then echo "a.sh is dir" ;else echo "a.sh is not dir"; fi
a.sh is not dir
admindeMacBook-Pro:myshell admin$ if [ -f a.sh ];then echo "a.sh is file";else echo "a.sh is not file";fi
a.sh is file
admindeMacBook-Pro:myshell admin$ ls -l
total 32
-rwxr-xr-x  1 admin  staff   56  1  7 17:49 a.sh
-rwxr-xr-x  1 admin  staff  110  1  7 12:02 b.sh
-rw-r--r--  1 admin  staff  127  1  4 17:01 hello.sh
-rw-r--r--  1 admin  staff   33  1  7 10:26 log.txt
admindeMacBook-Pro:myshell admin$ if [ -w a.sh ]; then echo "a.sh writeable"; else echo "a.sh not writeable"; fi
a.sh writeable
admindeMacBook-Pro:myshell admin$ if [ -r a.sh ]; then echo "a.sh readable"; else echo "a.sh not readable"; fi
a.sh readable
admindeMacBook-Pro:myshell admin$ if [ -x a.sh ]; then echo "a.sh executable"; else echo "a.sh not executable"; fi
a.sh executable

admindeMacBook-Pro:myshell admin$ if [ -e a.sh ]; then echo "a.sh exist"; else echo "a.sh not exist"; fi
a.sh exist
admindeMacBook-Pro:myshell admin$ if [ -s a.sh ]; then echo "a.sh not empty"; else echo "a.sh empty"; fi
a.sh not empty
  • -dis directory-fis file
  • -w -r -x与权限对应
  • -eexist文件存在,-ssubstance内容存在。

猜你喜欢

转载自blog.csdn.net/hanchao5272/article/details/86430498
今日推荐