0

I am facing issue in looping in shell script. I have a requirement where i have to read the content from a flat file and substitue it word by word. For example:

file content is as below:

Test1 001
Test2 002

and my script is as follows:

#!/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

but the above snippet is not giving the desired output.

I need the output as below:

./startcmd Test1 001
./startcmd Test2 002

Can anyone please help me in the same.

Thanks

2
  • Look into using Perl, Python or Ruby for tasks like this Commented Oct 7, 2014 at 18:55
  • Why do you have same for word in $line; do nested? Commented Oct 7, 2014 at 18:58

1 Answer 1

1

This should work as intended:

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

$line will contain two words, which will be treated as separate arguments after variable expansion.

Answer to updated question

while read attrib1 attrib2; do
    nohup ./startcmd.sh attribute1=$attrib1 attribute2=$attrib2
done < /directorypath/test1.txt
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, the nohup./startcmd has some attributes: I have to run the command as nohup ./startcmd.sh attribute1=$word attribute2=$word1. So i have to seperate the line contents in words.
The question does not show attribute=value syntax. It shows command line argument syntax. Update the question, then I'll update the answer.
Updated answer for that use case as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.