虽然文件不包含字符串 BASH

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42377739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:48:25  来源:igfitidea点击:

While file doesn't contain string BASH

bashwhile-loop

提问by Jacob Collins

I am making a script for my school, and I was wondering how I could check a file, and if the string isn't in the file, do a code, but if it is, continue, like this:

我正在为我的学校制作脚本,我想知道如何检查文件,如果该字符串不在文件中,请执行代码,但如果是,请继续,如下所示:

while [ -z $(cat File.txt | grep "string" ) ] #Checking if file doesn't contain string
do
    echo "No matching string!, trying again" #If it doesn't, run this code
done
echo "String matched!" #If it does, run this code

回答by George Vasiliou

You can do something like:

您可以执行以下操作:

$ if grep "string" file;then echo "found";else echo "not found"

To do a loop :

做一个循环:

$ while ! grep "no" file;do echo "not found";sleep 2;done
$ echo "found"

But be carefull not to enter an infinite loop. string or file must be changed otherwise the loop has no meaning.

但注意不要进入无限循环。必须更改字符串或文件,否则循环没有意义。

Above if/while works based on the return status of the command and not on the result. if grep finds string in file will return 0 = success = true if grep does not find string will return 1 = not success = false

上面的 if/while 是基于命令的返回状态而不是结果来工作的。如果 grep 在文件中找到字符串将返回 0 = 成功 = true 如果 grep 未找到字符串将返回 1 = 不成功 = 假

By using ! we revert the "false" to "true" to keep the loop running since while loops on something as soon as it is true.

通过使用 !我们将“假”恢复为“真”以保持循环运行,因为一旦它为真,while 就会在某些东西上循环。

A more conventional while loop would be similar to your code but without the useless use of cat and the extra pipe:

更传统的 while 循环与您的代码类似,但没有无用的使用 cat 和额外的管道:

$ while [ -z $(grep "no" a.txt) ];do echo "not found";sleep 2;done
$ echo "found"

回答by Roald Nefs

A simple if statement to test if a 'string' is not in file.txt:

一个简单的 if 语句来测试“字符串”是否不在file.txt

#!/bin/bash
if ! grep -q string file.txt; then
    echo "string not found in file!"
else
    echo "string found in file!"
fi

The -qoption (--quiet, --silent) ensures output is not written to standard output.

-q选项(--quiet--silent)可确保输出不被写入到标准输出。

A simple while loop to test is a 'string' is not in file.txt:

要测试的简单 while 循环是“字符串”不在file.txt

#!/bin/bash
while ! grep -q string file.txt; do
    echo "string not found in file!"
done
echo "string found in file!"

Note:be aware of the possibility that the while loop may cause a infinite loop!

注意:注意while循环可能导致无限循环的可能性!

回答by JayRugMan

Another simple way is to just do the following:

另一种简单的方法是执行以下操作:

[[ -z $(grep string file.file) ]] && echo "not found" || echo "found"

&&means AND - or execute the following command if the previous is true

&&表示 AND - 如果前一个为真,则执行以下命令

||means OR - or execute if the previous is false

||表示或 - 或如果前一个为假则执行

[[ -z $(expansion) ]]means return trueif the expansion output is null

[[ -z $(expansion) ]]表示如果扩展输出为则返回

This line is much like a double negative, basically: "return trueif the string is not foundin file.file; then echo not foundif true, or foundif false"

这一行很像双重否定,基本上:“如果在 file.file 中找不到字符串,则返回true;如果为true,则回显未找到,如果为false,则找到

Example:

例子:

bashPrompt:$ [[ -z $(grep stackOverflow scsi_reservations.sh) ]] && echo "not found" || echo "found"
not found
bashPrompt:$ [[ -z $(grep reservations scsi_reservations.sh) ]] && echo "not found" || echo "found"
found