Reputation: 11
I'm writing a bash script to basically run one script (i.e script called list
) that is already made, then take the results of the executed list
script and add the listed items into another script that is made (let's call it export
).
To break it down
list localhost
(this would then create a list)
0001
0002
0003
...
then I want to take the items listed (0001
, 0002
, 0003
) and add them as parameters to another script (called export
). This would then needed to be run as many times as there are items listed. So if there are 3 items in the list, export
would run 3 times with the name of the items listed.
export 0001
export 0002
export 0003
Upvotes: 1
Views: 211
Reputation: 157937
Alternatively to the read
command in a while
loop you can use xargs:
./list localhost | xargs -L1 ./export.sh
xargs -L1
calls ./export.sh
once for every line of output ./list localhost
produces. The line will be subject to word splitting. Meaning if ./list
outputs a line like foo bar
, xargs
will call ./export.sh
with two arguments: foo
and bar
. If you want to pass the whole line as a single argument instead like `./export.sh "foo bar" you can use the newline symbol as delimiter (with GNU xargs):
./list localhost | xargs -L1 -d '\n' ./export.sh
Another, portable, option (thanks mklement0) to control this behaviour is using the -I
option to specify a placeholder for the argument and specify how it should be used in the command:
# Will call like ./export.sh "foo bar" (quoted as single argument)
./list localhost | xargs -I '{}' ./export.sh {}
Upvotes: 2
Reputation: 701
./list.sh localhost | while read -r item; do ./export.sh "$item"; done
Explanation:
./list.sh localhost
outputs the itemsread -r item
reads one line of the output and save it to the variable $item
. -r
prevents read
from expanding escape sequences in the input. Just in case.while
loop over all the lines (while …; do …; done
) to call export.sh
on all itemsUpvotes: 3
Reputation: 112
You can use `` in your command for getting the job done quickly:
for i in `./list localhost`; do ./export $i; done
Upvotes: -1
Reputation: 169
You can use a 'piped' command to do what you want.
list localhost | myscript.sh
This will push output results from 'list localhost' into 'myscript.sh'.
Upvotes: -1