简体   繁体   中英

Illegal variable name tcsh shell linux

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.

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"

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"`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM