簡體   English   中英

if 在 shell 腳本中找不到令牌時的條件

[英]if condition for when a token is not found in a shell script

我正在嘗試構建一個 if-else 塊,其中一個條件是如果在文本文件上運行 grep 命令時找不到指定的標記,則回顯一條消息。

grep 命令是

grep -i -n "token" file | cut -d':' -f 1

如果找到令牌,它將像往常一樣返回行號。 我想知道如何解釋文本文件中不存在令牌並且終端在執行命令時不輸出任何內容的情況。

IE

if []
  then
    echo "This token does not exist in the file"
  fi

我希望這就是你所需要的:

result=$(grep -i -n "token" file | cut -d':' -f 1)
if [[ -z "$result" ]]; then
    echo "Not found"
else
    echo "$result"
fi

暫無
暫無

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

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