linux中(),(()),[],[[]]
1、(),在括号中的命令列表, 将会作为一个子shell来运行.通常与$(command)一起使用,效果如`command`,返回执行命令的结果[root@master ~]# cat brackets.sh#!/bin/bashn=$(ls)echo $n[root@master ~]# sh brackets.sh2 2019-01-08.tar 2.txt a.csv a...
·
1、(),在括号中的命令列表, 将会作为一个子shell来运行.
通常与$(command)一起使用,效果如`command`,返回执行命令的结果
[root@master ~]# cat brackets.sh
#!/bin/bash
n=$(ls)
echo $n
[root@master ~]# sh brackets.sh
2 2019-01-08.tar 2.txt a.csv anaconda-ks.cfg ansible.cfg args.yml a.sh block.yml brackets.sh centos7.cfg date.yml delegate_to.yml Desktop Dockerfile Documents Downloads fail.yml
2、(()),用来计算并测试算术表达式的结果. 还可应用到c风格的for,while循环语句,(( )) 中,所有的变量(加不加$无所谓)都是数值。
[root@master ~]# cat ll.sh
((3>2))
echo $?
((2>3))
echo $?
[root@master ~]# sh ll.sh
0
1
[root@master ~]# cat hh.sh
LIMIT=10
for ((a=1;a<=LIMIT;a++))
do
echo "$a"
done
[root@master ~]# sh hh.sh
1
2
3
4
5
6
7
8
9
10
3、[],条件测试表达式放在[ ]中.
[root@master ~]# cat hh.sh
if [ 2 -gt 1 ];then
echo "yes"
else
echo "no"
fi
[root@master ~]# sh hh.sh
yes
4、[[]],就是条件表达式。在bash中,字符串比较用 > < != == <= >= ,只是在[]中 < >需要转义;对于数值比较.用 -lt -le -eq -ge -gt 来比较,与[[]]中表达不太一样,[[]]中表达式不需要转义。在[ ] 中的 < > 需要用转义 \< \>,如果有多个表达式,在[[ ]] 中用 && || 来组合,而[] 中是用 -a -o 来组合
[root@master ~]# cat hh.sh
#!/bin/bash
INT=-15a
if [[ "$INT" =~ ^-?[0-9]+$ ]]; then
echo "INT is an integer."
else
echo "INT is not an integer." >&2
exit 1
fi
[root@master ~]# sh hh.sh
INT is not an integer.
[[...]] || [[...]] 和[[... || ...]],结果一样。
[[...]] && [[...]] 和[[... && ...]],结果一样。
参考:
更多推荐
已为社区贡献2条内容
所有评论(0)