0

I'm struggling with passing a shell command. Specifically, I have written a shell file that the user will run. In it, another shell file is written based on the user inputs. This file will then be passed to a command that submits the job.

Now in this internal shell file I have a variable containing a function. However, when I run the user shell script I can't get this function to pass in a way that the internal shell file can use it.

I can't share my work but I'll try to make an example

#User shell script
cat >test.txt <<EOF
#a bunch of lines that are not relevant
var=`grep examples input.txt`
/bin/localfoo -${var}
EOF
# pass test.txt to localfoo2
/bin/localfoo2 /test.txt

When I run the 'User Shell Script' it prints that grep can't find the file, but I don't want grep to be evaluated. I need it to be written, as is, so that when the line '/bin/localfoo2 /test.txt' is read, grep is evaluated.

I've tried a number of things. I've tried double back ticks, i've tried using 'echo $(eval $var)'. But none of the methods I've found through googling have managed to pass this var in a way that will accomplish what I want.

Your help is much appreciated.

1 Answers1

0

You can try with single quote ('). You have to put the single quote in before the grep command and end of the grep command like below.

#User shell script
cat >test.txt <<EOF
#a bunch of lines that are not relevant
var='`grep examples input.txt`'
/bin/localfoo -${var}
EOF
# pass test.txt to localfoo2
/bin/localfoo2 /test.txt

I did not understand where you have to execute that grep command. If you want to execute the grep command inside the localfoo script, I hope this method will help.

sureshkumar
  • 589
  • 5
  • 15
  • The grep command should be executed when localfoo2 is executed. It takes test.txt as input. I tried wrapping it in single quotes, but then test.txt just contains var='' – open_eye_signal Jan 18 '17 at 12:33
  • You just remove the back tick as like var='grep examples input.txt'. Try this. – sureshkumar Jan 18 '17 at 12:39
  • But then it just becomes a common string doesn't it? You'd need an eval then to actually obtain the value from grep. – open_eye_signal Jan 18 '17 at 12:44
  • 1
    I found the solution in another thread http://stackoverflow.com/questions/13122147/prevent-expressions-enclosed-in-backticks-from-being-evaluated-in-heredocs It seems that putting "EOF" like so does the trick. – open_eye_signal Jan 18 '17 at 14:16
  • The other method is to put a backslash before backticks and `$`, while using `<< EOF`. – codeforester Jan 18 '17 at 19:40