shell 列表 数组

shell的列表(数组?)绝对是让我最迷惑的语法(没有之一)。
在$*和$@和分号之间纠结已久。
忘了哪本书里有这句话,记下来了。
"${arr[*]}" returns all the items as a single word, whereas "${arr[@]}" returns each item as a separate word.

在做笔记前先写总结:
1)如果不确定,先写下小代码片段验证吧,脚本出错后果很严重;
2)如果确认是字符串,请务必加上双引号;
3)尽量使代码逻辑简单,即使增多了代码量.

数组声明
declare -a arr

数组赋值
1)
arr[0]=BeiJing
arr[1]=GuangZhou
arr[2]=ShenZhen
2)
arr=([0]=BeiJing [1]=GuangZhou [2]=ShenZhen)
3)
arr=(BeiJing GuangZhou ShenZhen)

数组引用
echo ${arr[0]}
BeiJing

数组大小
echo ${#arr[@]}

数组遍历
# echo ${name[*]}
# echo ${name[@]}

遍历是最让我困扰的点.虽然对平时没什么大影响,对于我来说,直接用最简易的空格分割就搞定了.
可是,总结试验也就如此.在笔记前总结以下下代码试验:
filename=([0]="line1" [1]="line2" [2]="line3" [3]="line4")
"${filename[*]}" 没有for遍历 ([@]是正统的arr遍历)
filename='line1 line2 line3 line4'
"${filename[*]}" "${filename[@]}" 都没有for遍历(""括起来表面其成为一个整体.不加""就是以空格分割,可以遍历,回车符同理)

以下是试验代码

$ cat arr
line1
line2
line3
line4

$ filename='line1 line2 line3 line4'

$ declare | grep filename
filename='line1 line2 line3 line4'

$ for x in $filename; do echo $x, "yeah"; done
line1, yeah
line2, yeah
line3, yeah
line4, yeah

$ for x in ${filename[*]}; do echo $x, "yeah"; done
line1, yeah
line2, yeah
line3, yeah
line4, yeah

$ for x in ${filename[@]}; do echo $x, "yeah"; done
line1, yeah
line2, yeah
line3, yeah
line4, yeah

$ for x in "${filename[*]}"; do echo $x, "yeah"; done
line1 line2 line3 line4, yeah

$ for x in "${filename[@]}"; do echo $x, "yeah"; done
line1 line2 line3 line4, yeah

$ filename="line1
line2
line3
"

$ declare | grep filename
filename=$'line1\nline2\nline3\n'

$ for x in ${filename[@]}; do echo $x, "yeah"; done
line1, yeah
line2, yeah
line3, yeah

$ for x in $filename; do echo $x, "yeah"; done
line1, yeah
line2, yeah
line3, yeah

$ filename=`cat arr`
$ declare | grep filename
filename=$'line1\nline2\nline3\nline4'
$ for x in ${filename[@]}; do echo $x, "yeah"; done
line1, yeah
line2, yeah
line3, yeah
line4, yeah
$ for x in $filename; do echo $x, "yeah"; done
line1, yeah
line2, yeah
line3, yeah
line4, yeah

$ filename=`cat arr | tr '\n' ' '`
$ declare | grep filename -w
filename='line1 line2 line3 line4 '
$ for x in ${filename[@]}; do echo $x, "yeah"; done
line1, yeah
line2, yeah
line3, yeah
line4, yeah
$ for x in $filename; do echo $x, "yeah"; done
line1, yeah
line2, yeah
line3, yeah
line4, yeah

$ filename=(`cat arr|sed -e :a -e '$!N;s/\\n/ /;ta'`)
$ declare | grep filename
filename=([0]="line1" [1]="line2" [2]="line3" [3]="line4")
$ for x in $filename; do echo $x, "yeah"; done
line1, yeah
$ for x in ${filename[*]}; do echo $x, "yeah"; done
line1, yeah
line2, yeah
line3, yeah
line4, yeah
$ for x in "${filename[*]}"; do echo $x, "yeah"; done
line1 line2 line3 line4, yeah
$ for x in ${filename[@]}; do echo $x, "yeah"; done
line1, yeah
line2, yeah
line3, yeah
line4, yeah
$ for x in "${filename[@]}"; do echo $x, "yeah"; done
line1, yeah
line2, yeah
line3, yeah
line4, yeah

几个特别用法:

一个文件一行文本为一个元素存入一个列表中?
$ cat /etc/shells | tr "\n" " " > /tmp/tmp.file
$ read -a SHELLS < /tmp/tmp.file
下面这个有问题,暂时没想懂为什么
$ cat /etc/shells | tr "\n" " " | read -a SHELLS
也有别的做法,即上文用到的,不利用临时文件
$ filename=(`cat filename|sed -e :a -e '$!N;s/\\n/ /;ta'`)

还有一个偏门的用法.我有几块网卡,eth0,eth1.怎样一次性提出来其ip?
因为工作环境需要不得不写成一行了.不过也很好理解.如果有更好的方法,谢谢您告诉我.
for x in $(/sbin/ifconfig | grep eth -n | awk -F: '{print $1+1}') ; do /sbin/ifconfig | sed -n ${x}p | awk -F: '{print $2}' | awk '{print $1}'; done
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Shell 遍历数组可以使用 for 循环来实现,具体步骤如下: 1. 定义数组:可以使用以下语法定义一个数组: ``` array=(value1 value2 value3 ...) ``` 2. 遍历数组:使用 for 循环遍历数组,语法如下: ``` for var in ${array[@]} do echo $var done ``` 其中,${array[@]} 表示将数组中的所有元素作为一个列表返回,$var 表示当前遍历到的数组元素。 例如,以下代码定义了一个数组并遍历输出其中的元素: ``` array=(apple banana orange) for var in ${array[@]} do echo $var done ``` 输出结果为: ``` apple banana orange ``` ### 回答2: Shell 是一个强大的脚本语言,它内置了许多操作数组的函数和方法,可以方便地遍历数组。 首先,我们需要定义一个数组变量,语法如下: ```shell array=(value1 value2 ... valueN) ``` 其中,value1~valueN 为数组元素的值。 接下来,我们可以使用 for 循环遍历数组,语法如下: ```shell for i in "${array[@]}" do # 执行操作 done ``` 其中,${array[@]} 表示整个数组,$i 表示当前遍历到的数组元素的值。我们可以在 do...done 中执行具体的操作,如打印数组元素、修改数组元素等。 下面是一个示例,演示如何遍历数组并打印数组元素: ```shell #!/bin/bash array=("apple" "banana" "orange" "grape" "lemon") for i in "${array[@]}" do echo $i done ``` 运行上述脚本,可以输出以下结果: ```shell apple banana orange grape lemon ``` 除了 for 循环,我们还可以使用 while 循环遍历数组。其原理是使用一个计数器来迭代数组元素,直到计数器达到数组元素个数为止。while 循环的语法如下: ```shell i=0 while [ $i -lt ${#array[@]} ] do # 执行操作 let i++ done ``` 其中,${#array[@]} 表示数组元素的个数。我们可以在 do...done 中执行具体的操作,如打印数组元素、修改数组元素等。 下面是一个示例,演示如何使用 while 循环遍历数组并打印数组元素: ```shell #!/bin/bash array=("apple" "banana" "orange" "grape" "lemon") i=0 while [ $i -lt ${#array[@]} ] do echo ${array[i]} let i++ done ``` 运行上述脚本,可以输出以下结果: ```shell apple banana orange grape lemon ``` 综上所述,Shell 遍历数组非常简单,只需要使用 for 或 while 循环即可。同时,我们还可以使用数组下标访问数组元素,使用数组长度获取数组元素个数,使用数组赋值修改数组元素等。掌握这些技巧可以帮助我们更好地操作数组,提高 Shell 脚本编程效率。 ### 回答3: 在Shell脚本中,遍历数组是一个非常基本的操作,通常我们使用循环语句来实现。Shell数组的定义方式如下: ```bash arr=(value1 value2 value3 ...) ``` 其中arr是数组名,value1、value2、value3等是数组元素,元素之间用空格分隔。接下来是遍历数组的几种常见方式。 #### 1. for循环遍历数组 ```bash arr=(1 2 3 4 5) for i in ${arr[@]}; do echo $i done ``` 使用`[@]`或者`[*]`可以遍历整个数组,每次循环将数组中的元素赋值给`$i`。 #### 2. while循环遍历数组 ```bash arr=(1 2 3 4 5) i=0 while [ $i -lt ${#arr[@]} ]; do echo ${arr[$i]} i=$[$i+1] # 注意这里的写法 done ``` 使用while循环的方法,在循环中需要使用索引下标,`$[$i+1]`是一种常见的计算方式。 #### 3. until循环遍历数组 ```bash arr=(1 2 3 4 5) i=0 until [ $i -ge ${#arr[@]} ]; do echo ${arr[$i]} i=$[$i+1] # 同样需要计算 done ``` 使用until循环类似于while循环,只是条件判断的方式不同,这里使用了`-ge`表示大于等于。 #### 4. select循环遍历数组 ```bash arr=(1 2 3 4 5) select i in ${arr[@]}; do echo $i done ``` 使用select循环可以方便地让用户选择数组中的元素,每次循环将用户的选择赋值给`$i`。 以上就是几种常见的Shell遍历数组的方式,它们都可以在需要的时候用到。同时,对于大规模的数组遍历,我们也可以考虑使用并行计算等方法提高效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值