嵌套在unix shell脚本中循环

时间:2014-10-07 18:53:35

标签: shell unix

我在shell脚本中遇到问题。我有一个要求,我必须从平面文件中读取内容并逐字替换。例如:

文件内容如下:

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

任何人都可以帮助我。

由于

1 个答案:

答案 0 :(得分: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
相关问题