如何testingstring存在与Bash shell的文件?

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

my_list.txt

 /tmp /var/tmp 

如果该名称已经存在于文件中,我想在Bash之前添加一个目录名称。

 grep -Fxq "$FILENAME" my_list.txt 

如果find名字,退出状态为0(真),否则为1(假),所以:

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

这里是grep手册页的相关部分:

 grep [options] PATTERN [FILE...] -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by new- lines, 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 immedi- ately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option. 

关于以下解决scheme:

 grep -Fxq "$FILENAME" my_list.txt 

如果你想知道(和我一样)什么-Fxq意味着简单的英语F影响如何解释PATTERN(固定string,而不是正则expression式), x匹配整行, q shhhhh …最小的打印

从man文件中:

 -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) -x, --line-regexp Select only those matches that exactly match the whole line. (-x is specified by POSIX.) -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. (-q is specified by POSIX.) 

我脑海中的三种方法:

1)短路testing名称(我不知道这可能是你的情况)

 ls -a "path" | grep "name" 

2)对文件中的string进行简短的testing

 grep -R "string" "filepath" 

3)使用正则expression式更长的bash脚本:

 #!/bin/bash declare file="content.txt" declare regex="\s+string\s+" declare file_content=$( cat "${file}" ) if [[ " $file_content " =~ $regex ]] # please note the space before and after the file content then echo "found" else echo "not found" fi exit 

这应该是更快,如果你必须使用循环testing多个string文件内容,例如在任何cicle更改正则expression式。

更简单的方法:

 if grep "$filename" my_list.txt > /dev/null then ... found else ... not found fi 

提示:如果您想要命令的退出状态,而不是输出,则发送到/dev/null

如果我正确理解你的问题,这应该做你所需要的。

  1. 你可以指定你想通过$ checkvariables添加的目录
  2. 如果该目录已经在列表中,则输出是“dir already listed”
  3. 如果该目录还没有在列表中,它会被追加到my_list.txt

在一行中check="/tmp/newdirectory"; [[ -n $(grep "^$check\$" my_list.txt) ]] && echo "dir already listed" || echo "$check" >> my_list.txt check="/tmp/newdirectory"; [[ -n $(grep "^$check\$" my_list.txt) ]] && echo "dir already listed" || echo "$check" >> my_list.txt

如果你只是想检查一行的存在,你不需要创build一个文件。 例如,

 if grep -xq "LINE_TO_BE_MATCHED" FILE_TO_LOOK_IN ; then # code for if it exists else # code for if it does not exist fi 

我的版本使用fgrep

  FOUND=`fgrep -c "FOUND" $VALIDATION_FILE` if [ $FOUND -eq 0 ]; then echo "Not able to find" else echo "able to find" fi 

最简单最简单的方法是:

 isInFile=$(cat file.txt | grep -c "string") if [ $isInFile -eq 0 ]; then #string not contained in file else #string is in file at least once fi 

grep -c将返回文件中string出现的次数。

 grep -E "(string)" /path/to/file || echo "no match found" 

-E选项使grep使用正则expression式

一个没有问题的解决scheme,适用于我:

 MY_LIST=$( cat /path/to/my_list.txt ) if [[ "${MY_LIST}" == *"${NEW_DIRECTORY_NAME}"* ]]; then echo "It's there!" else echo "its not there" fi 

基于: https : //stackoverflow.com/a/229606/3306354

 if grep -q "$Filename$" my_list.txt then echo "exist" else echo "not exist" fi