2

Simple question I think.. but I couldnt find it with google..

I have a file test.txt.. it contains;

blah
blah2
blah3

What I want to do is;

cat test.txt and then execute;

./script blah
./script blah2
./script blah3

But I want to do it in one line.. What would be the best ways to do it.. ? I know using && is a option.. but maybe there are better ways to pass the string?

2 Answers 2

5

No need for cat (or any other external tool for that matter). Something like:

while read line; do ./script "$line"; done < test.txt

does what you want, using the shell builtin read.

As pointed out in the comments, this assumes that you are in the same directory as the script. If not, replace ./script with path/to/script.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for beating me in secs.... better change ./script to a full path. in case the script was in a different dir. but perhaps OP could guarantee that won't happen.
True, I've added a note to my answer regarding the path.
4

Using xargs:

xargs -n 1 ./script < test.txt

1 Comment

This works (so +1) and the -n option to xargs is good to be reminded about, but the while read line technique is probably more generic.

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.