1

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?

6
  • 1
    Do you really have IFS- or is that a typo? You want IFS= Commented Sep 7, 2022 at 15:04
  • 2
    Does netNames.txt contain \r\n line endings? Commented Sep 7, 2022 at 15:04
  • 1
    (1) Run your script with bash -x yourscript and read the trace logs emitted (and edit them into the question). (2) Include the error message in your question; let us evaluate what it "seems". (3) Glenn's comment above is almost certainly your problem. Commented Sep 7, 2022 at 15:51
  • It won't help your problem here, btw, but --netId "$netIdValue" is how the above code should be quoted. Commented Sep 7, 2022 at 15:52
  • 1
    (also, why use a line variable instead of read -r netIdValue to put the value directly into the name you want?) Commented Sep 7, 2022 at 16:33

1 Answer 1

1

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.

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

Comments

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.