shell运算

shell运算

整数运算:expr,$(()),$[]


expr

使用expr进行运算,如果遇到特殊字符,则需要转义,如*

[root@nginx ~]# num1=2
[root@nginx ~]# num2=5
[root@nginx ~]# expr $num1 + $num2
7
[root@nginx ~]# expr $num1 - $num2
-3
[root@nginx ~]# expr $num1 * $num2		#需要转移
10
[root@nginx ~]# expr $num2 / $num1
2
[root@nginx ~]# expr $num2 % $num1
1
[root@nginx ~]#

$(()) $[]

​ 不需要转义,都可以直接使用


计算字符串长度:

[root@nginx opt]# a="abcd"
[root@nginx opt]# echo $(($(echo $a|wc -c)-1))		# 比较麻烦
4
[root@nginx opt]# echo ${#a}	# 高级用法
4
[root@nginx opt]# 

EOF多行输出:

# cat << EOF > test.sh
> #!/bin/bash
> #you Shell script writes here.
> EOF
 
结果:
引用# cat test.sh
#!/bin/bash
#you Shell script writes here.



[root@nginx opt]# cat <<EOF
> hello world
> this is my girlfriend!
> what's wrong?
> Are you OK?
> EOF
hello world
this is my girlfriend!
what's wrong?
Are you OK?
[root@nginx opt]# 

if语句中的文件比较

条件成立,则执行&&后面的内容			# &&
条件不成立,则执行 || 后面的		# ||

数值比较

原文地址:https://www.cnblogs.com/xueheng36/p/11717122.html