shell 在Bash脚本中处理isql登录失败

tvokkenx  于 2023-03-30  发布在  Shell
关注(0)|答案(1)|浏览(108)

我想在我的shell脚本中捕获登录失败
代码如下所示

retval=`isql -S$envServer -U$GCMS_USERNAME -h -b << ENDSQL | grep -vE "^Password: $"
$GCMS_PASSWORD
set nocount on
GO
USE "mydb"
GO
USE "SELECT tname from MYTABLE"
GO
ENDSQL`
echo retval

所以如果登录失败,我得到Msg 4002, Level 14, State 1: Server 'MYSERVER': Login failed. CT-LIBRARY error: ct_connect(): protocol specific layer: external error: The attempt to connect to the server failed.
我想捕捉这个,所以当这个错误来的时候是做退出,如果它是成功的,我可以执行我的其他操作。

s2j5cfk0

s2j5cfk01#

Msg字符串的快速测试:

if [[ "${retval}" =~ "Msg" ]]
then
    # do stuff with error message (eg display to console, write to log file, etc)
    exit
fi

如果来自MYTABLE的数据可能包含字符串Msg,则可以将测试扩展为一个模式,该模式足够唯一,可以仅匹配错误:

regex='Msg.*Level.*State'

if [[ "${retval}" =~ ${regex} ]]                # do *NOT* wrap ${regex} is double quotes otherwise the quotes will be processed literally
then
    # do stuff with error message (eg display to console, write to log file, etc)
    exit
fi

相关问题