Haa*_*ya 3 scripting hp-ux shell-script environment-variables
我想检查 $Y 中的“错误”或“ORA-”。如果有错误则退出
Y=`sqlplus -s user/passwd<< EOF
exec test_Proc;
exit;
EOF`
if [ echo $Y | awk '/ERROR/ || /ORA-/' ] ; then
exit 1
fi
Run Code Online (Sandbox Code Playgroud)
但这不起作用。
如果您正在运行,bash您可以使用正则表达式匹配来完成:
if [[ $Y =~ (ERROR|ORA-) ]]; then
echo error
fi
Run Code Online (Sandbox Code Playgroud)
或者,如果你坚持,你也可以这样做awk:
if ! printf '%s\n' "$Y" | awk '/ERROR|ORA-/ {exit 1}'; then
echo error
fi
Run Code Online (Sandbox Code Playgroud)
关键是,awk使用正则表达式,而不是 shell 表达式逻辑。
最简单的方法可能是使用grep:
printf '%s\n' "$Y" | egrep -q 'ERROR|ORA-' && echo error
Run Code Online (Sandbox Code Playgroud)