-1

I have done with some conditions but not able to use exact syntax to check more than 3 condition from file.

I am able to do multiple grep from file but not able to add 3 pattern with conditions. like below.

you can provide CASE/Loop/if-else(ladder syntax. I just want print user friendly msg to user when the user runs this script instead pattern which are found in startup.log file. and those user friendly msg should depend on what pattern found in startup.log

Suppose I found pid already exists when I fire above command in startup.log then I want to print echo "DB services already running" like this

pg_ctl -D $PGDATA start > startup.log

if [$? -eq 0]

then #if db services is stopped priviously, then it will start and grep below msg to user ls -t postgresql*.log | head -n1 | args grep "Database for read only connections"

else

elif grep 'word1|word2|word3' startup.log then
#if above word1 exists in file it should print below msg echo "hello" else

#if word2 is present in file it shhould print below msg

     echo " world"

and one more contion i want to add like below

#if word3 is exists in the file it should below msg

echo "postgresql"

If you could provide 1 simple example, I really appreciate for that because, I have tried syntax but not able to resolve the issue.

  • is postgressql*.log a typo or is the extra s deliberate? – cas Jul 18 '21 at 13:39
  • If you're asking about matching some set of lines, show a sample input and sample output, i.e. the stuff you'd want to match. If you're asking about the code of some script, start with fixing your indentation; checking that the code even runs (use e.g. shellcheck.net); and post a full and complete script. You don't want to make people waste time on fixing the basic stuff on your script: that effort will be gone from focusing on your particular issue, and demanding others to fix the basics shows you don't really care about the people you're asking help from. – ilkkachu Jul 18 '21 at 14:27
  • Right now, the script you've shown won't even run, and it has a number of other issues, and that makes it really hard to use to deduce what you're trying to achieve – ilkkachu Jul 18 '21 at 14:31
  • @cas , it was typo. corrected – Adam Mulla Jul 18 '21 at 15:55

2 Answers2

1

From your description of the problem, you want to do different things if different patterns are found in the file. That requires different checks:

if grep -q word1 startup.log; then
echo "Message 1"
elif grep -q word2 startup.log; then
echo "Message 2"
elif grep -q word3 startup.log; then
echo "Message 3"
else
echo "Message 4"
fi

grep -q silently checks a file for matches. For each pattern that is matched, you can add a corresponding message to be displayed.

Note that only one message will be displayed with the above logic. If multiple patterns are present in the file, the earlier one specified in the if-elif chain takes precedence.

If you want to check each pattern independently, you can use separate if blocks:

if grep -q word1 startup.log; then
echo "Message 1"
fi

if grep -q word2 startup.log; then echo "Message 2" fi

Haxiel
  • 8,361
  • Thank you for your ans. It was helpful.. I want to know, how I can grep string from file which is single quoted along with non single quotes? like below
    Lock file "postmaster.pid" already exists I want to fetch hole string not only postmaster.pid
    – Adam Mulla Jul 18 '21 at 16:30
  • @AdamMulla You need to escape the quotes appropriately. You can see this QA for details: https://unix.stackexchange.com/q/30903/173368 – Haxiel Jul 18 '21 at 17:24
1

You didn't say what you wanted to do if more than one of the patterns was found in the file, or if multiple instances of a pattern were found. The following shell code has been written as a loop to handle any/all of them if found.

for word in $(grep -oE "word1|word2|word3" startup.log ); do
  case "$word" in
    word1) echo "hello" ;;
    word2) echo "world" ;;
    word3) echo "postgresql" ;;
  esac
done

grep options:

  • -E use extended regular expressions (so I can use word1|word2 instead of word1\|word2)
  • -o output only the matching part of the line (i.e. just word1, word2, or word3), instead of the entire line
cas
  • 78,579