-1

Suppose I have a script test.sh that contains

#!/bin/bash

for var in var1 var2;
do
  for i in `seq 2`; do
    sh test2.sh $var > tmp.sh
    cat tmp.sh;
  done
done

and I have another script test2.sh that looks as such

#!/bin/bash

echo "I use the variable here $1"
echo "and again here $1"
echo "even a third time here $1"

Now in my first script what I want to do is to pass the entire content of test2.sh with the current local variable (i.e. on line six: sh test2.sh $var > tmp.sh) so if I were to call say sh test2.sh var1 then it would return

I use the variable here var1
and again here var1
even a third time here var1

So I want to pass the entire content of sh test2.sh $var to a new shell file, but with the argument in place of the variable. Hence the output should be:

I use the variable here var1
and again here var1
even a third time here var1

I use the variable here var1
and again here var1
even a third time here var1

I use the variable here var2
and again here var2
even a third time here var2

I use the variable here var2
and again here var2
even a third time here var2

Thus what I am really wondering is; how do I pass the entire shell with a local argument, to a new, temporary shell script? Really what I wondering is how I run something like this:

for var in var1 var2;
do
  for i in `seq 2`; do
  sh (sh test2.sh $var)
  done
done

Thanks.

Astrid
  • 1,846
  • 4
  • 26
  • 48
  • I'm not at all sure I understand what you want to do. Partly because you have terminology problems. `test.sh` is not a function it is a script. "A shell" is `bash`, `sh`, `ksh`, etc. not a script or a function or output. You say you have two scripts one of which calls the other. What do you want to do with the output of the second script? Or do you not want the output of the second script but instead the actual *contents* of the second script but with certain variables replaced? – Etan Reisner Aug 31 '15 at 01:18
  • Sorry, fixed this now. What I want is the latter of your two questions. – Astrid Aug 31 '15 at 01:21
  • Running a shell script gets you its output. If you want to modify its contents then you can't run it and you need to operate on it as textual data (with something like `sed` or `awk` or perl/python/etc.). – Etan Reisner Aug 31 '15 at 01:28
  • ok... would you know how to do that? – Astrid Aug 31 '15 at 01:30
  • 1
    There are *hugely* many questions on this site about replacing text in a file with other text. Doing what you want without using temporary files is a more complicated question but I'd probably recommend just not doing that unless you have a real reason to need to. – Etan Reisner Aug 31 '15 at 01:36

1 Answers1

2

You can read the contents of the second script test2.sh and execute it in test1.sh with the arguments replaced with your variable value, like this:

#!/bin/bash

for var in var1 var2;
do
    for i in `seq 2`; do
        # Get the contents of test2 to a variable
        script=$(cat test2.sh)
        # Set the arguments of the script in the variable and execute
        eval "set -- $var; $script"
    done
done

But, read up on the risks of using eval, e.g. here: Why should eval be avoided in Bash, and what should I use instead?

Community
  • 1
  • 1
Andrzej Pronobis
  • 33,828
  • 17
  • 76
  • 92
  • How does this address the (clarified in comments) question? This seems , unless I missed something, to just be an inefficient (and potentially dangerous) version of the original code which calls the second script in the loop. – Etan Reisner Aug 31 '15 at 01:35
  • Honestly, I am not sure what is the purpose of this. My assumption was that the OP wants to execute a template script read from another file in a loop for some reason. – Andrzej Pronobis Aug 31 '15 at 01:36
  • The point is that I am trying to create a many very large run scripts for distributed computing. Each run script is parametrised by several different variables, and each script needs to be run several times, and they all need to started at the same time. This is why I need to create all the jobs with a process like the above. As I do not feel like copying and pasting to that degree. I do not know if this is the most efficient way granted. – Astrid Aug 31 '15 at 01:38
  • Ok, so why again would you not just want to execute the scripts with arguments? – Andrzej Pronobis Aug 31 '15 at 01:41
  • Well I do, but I am still new to distributed computing, so I am trying to figure out how exactly to do that. – Astrid Aug 31 '15 at 01:45
  • I'm still lost in what you are trying to achieve overall. Are you trying to execute the output of the script or just the script? – Andrzej Pronobis Aug 31 '15 at 01:49
  • If you are just trying to *run* the script many times for different variables then just do that. The distributed computing framework almost certainly has a way to supply arguments to the script being run the way normal scripts can have arguments. – Etan Reisner Aug 31 '15 at 01:52