Skip to content
Joel Shaikin edited this page Jan 25, 2017 · 2 revisions
FOUND=`fgrep -c "FOUND" $VALIDATION_FILE`
  if [ $FOUND -eq 0 ]; then
   echo "Not able to find"
  else
   echo "able to find"
  fi
if grep -q "$Filename$" my_list.txt
   then
     echo "exist"
else
     echo "not exist"
fi
grep -E "(string)" /path/to/file || echo "no match found"
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
check="/tmp/newdirectory"; [[ -n $(grep "^$check\$" my_list.txt) ]] && echo "dir already listed" || echo "$check" >>  my_list.txt

Longer bash script using regex:

#!/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
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

Clone this wiki locally