shell中数组的表示
方法:
array_name=(ele1 ele2 ele3 … elen)
举例:
[root@hadoop01 ~]
[root@hadoop01 ~]
[root@hadoop01 ~]
[root@hadoop01 ~]
/u01/app/grid/diag/tnslsnr
[root@hadoop01 ~]
注意事项:1、赋值号=两边不能有空格,必须紧挨着数组名和数组元素
2、数组内类型可以不同
[root@hadoop01 ~]
[root@hadoop01 ~]
1
[root@hadoop01 ~]
www
[root@hadoop01 ~]
打印所有元素
[root@hadoop01 ~]
1 2 www
[root@hadoop01 ~]
shell中使用for循环遍历数组
[root@hadoop01 ~]
[root@hadoop01 ~]
> do
> echo ${i}
> done
c
d
www
[root@hadoop01 ~]
shell中读取文本文件中的内容做变量
[root@hadoop01 ~]
1
0
2
[root@hadoop01 ~]
for line in `cat test.txt`
do
echo ${line}
done
[root@hadoop01 ~]
[root@hadoop01 ~]
1
0
2
[root@hadoop01 ~]
shell中使用if语句
[root@hadoop01 ~]
ucc
[root@hadoop01 ~]
line=`cat test.txt`
if [ ${line}="succ" ];then
echo "succ"
fi
[root@hadoop01 ~]
succ
[root@hadoop01 ~]
注意:此处输出是不对的,因为文本内容并不是succ,二是ucc,if却判断为成功了。
原因是${line}="succ"这一句,中间缺少了空格。
修改后如下:
[root@hadoop01 ~]
ucc
[root@hadoop01 ~]
line=`cat test.txt`
if [ ${line} = "succ" ];then
echo "succ"
fi
[root@hadoop01 ~]
[root@hadoop01 ~]
注意:此处没有了输出,才说明是正确的
注意事项:
if语句中的判断语法,等号两边需要加空格
补充
shell中的单引号和双引号的区别
结论:单引号中的字符串不会做任何处理
双引号中的字符串会做处理,如处理转义字符(\n)
[root@hadoop01 ~]
[root@hadoop01 ~]
[root@hadoop01 ~]
\n
[root@hadoop01 ~]
\\n
[root@hadoop01 ~]
即,如果希望字符串不做任何编号,则使用单引号即可。
shell中的变量加大括号和不加的区别
[root@hadoop01 ~]
[root@hadoop01 ~]
[root@hadoop01 ~]
\\nvv
[root@hadoop01 ~]
即,在使用变量时,外面加上大括号是可靠的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)