Reputation: 22957
i am running script as root and trying to see if user oracle has ORACLE_HOME defined. here is my script:
su - $user << EOF 2>&1
if [ -n "${ORACLE_HOME:+x}" ]
then
echo "KO" > /tmp/oracle.tmp
else
echo "$ORACLE_HOME" > /tmp/oracle.tmp
fi
EOF
this doesnt work. it gives me if: Expression Syntax.
and the file is not created. i think the problem is with the su
encapsulation since when i am running the if
statement alone it is working. any idea ?
And please don't ask me why i am running as root. i know its bad practice but there is nothing i can do about it.
Upvotes: 0
Views: 1836
Reputation: 212464
The problem is that the quotes are getting stripped by the shell parsing the heredoc, so the code being executed is:
if [ -n ]
To eliminate that problem, you want to avoid string interpolation in the heredoc, which you can do by quoting the delimeter:
su - $user << 'EOF' 2>&1
Of course, that will prevent ORACLE_HOME from being interpolated, so this will only work if ORACLE_HOME is in the environment (that is, it is not a shell variable, but has been exported and is an environment variable.)
Unless the sample code given in the question is a simplification, you could just do:
echo ${ORACLE_HOME:-KO} > /tmp/oracle.tmp
Upvotes: 0
Reputation: 242028
The code is probably not run in bash, but in sh that does not support the alternate value syntax.
Upvotes: 1