2

I get a "Illegal variable name" error for the following piece of line in one of my shell scripts -

"$WORKING_DIR"/sendEmail.py "$TEST_STRING, Tests passed" "$(cat "$WORKING_DIR"/logs/"$THE_PACKAGE"/testResults.out)" >& "$WORKING_DIR"/logs/mailOut.txt

I believe the error is coming from the following line:

"$(cat "$WORKING_DIR"/logs/"$THE_PACKAGE"/testResults.out)"

I have looked extensively on the internet, but have not yet been able to resolve the issue.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
Ashesh
  • 33
  • 1
  • 1
  • 3

2 Answers2

3

The $() syntax isn't valid in csh, that will work only in Bourne shells such as /bin/sh, ksh, zsh, bash, etc. For csh, you're limited to backticks (`).

It's probably also best to quote the entire pathnames instead of just the variables: "$var/logs" instead of "$var"/logs; no real reason not to ;-)

Putting that together, with some added newlines for readability, we get:

"$WORKING_DIR/sendEmail.py" "$TEST_STRING, Tests passed" \
    "`cat "$WORKING_DIR/logs/$THE_PACKAGE/testResults.out"`"\
    >& "$WORKING_DIR/logs/mailOut.txt"

That being said, if you wrote this Python script it's probably best to modify it so that it reads from stdin:

cat "$WORKING_DIR/logs/$THE_PACKAGE/testResults.out" | \
    "$WORKING_DIR/sendEmail.py" "$TEST_STRING, Tests passed" \
    >& "$WORKING_DIR/logs/mailOut.txt"
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
-1

Try this:

ZZZ=$(cat "$WORKING_DIR/logs/$THE_PACKAGE/testResults.out")

"$WORKING_DIR"/sendEmail.py "$TEST_STRING, Tests passed" "$ZZZ" >& "$WORKING_DIR"/logs/mailOut.txt

Or
ZZZ=`cat "$WORKING_DIR/logs/$THE_PACKAGE/testResults.out"`

Bor Laze
  • 2,458
  • 12
  • 20
  • Now I get the error in this line - ZZZ=$(cat "$WORKING_DIR/logs/$THE_PACKAGE/testResults.out") I am using a tcsh shell. So I even tried setenv ZZZ $(cat "$WORKING_DIR/logs/$THE_PACKAGE/testResults.out") – Ashesh May 01 '17 at 19:44
  • `$()` is not valid csh syntax. – Martin Tournoij May 03 '17 at 21:07