如何使用 Bash 测试文件中是否存在字符串? [英] How to test if string exists in file with Bash?

查看:37
本文介绍了如何使用 Bash 测试文件中是否存在字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含目录名称的文件:

my_list.txt :

/tmp/var/tmp

如果该名称已存在于文件中,我想在添加目录名称之前先检查 Bash.

解决方案

grep -Fxq "$FILENAME";my_list.txt

如果找到名称,退出状态为 0(真),否则为 1(假),因此:

if grep -Fxq "$FILENAME";my_list.txt然后# 如果找到代码别的#如果没有找到代码菲

说明

以下是grep 手册页的相关部分:

<块引用>

grep [选项] 模式 [文件...]

-F, --fixed-strings

将 PATTERN 解释为以换行符分隔的固定字符串列表,其中任何一个都将被匹配.

-x, --line-regexp

只选择与整行完全匹配的那些匹配项.

-q, --quiet, --silent

安静;不要向标准输出写入任何内容.如果找到任何匹配项,则立即以零状态退出,即使检测到错误也是如此.另请参阅 -s--no-messages 选项.

错误处理

正如评论中正确指出的那样,上述方法会默默地将错误情况视为找到了字符串.如果您想以不同的方式处理错误,则必须省略 -q 选项,并根据退出状态检测错误:

<块引用>

通常,如果找到选定的行,退出状态为 0,否则为 1.但如果发生错误,退出状态为 2,除非使用了 -q--quiet--silent 选项并选择了线找到了.但是请注意,POSIX 仅要求对于 grepcmpdiff 等程序,出错时的退出状态为大于 1;因此,为了可移植性,建议使用测试此一般条件的逻辑,而不是与 2 严格相等.

要抑制 grep 的正常输出,您可以将其重定向到 /dev/null.请注意,标准错误仍然是未定向的,因此 grep 可能打印的任何错误消息都将按照您的需要最终显示在控制台上.

为了处理这三种情况,我们可以使用case语句:

case `grep -Fx "$FILENAME";$LIST">/dev/null;回声 $?` 在0)# 如果找到代码;;1)#如果没有找到代码;;*)# 发生错误时的代码;;esac

I have a file that contains directory names:

my_list.txt :

/tmp
/var/tmp

I'd like to check in Bash before I'll add a directory name if that name already exists in the file.

解决方案

grep -Fxq "$FILENAME" my_list.txt

The exit status is 0 (true) if the name was found, 1 (false) if not, so:

if grep -Fxq "$FILENAME" my_list.txt
then
    # code if found
else
    # code if not found
fi

Explanation

Here are the relevant sections of the man page for grep:

grep [options] PATTERN [FILE...]

-F, --fixed-strings

        Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.

-x, --line-regexp

        Select only those matches that exactly match the whole line.

-q, --quiet, --silent

        Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.

Error handling

As rightfully pointed out in the comments, the above approach silently treats error cases as if the string was found. If you want to handle errors in a different way, you'll have to omit the -q option, and detect errors based on the exit status:

Normally, the exit status is 0 if selected lines are found and 1 otherwise. But the exit status is 2 if an error occurred, unless the -q or --quiet or --silent option is used and a selected line is found. Note, however, that POSIX only mandates, for programs such as grep, cmp, and diff, that the exit status in case of error be greater than 1; it is therefore advisable, for the sake of portability, to use logic that tests for this general condition instead of strict equality with 2.

To suppress the normal output from grep, you can redirect it to /dev/null. Note that standard error remains undirected, so any error messages that grep might print will end up on the console as you'd probably want.

To handle the three cases, we can use a case statement:

case `grep -Fx "$FILENAME" "$LIST" >/dev/null; echo $?` in
  0)
    # code if found
    ;;
  1)
    # code if not found
    ;;
  *)
    # code if an error occurred
    ;;
esac

这篇关于如何使用 Bash 测试文件中是否存在字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆