在外壳脚本中找不到令牌时的 if 条件



>我正在尝试构造一个if-else块,其中条件之一是在文本文件上运行grep命令时找不到指定的令牌时回显消息。

grep 命令是

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

如果找到令牌,它将像往常一样返回行号。我想知道如何解释当令牌在文本文件中不存在并且终端在执行命令时不输出任何内容的情况。

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

最新更新