簡體   English   中英

如何在Shell腳本中測試ant命令的輸出?

[英]How can I test the output of ant command in a Shell script?

我需要測試“ ant start”命令的輸出是“ Build success”還是“ Build Fail”

我的代碼是:

# Start the App
sleep 20
ant -f start
if [ ! $? = 0 ] || [ "Here I have to test if ant start command output was Build Success or Build Failure" ] ; then
    echo "*** Failed to start" 
        Exit 2
fi

你可以做:

if ant -f start
then
    echo "*** Build started" 
else
    echo "*** Failed to start" 
    Exit 2
fi

要不然:

ant -f start
if [ $? -eq 0 ]
then
    echo "*** Build started" 
else
    echo "*** Failed to start" 
    Exit 2
fi
ant -f start
antRet=$?


if [ $antRet -ne 0 ];then
    echo "*** Failed" 
    exit 1;
else
     echo "*** Build started"    
    exit 0;
fi

兼容POSIX:

# Start the App
sleep 20
cmd_out=$(ant -f start)
if [ $? -ne 0 ] || echo "$cmd_out" | grep -q "Build Fail"; then
    echo "*** Failed to start" 
    exit 2
fi

如果您使用的是bash ,則輸出檢查更為簡單:

sleep 20
cmd_out=$(ant -f start)
if [[ $? != 0 || $cmd_out = Build\ Fail ]]; then
    echo "*** Failed to start" 
    exit 2
fi

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM