如何比較 ksh 中的字串

如何比較 ksh 中的字串

我想檢查作業的結果並在 上執行操作FAILED

首先:我 grep 應用程式日誌檔案中該行的最後一個單字(對於最近處理的檔案 ( $processedfilename)):

check1=$(grep "$processedfilename" "$logfile" | grep "anotherword" | \
    grep "FAILED" | tail -1 | awk '{print $NF}')

這導致[FAILED].

現在我想檢查結果

if [ $check1 -eq "[[FAILED]" ] 
then

或者

if [ $check1 -eq "\[FAILED]" ] 
then

總有算術語法錯誤

檢查的正確文法是什麼[FAILED]

答案1

您應該始終用雙引號引用變數。並且您需要=字串等於。所以:

if [ "$check1" = "[FAILED]" ]; then

答案2

您正在透過使用進行算術比較-eq導致錯誤,您需要透過使用=(或==內部[[)進行字串比較,為此使用引號就足夠了:

[ "$check1" = "[[FAILED]" ]
[[ "$check1" = "[[FAILED]" ]]

相關內容