检查 shell 脚本中的 $ORACLE_HOME var
我正在以 root 身份运行脚本并尝试查看用户 oracle 是否已定义 ORACLE_HOME。这是我的脚本:
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
这不起作用。它给了我 if: Expression Syntax.
并且该文件未创建。我认为问题出在 su
封装上,因为当我单独运行 if
语句时,它正在工作。有什么想法吗?
请不要问我为什么要以 root 身份运行。我知道这是不好的做法,但我对此无能为力。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该代码可能不在 bash 中运行,而是在不支持备用值语法的 sh 中运行。
The code is probably not run in bash, but in sh that does not support the alternate value syntax.
问题是引号被解析定界符的 shell 去除,因此正在执行的代码是:
为了消除该问题,您希望避免定界符中的字符串插值,您可以通过引用定界符来做到这一点:
当然,这将阻止 ORACLE_HOME 被插值,因此只有当 ORACLE_HOME 位于环境中时才有效(即,它不是 shell 变量,但已导出并且是环境变量。)
除非给出示例代码问题是一个简化,你可以这样做:
The problem is that the quotes are getting stripped by the shell parsing the heredoc, so the code being executed is:
To eliminate that problem, you want to avoid string interpolation in the heredoc, which you can do by quoting the delimeter:
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: