出口 1 没有消息?
No message on exit 1?
#!/bin/sh
set -e
export DATABASE=U414_157_6300/U414_157_6300@db1230cn:1526/DBG192
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
export JAVA_HOME=/u01/app/java/jdk180_231
export ORACLE_HOME=/u01/app/oracle/client19
export LD_LIBRARY_PATH=/u01/app/oracle/client19/lib
export PATH=$ORACLE_HOME/bin:$PATH
PATH=$PATH:$HOME/bin
export PATH
echo "exit" |sqlplus -L $DATABASE | grep 'Connected' > /dev/null
if [ $? -eq 0 ]
then
echo "Database is up and the connection is successful!!"
exit 0;
else
echo "Database seems to be down!!"
exit 1;
fi
此脚本用于检查数据库是启动还是关闭。启动时,我得到以下结果。
./db_status.sh
Database is up and the connection is successful!!
但是当数据库关闭时我什么也得不到!!为什么??
./db_status.sh
[root@qaapp828cn u414_157_6300]#
因为脚本开头有 set -e
。如果任何命令具有 non-zero 退出代码,这会使脚本立即退出。因此,当 grep
失败时,脚本会立即退出,您永远不会在下一行测试 $?
。
但也有例外:如果命令在 if
中用作测试,则脚本不会退出。因此,与其测试 $?
,不如测试命令本身。
if echo "exit" |sqlplus -L $DATABASE | grep -q 'Connected'
then
echo "Database is up and the connection is successful!!"
exit 0;
else
echo "Database seems to be down!!"
exit 1;
fi
#!/bin/sh
set -e
export DATABASE=U414_157_6300/U414_157_6300@db1230cn:1526/DBG192
export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
export JAVA_HOME=/u01/app/java/jdk180_231
export ORACLE_HOME=/u01/app/oracle/client19
export LD_LIBRARY_PATH=/u01/app/oracle/client19/lib
export PATH=$ORACLE_HOME/bin:$PATH
PATH=$PATH:$HOME/bin
export PATH
echo "exit" |sqlplus -L $DATABASE | grep 'Connected' > /dev/null
if [ $? -eq 0 ]
then
echo "Database is up and the connection is successful!!"
exit 0;
else
echo "Database seems to be down!!"
exit 1;
fi
此脚本用于检查数据库是启动还是关闭。启动时,我得到以下结果。
./db_status.sh
Database is up and the connection is successful!!
但是当数据库关闭时我什么也得不到!!为什么??
./db_status.sh
[root@qaapp828cn u414_157_6300]#
因为脚本开头有 set -e
。如果任何命令具有 non-zero 退出代码,这会使脚本立即退出。因此,当 grep
失败时,脚本会立即退出,您永远不会在下一行测试 $?
。
但也有例外:如果命令在 if
中用作测试,则脚本不会退出。因此,与其测试 $?
,不如测试命令本身。
if echo "exit" |sqlplus -L $DATABASE | grep -q 'Connected'
then
echo "Database is up and the connection is successful!!"
exit 0;
else
echo "Database seems to be down!!"
exit 1;
fi