1

I have a simple bash script which calls another script 10 times:

#!/bin/sh

val=10
for i in `seq $val`
do
       ./script2.sh flename$i
done

script2.sh take 2 input parameters (not cmd arguments, two input variables) (for me it does not matter what parameters, they can be just some 2 identical strings). Now = how can I pass those 2 identical strings to my script2.sh in my for loop?

I tried this:

./script2.sh flename$i | string1 | string1

but it does not work as I want it to.

4
  • What are you using | for? Commented Mar 4, 2014 at 14:13
  • why not just export two environment variables? can you show script2.sh to have a better understanding of what you are trying to do? Commented Mar 4, 2014 at 14:20
  • 2
    What does "two input variables" mean? Does script2.sh read two lines from standard input? Does it assume particular variables will be present in its environment? Commented Mar 4, 2014 at 14:41
  • This is not a bash script, it is an sh script. If you change the shebang to #!/bin/bash it opens up a lot of possibilities for different approaches (at the cost of portability). If you intend this to be a shell script, you should remove the word "bash" from your question and tags. Otherwise, you should change the shebang. Commented Mar 4, 2014 at 17:04

2 Answers 2

1

The syntax for that would be something like

printf '%s\n' "$string1" "$string2" | ./script2.sh filename"$i"

You could also use a here document:

./script2.sh filename"$i" <<____HERE
$string1
$string2
____HERE

Some people will tell you echo but it has various portability issues etc; printf is uniformly standard and safer. But if it's just two static lines of string, you can certainly do

echo 'string1
string2' | ./script2.sh filename "$i"

(yes, that is a single-quoted string with a newline in the middle) or somewhat less efficiently

( echo string1; echo string2 ) | ./script2.sh filename"$i"

The general syntax of a pipeline is command1 | command2 | command... so what you had would not work unless string1 was a command which produced the desired output. (This is not impossible to do, but probably not a good idea in this particular scenario.) But on top of that, you were piping the output from the script to string1 where you clearly want vice versa.

Any command which produces suitable output can be used to feed the pipeline; if you need lots of input, try yes, which exists specifically for this purpose;

yes string1 | head -n 2 | ./script2.sh filename"$i"

Here, yes prints an endless sequence of string1 lines (well, it ends when the pipeline is closed by head); head passes through just the first two.

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

Comments

0

You just need to use

./script2.sh flename$i "string1" "string2"

Then in script2, filename$i will be available as $1, string1 as $2, and string2 as $3

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.