linux - How to loop over shell script, with different variable, store entire contents of script with variable, and then run another function -
suppose have script test.sh
contains
#!/bin/bash var in var1 var2; in `seq 2`; sh test2.sh $var > tmp.sh cat tmp.sh; done done
and have script test2.sh
looks such
#!/bin/bash echo "i use variable here $1" echo "and again here $1" echo "even third time here $1"
now in first script want pass entire content of test2.sh
current local variable (i.e. on line six: sh test2.sh $var > tmp.sh
) if call sh test2.sh var1
return
i use variable here var1 , again here var1 third time here var1
so want pass entire content of sh test2.sh $var
new shell file, argument in place of variable. hence output should be:
i use variable here var1 , again here var1 third time here var1 use variable here var1 , again here var1 third time here var1 use variable here var2 , again here var2 third time here var2 use variable here var2 , again here var2 third time here var2
thus wondering is; how pass entire shell local argument, new, temporary shell script? wondering how run this:
for var in var1 var2; in `seq 2`; sh (sh test2.sh $var) done done
thanks.
you can read contents of second script test2.sh
, execute in test1.sh
arguments replaced variable value, this:
#!/bin/bash var in var1 var2; in `seq 2`; # contents of test2 variable script=$(cat test2.sh) # set arguments of script in variable , execute eval "set -- $var; $script" done done
but, read on risks of using eval, e.g. here: why should eval avoided in bash, , should use instead?
Comments
Post a Comment