while read line 只執行第一行的解決辦法

寫shell腳本的時候,常常須要逐行讀取文件內容,而咱們經常採用while read line重定向到文件,可是執行過程則會遇到問題:
cat list
172.16.50.175   t-1
172.16.50.176   t-2
172.16.50.177   t-3
172.16.50.178   t-4
172.16.50.179   t-5

cat get_hostname.sh
css

  1. #!/bin/shshell

  2. cat list | while read linessh

  3. ip=`awk '{print $1}' tmp`ide

  4. dospa

  5. ssh $ip "hostname"orm

  6. doneip

下面來看執行結果:
test-1.XXX.com
啊哦,彷佛while循環只執行了一行就不在執行了,這個很糟糕
那麼如何來改進這個腳本呢?

get


  1. #!/bin/shit


  2. exec 3< listclass

  3. while read -u3 line

  4. do

  5. ip=`echo $line |awk '{print $1}'`

  6. ssh $ip "hostname"

  7. done

  8. exec 3<&-

好,再來執行,查看結果:
test-1.XXX.com
test-2.XXX.comtest-3.XXX.comtest-4.XXX.comtest-5.XXX.com好,結果是咱們想要的。至於爲何,本身仔細思考

相關文章
相關標籤/搜索