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