shell 列表 陣列

2021-06-03 18:06:51 字數 3952 閱讀 8630

shell的列表(陣列?)絕對是讓我最迷惑的語法(沒有之一)。

在$*和$@和分號之間糾結已久。

忘了哪本書裡有這句話,記下來了。

"$" returns all the items as a single word, whereas "$" 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 $

beijing

陣列大小

echo $

陣列遍歷

# echo $

# echo $

遍歷是最讓我困擾的點.雖然對平時沒什麼大影響,對於我來說,直接用最簡易的空格分割就搞定了.

可是,總結試驗也就如此.在筆記前總結以下下**試驗:

filename=([0]="line1" [1]="line2" [2]="line3" [3]="line4")

"$" 沒有for遍歷 ([@]是正統的arr遍歷)

filename='line1 line2 line3 line4'

"$" "$" 都沒有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 $; do echo $x, "yeah"; done

line1, yeah

line2, yeah

line3, yeah

line4, yeah

$ for x in $; do echo $x, "yeah"; done

line1, yeah

line2, yeah

line3, yeah

line4, yeah

$ for x in "$"; do echo $x, "yeah"; done

line1 line2 line3 line4, yeah

$ for x in "$"; 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 $; 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 $; 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 $; 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 $; do echo $x, "yeah"; done

line1, yeah

line2, yeah

line3, yeah

line4, yeah

$ for x in "$"; do echo $x, "yeah"; done

line1 line2 line3 line4, yeah

$ for x in $; do echo $x, "yeah"; done

line1, yeah

line2, yeah

line3, yeah

line4, yeah

$ for x in "$"; 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: '') ; do /sbin/ifconfig | sed -n $p | awk -f: '' | awk ''; done

shell 列表操作

這是列表的各種新增方式,拷貝自他人部落格。詳細用法參考 刪除列表元素用法 unset array name index 其中,array name 表示陣列名,index 表示陣列下標。如果不寫下標,寫成下面這樣就是刪除整個陣列 unset array name 下圖是我本人借助這兩個用法寫得的乙個...

Shell陣列 shell陣列的定義 陣列長度

shell在程式設計方面比windows批處理強大很多,無論是在迴圈 運算。bash支援一維陣列 不支援多維陣列 並且沒有限定陣列的大小。類似與c語言,陣列元素的下標由0開始編號。獲取陣列中的元素要利用下標,下標可以是整數或算術表示式,其值應大於或等於0。在shell中,用括號來表示陣列,陣列元素用...

Shell陣列 shell陣列的定義 陣列長度

shell在程式設計方面比windows批處理強大很多,無論是在迴圈 運算。bash支援一維陣列 不支援多維陣列 並且沒有限定陣列的大小。類似與c語言,陣列元素的下標由0開始編號。獲取陣列中的元素要利用下標,下標可以是整數或算術表示式,其值應大於或等於0。在shell中,用括號來表示陣列,陣列元素用...