0
点赞
收藏
分享

微信扫一扫

《Shell脚本攻略读书》笔记二之数组

時小白 2022-01-26 阅读 84


1 shell脚本中数组的内容是以空格分割,而不是逗号

[root@server5 local]# cat test7.sh
#!/bin/bash
array=(a,b,c,d dfd) # 定义一个数组a
echo "${array[0]}" # 输出数组中的第一个元素
echo "${array[1]}"


[root@server5 local]# ./test7.sh
a,b,c,d
dfd

其它下标使用:


  • 输出数组中的所有数:​​echo ${a[@]}​​​ #输出​​a,b,c d e fgh​
  • 输出下标索引清单:​​echo ${!a[@]}​​​ #输出​​0 1 2 3​

2 shell脚本中连空格都是多余的!

比如下面这个val = ​​expr 2 + 2​​因为val与=之间有空格,导致出错!

[root@server5 local]# cat test8.sh
#!/bin/bash
val = `expr 2 + 2 `
echo "the sum is : ${val}"

[root@server5 local]# ./test8.sh
./test8.sh:行2: val: 未找到命令
the sum is :


[root@server5 local]# cat test8.sh
#!/bin/bash
val=`expr 2 + 2 `
echo "the sum is : ${val}"

[root@server5 local]# ./test8.sh
the sum is : 4



举报

相关推荐

0 条评论