天天看点

shell if 多个条件_shell基础之运算符

头条号:浩渺烟波

接着上一篇shell变量继续往下说:

Shell中是不支持简单数学运算的,比如这样:

[[email protected]_0_11_centos ~]# rest=10+10[[email protected]_0_11_centos ~]# echo $rest10+10
           

但是我们可以加上命令使其支持,在shell中有三种弄方法可以支持简单的数学运算:

  1. 使用$(())

    这个$后面是两个小括号,一个小括号不对:

[[email protected]_0_11_centos ~]# rest=$((10+20))[[email protected]_0_11_centos ~]# echo $rest30[[email protected]_0_11_centos ~]# rest=$(10+20) -bash: 10+20: command not found
           
  1. 使用$[]

    使用$[]这个感觉是最简单的

[[email protected]_0_11_centos ~]# rest=$[10 + 10][[email protected]_0_11_centos ~]# echo $rest20
           

3.使用关键字expr使用这个expr有点复杂,在使用的时候需要用反单引号将其包裹起来,而且必须有空格:

[[email protected]_0_11_centos ~]# rest=`expr 10 + 20`[[email protected]_0_11_centos ~]# echo $rest30
           

如果没有空格的话:

[[email protected]_0_11_centos ~]# rest=`expr 10+20`[[email protected]_0_11_centos ~]# $rest-bash: 10+20: command not found
           

而且还不支持括号,并且乘号还要用反斜杠给转译一下:

[[email protected]_0_11_centos ~]# rest=`expr 10 + 20 * (10 + 10)` -bash: command substitution: line 1: syntax error near unexpected token `('-bash: command substitution: line 1: `expr 10 + 20 * (10 + 10)'[[email protected]_0_11_centos ~]# 
           

所以遇到复杂的运算,需要一步一步的算,很麻烦!

还有一些其他的简单运算 比如乘除,取余等都类似

关系运算:一般用条件语句上,需要用到[]和(())比如 大于 在[]中要用-gt表示 ,在(())用==表示,gt是 greater than的缩写例如 在文件中写入:

if (($1 > $2))then        echo "$1 大于 $2"else        echo "$1 小于 $2"fiif [ $1 -gt $2 ]then        echo "$1 大于 $2"else        echo "$1 小于 $2"fi
           

执行:

[[email protected]_0_11_centos shell]# ./myShell.sh 100 200100 小于 200100 小于 200
           

shell的if语句也挺奇怪,if 之后是换行,然后then 最后需要加上fi,表示结束。 如果多个判断的话用elif,不是else if,跟java还是有区别的需要注意的一点的是[]中括号使用的时候,一定要主要前后的空格,否则会报错的。

其他的关系运算类似:-eq 表示相等,在(())用 ==表示,是equal的缩写-ne表示不相等,在(())用 !=表示,是 not equal的缩写-gt 表示大于,在(())用 >表示,是greater than的缩写-ge 表示大于等于,在(())用 >=表示,是greater equall的缩写-lt 表示小于,在(())用

逻辑运算符 逻辑运算符,一般是指与或非在shell []中 -a表示与,就是两个表达式都成立,条件才成立,在(())表示为&&-o或运算,表示两个条件只要有一个为真即可,在(()))表示为||!表示非 在(()))表示为!

字符串运算符= 检测两个字符串是否相等,相等则返回true != 检测两个字符串是否相等,不相等则返回true-z 检测字符串长度是否为0,为0则返回true-n 检测字符串长度是否为0,不为0则返回true str 检测字符串是否为null,不为null则返回true

比如:

str="abcdee"if [ $str = "abc" ]then        echo "相等"else        echo "不相等"fiif [ -n $str ]then        echo "不为0"else        echo "为0"fi
           

执行结果:

[[email protected]_0_11_centos shell]# ./myShell.sh  不相等不为0
           

文件检测运算符:

-b 检测文件是否是块设备文件,如果是,则返回true -c 检测文件是否是字符设备文件,如果是,则返回true -d 检测文件是否是目录文件,如果是,则返回true -f 检测文件是否是普通文件(既不是目录也不是设备文件),如果是,则返回true -g 检测文件是否设置了SGID位,如果是,则返回true -k 检测文件是否设置了粘着位(stucky Bit),如果是,则返回true -p 检测文件是否具名管道,如果是,则返回true -u 检测文件是否设置了SUID位,如果是,则返回true -r 检测文件是否可读,如果是,则返回true -w 检测文件是否可写,如果是,则返回true-x 检测文件是否可执行,如果是,则返回true -s 检测文件是否为不为空(文件大小是否不为0),如果不为0,则返回true -e 检测文件(包括目录)是否存在,如果存在,则返回true -a 检测文件(包括目录)是否存在,如果存在,则返回true

比如检测文件是否存在用-e 比如创建一个文件test.txt

file=/root/training/shell/test.txtif [ -e $file ]then        echo "文件存在"else        echo "文件不存在"fi
           

执行的结果:

[[email protected]_0_11_centos shell]# ./myShell.sh 文件存在
           

未完待续!

参考文章:

https://blog.csdn.net/yuki5233/article/details/81166509

https://www.runoob.com/linux/linux-shell-basic-operators.html

shell if 多个条件_shell基础之运算符