Alex van Es
Alex van Es

Reputation: 1181

Use the output of a cat command in execution (bash)

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?

Upvotes: 2

Views: 8944

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74595

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.

Upvotes: 5

falsetru
falsetru

Reputation: 368964

Using xargs:

xargs -n 1 ./script < test.txt

Upvotes: 4

Related Questions