2014-10-07 23 views
0

我在使用shell腳本循環時遇到問題。我有一個要求,我必須從平面文件中讀取內容並逐字取代它。例如:嵌套在unix腳本中循環使用

文件內容如下:

Test1 001 
Test2 002 

和我的腳本如下:

#!/bin/sh 
cd /directorypath/bin/ 
while read line; do 
    for word in $line; do 
     for word1 in $line; do 
      nohup ./startcmd.sh attribute1=$word attribute2=$word1  
     done 
    done 
done < /directorypath/test1.txt 

,但上面的代碼中沒有得到所需的輸出。

我需要的輸出如下:

./startcmd Test1 001 
./startcmd Test2 002 

任何人都可以請幫我在相同的。

由於

+0

考慮使用的Perl,Python或Ruby像這樣 – JoelFan 2014-10-07 18:55:42

+0

任務,爲什麼你有相同的'在$行字;嵌套? – anubhava 2014-10-07 18:58:08

回答

1

這應該工作打算:

while read line; do 
    nohup ./startcmd.sh $line 
done < /directorypath/test1.txt 

$line將包含兩個字,這將被視爲可變膨脹之後單獨參數。

回答到更新的問題

while read attrib1 attrib2; do 
    nohup ./startcmd.sh attribute1=$attrib1 attribute2=$attrib2 
done < /directorypath/test1.txt 
+0

嗨,nohup./startcmd有一些屬性:我必須運行命令爲nohup ./startcmd.sh attribute1 = $ word attribute2 = $ word1。所以我必須用文字來分隔線條內容。 – CounterAgent 2014-10-07 18:59:22

+0

該問題未顯示「attribute = value」語法。它顯示命令行參數語法。更新問題,然後我會更新答案。 – isedev 2014-10-07 19:04:42

+0

更新了問題 – CounterAgent 2014-10-07 19:05:18