gpt4 book ai didi

shell - 使用 ! shell 命令上的运算符

转载 作者:行者123 更新时间:2023-12-01 06:54:42 24 4
gpt4 key购买 nike

我正在尝试编写一个脚本,该脚本在文件中不存在特定字符串时执行某些操作。

我知道为了检查字符串是否可用,我们可以这样做:

if grep -qi "sms" $FILE; then

但是如何将 ! 运算符与 shell 命令结合起来呢?

最佳答案

只要把它加在你的条件前面,让它求反:

if ! grep -qi "sms" $FILE; then echo "yes"; fi
^

测试

$ cat a
hello
bye
$ if ! grep -qi "sms" a; then echo "sms not found"; fi
sms not found

关于shell - 使用 ! shell 命令上的运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27020188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com