Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if string exists in file with Bash?

Tags:

string

file

bash

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.

like image 943
Toren Avatar asked Jan 20 '11 15:01

Toren


People also ask

How do you check if a file contains a string in Linux?

The proper way to do that is grep 'SomeString' "$File" .

How do I test a string in Bash?

When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = operator with the test [ command. Use the == operator with the [[ command for pattern matching.


1 Answers

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 
like image 148
Thomas Avatar answered Sep 28 '22 14:09

Thomas