LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script again (https://www.linuxquestions.org/questions/linux-newbie-8/script-again-4175510150/)

apss_evaluator 07-04-2014 03:56 PM

script again
 
Hi all,

Just want to ask what is wrong with the script below, so I just want to know if "TIMEOUT" word is present on pogi.log, if the keyword exist then it will print "NOT OK" if not it will print "OK"


#!/bin/bash
log_file='/tmp/pogi.log'
if [ ! grep -i "TIMEOUT" $log_file ]; then
echo "OK"
else
echo "NOT OK!"
fi


I tried to manipulate the log file by removing the "TIMEOUT" keyword but it still gives me this result:

./timeout.sh: line 3: [: too many arguments

NOT OK!



thanks in advance and happy 4rth of July

ntubski 07-04-2014 04:10 PM

You should call grep not [ (aka test):

Code:

#!/bin/bash
log_file='/tmp/pogi.log'
if ! grep -i "TIMEOUT" $log_file; then
  echo "OK"
else
  echo "NOT OK!"
fi


apss_evaluator 07-04-2014 05:48 PM

Quote:

Originally Posted by ntubski (Post 5198782)
You should call grep not [ (aka test):

Code:

#!/bin/bash
log_file='/tmp/pogi.log'
if ! grep -i "TIMEOUT" $log_file; then
  echo "OK"
else
  echo "NOT OK!"
fi


the code is working good but I need to see only on the screen "OK" or "NOT OK!"

when the script detected "TIMEOUT" on the log, the "TIMEOUT" keywords from the pogi.log appears on the screen along with the echoed "NOT OK!"


[pogi@test tmp]$ ./ntubski.sh
app1.ear deployment in progress .............................. [FAIL (TIMEOUT)]
app2.ear deployment in progress .............................. [FAIL (TIMEOUT)]
app3.war deployment in progress .............................. [FAIL (TIMEOUT)]
NOT OK!


when script did not detected the keyword "TIMEOUT" (outcome is fine as expected)

[pogi@test tmp]$ ./ntubski.sh
OK

ntubski 07-04-2014 05:59 PM

pass option -q to grep to suppress output.

Code:

#!/bin/bash
log_file='/tmp/pogi.log'
# also added -F which means plain string, not a regexp.
# and quote variables (doesn't matter in this case, but it's a good habit)
if ! grep -Fqi "TIMEOUT" "$log_file"; then
  echo "OK"
else
  echo "NOT OK!"
fi


unSpawn 07-04-2014 07:11 PM

...bit shorter and if the log file is kinda long you can bailout after one "TIMEOUT" ("-m1"):
Code:

LOGFILE="/tmp/pogi.log"; grep -Fqi -m1 "TIMEOUT" "${LOGFILE}" && RES="OK"; echo "${RES:="NOT OK"}"

ntubski 07-05-2014 08:06 AM

Quote:

Originally Posted by unSpawn (Post 5198828)
you can bailout after one "TIMEOUT" ("-m1")

Actually, this is covered by -q:
Quote:

-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 .)


apss_evaluator 07-05-2014 11:21 AM

Quote:

Originally Posted by ntubski (Post 5199055)
Actually, this is covered by -q:

thanks!, your suggestion really helps, one more thing how can I apply this --> if ! grep -Fqi "TIMEOUT" "$log_file"
by adding on the test commands below.

#!/bin/bash
log_file='/tmp/pogi.log'
if [ -e ! /tmp/pg_pid.txt ] && [ -e ! /tmp/jboss7_pid.txt ] && [ ! -f $JBOSS_DEPLOY_LOCATION/*.failed ]; then
echo "OK"
else
echo "NOT OK!"
fi

ntubski 07-06-2014 10:19 AM

Quote:

Originally Posted by apss_evaluator (Post 5199114)
thanks!, your suggestion really helps, one more thing how can I apply this --> if
by adding on the test commands below.

Code:

#!/bin/bash
log_file='/tmp/pogi.log'
# [ ! -e filename ] is okay too
if ! [ -e /tmp/pg_pid.txt ] && ! [ -e /tmp/jboss7_pid.txt ] && ! [ -f $JBOSS_DEPLOY_LOCATION/*.failed ] && ! grep -Fqi "TIMEOUT" "$log_file"; then
  echo "OK"
else
  echo "NOT OK!"
fi

At this point you maybe want apply De Morgan's Law to get rid of the "!":

Code:

#!/bin/bash
log_file='/tmp/pogi.log'
if [ -e /tmp/pg_pid.txt ] || [ -e /tmp/jboss7_pid.txt ] || [ -f $JBOSS_DEPLOY_LOCATION/*.failed ] || grep -Fqi "TIMEOUT" "$log_file"; then
  echo "NOT OK!"
else
  echo "OK"
fi


apss_evaluator 07-31-2014 03:11 PM

Quote:

Originally Posted by ntubski (Post 5199510)
Code:

#!/bin/bash
log_file='/tmp/pogi.log'
# [ ! -e filename ] is okay too
if ! [ -e /tmp/pg_pid.txt ] && ! [ -e /tmp/jboss7_pid.txt ] && ! [ -f $JBOSS_DEPLOY_LOCATION/*.failed ] && ! grep -Fqi "TIMEOUT" "$log_file"; then
  echo "OK"
else
  echo "NOT OK!"
fi

At this point you maybe want apply De Morgan's Law to get rid of the "!":

Code:

#!/bin/bash
log_file='/tmp/pogi.log'
if [ -e /tmp/pg_pid.txt ] || [ -e /tmp/jboss7_pid.txt ] || [ -f $JBOSS_DEPLOY_LOCATION/*.failed ] || grep -Fqi "TIMEOUT" "$log_file"; then
  echo "NOT OK!"
else
  echo "OK"
fi





Hi ntubski, I'm trying to find a way on what is the better approach on this:
grep the server.log file and check if 'deployment failed' exists

would this be okay to add in the above condition?
[ ! grep -i 'deployment failed' server.log ]


so if the check finds a 'deployment failed' it would report not okay

Firerat 07-31-2014 03:17 PM

Code:

grep -iq "deployment failed" serverlog || echo "true" && echo "false"

apss_evaluator 07-31-2014 03:43 PM

Quote:

Originally Posted by Firerat (Post 5212709)
Code:

grep -iq "deployment failed" serverlog || echo "true" && echo "false"

thanks Firerat! that was quick, I'll try this on


All times are GMT -5. The time now is 02:20 AM.