Run command line with named arguments from another bash script

Viewed 42

I have one script that takes the name of argument and its value, like this:

script1.sh --netId netIdValue

However, I'd like to call this command multiple times in another script, for the different values of netId argument. So, in script2.sh I want to read the values of netIdValue from the .txt file and then to call the same command, like this:

while IFS= read -r line; do
    netIdValue=$line
    ./script1.sh --netId $netIdValue
done < netNames.txt

But, this fails, and it seem the problem is that it does not take --netId properly. How can I pass the argument name and its value in script2.sh?

1 Answers

My script1.sh is:

#!/bin/sh
echo "$1" "$2" - "$#"

If your ID's are in a file one per line, do this:

% cat file
1
2
% cat file | \
    xargs -I {} ./script1.sh --netId {}
--netId 1 - 2
--netId 2 - 2

If your ID's are on one line separated by spaces, do this:

% cat file
1 2
% cat file | tr ' ' '\0' | \
    xargs -0 -I {} ./script1.sh --netId {}
--netId 1 - 2
--netId 2 - 2

For the separation, I'm using tr to translate the spaces to null (\0) and using xargs -0 option to separate inputs by nulls.

Related