一、基本语法
1、
“$((运算式))”
或“$[运算式]”
2、expr + , - , *, /, %:加,减,乘,除,取余;注意:expr运算符间要有空格
3、$(($a + $b))
:变量中的$
符号可以去掉$((a + b))
4、比较运算:echo $((a<b))
5、交互方式:bc
🌙加法:
zhilong@zhilong-virtual-machine:~$ expr 2 + 3
2+3
或者:
zhilong@zhilong-virtual-machine:~$ a=10
zhilong@zhilong-virtual-machine:~$ b=8
zhilong@zhilong-virtual-machine:~$ c=6
zhilong@zhilong-virtual-machine:~$ echo $(($a + $b))
18
🌙减法:
zhilong@zhilong-virtual-machine:~$ expr 2 - 3
-1
或者:
zhilong@zhilong-virtual-machine:~$ a=10
zhilong@zhilong-virtual-machine:~$ b=8
zhilong@zhilong-virtual-machine:~$ echo $(($a - $b))
2
🌙乘法:
乘法中
*
前面一定加\
,不加语法错误
zhilong@zhilong-virtual-machine:~$ expr 2 * 3
expr: 语法错误:未预期的参数 “公共的”
zhilong@zhilong-virtual-machine:~$ expr 2 \* 3
6
或者:
zhilong@zhilong-virtual-machine:~$ a=10
zhilong@zhilong-virtual-machine:~$ b=8
zhilong@zhilong-virtual-machine:~$ echo $(($a * $b))
80
🌙除法:
除法默认取整
zhilong@zhilong-virtual-machine:~$ expr 6 / 2
3
zhilong@zhilong-virtual-machine:~$ expr 6 / 4
1
或者:
zhilong@zhilong-virtual-machine:~$ a=10
zhilong@zhilong-virtual-machine:~$ b=8
zhilong@zhilong-virtual-machine:~$ echo $(($a / $b))
1
🌙取余:
zhilong@zhilong-virtual-machine:~$ expr 6 % 4
2
🌙混合运算:
zhilong@zhilong-virtual-machine:~$ expr `expr 2 + 4` / 2
3
或者
zhilong@zhilong-virtual-machine:~$ S=$[(2+4)/2]
zhilong@zhilong-virtual-machine:~$ echo $S
3
🌙比较大小
如果成立:打印1;不成立打印0;与
$?
恰恰相反
zhilong@zhilong-virtual-machine:~$ a=10
zhilong@zhilong-virtual-machine:~$ b=6
zhilong@zhilong-virtual-machine:~$ echo $((a>b))
1
zhilong@zhilong-virtual-machine:~$ echo $((a<b))
0
二、条件表达式
🌙1、返回值
条件成立,返回0
条件不成立,返回1
🌙2、文件表达式
-f:判断输入文件是否是一个文件
-d:判断输入的内容是否是一个目录
-x:判断输入的内容是否可执行
-e:判断文件是否存在
zhilong@zhilong-virtual-machine:~$ [ -f /home/zhilong ];echo $?
1
zhilong@zhilong-virtual-machine:~$ [ -d /home/zhilong ];echo $?
0
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ -x /home/zhilong/passwd ];echo $?
1
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ -e /home/zhilong/Desktop/shell_text ];echo $?
0
🌙3、数值操作符
n1 -eq n2:相等
n1 -gt n2:大于
n1 -lt n2:小于
n1 -ne n2:不等于
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ a=10
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ b=8
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ c=6
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ $a -eq $b ];echo $?
1
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ $a -gt $b ];echo $?
0
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ $a -lt $b ];echo $?
1
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ $a -ne $b ];echo $?
0
🌙4、字符串比较
str1 == str2:str1和str2字符串内容一致,字符串与符号之间加空格
str1 != str2:str1和str2字符串内容不一致,字符串与符号之间加空格
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ a='hello'
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ b='hell'
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ c='hello'
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ $a == $b ];echo $?
1
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ $a == $c ];echo $?
0
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ $a != $c ];echo $?
1
🌙5、判断字符串的长度是否为0
特别注意:如果字符串包含空格,需要包裹起来用
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ a="qweqw qweqwe"
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ -z $a ];echo $?
bash: [: qweqw:需要二元表达式
2
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ -z "$a" ];echo $?
1
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ -z "$q" ];echo $?
0
🌙6、判断字符串的长度是否不为0
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ -n "$q" ];echo $?
1
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ -n "$a" ];echo $?
0
🌙7、逻辑表达式
&&: 表示前一条命令执行成功时,才执行后一条命令;
|| :表示上一条命令执行失败后,才执行下一条命令
-a:多个条件和的判断
-o:多个条件或的判断
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ username="kobe"
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ "$username" == "kobe" ] && echo "成功"
成功
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ "$username" == "kobe1" ] && echo "成功" || echo "失败"
失败
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ "$username" == "kobe" ] && echo "成功" || echo "失败"
成功
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ username="kobe"
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ password=123
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ "$username" == "kobe" -a "$password" == "123" ] && echo "成功" || echo "失败"
成功
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ "$username" == "kobe" -a "$password" == "123123" ] && echo "成功" || echo "失败"
失败
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ "$username" == "kobe" -o "$password" == "123123" ] && echo "成功" || echo "失败"
成功
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ [ "$username" == "kobe1" -o "$password" == "123123" ] && echo "成功" || echo "失败"