LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-04-2014, 04:56 PM   #1
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Rep: Reputation: Disabled
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
 
Old 07-04-2014, 05:10 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
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
 
1 members found this post helpful.
Old 07-04-2014, 06:48 PM   #3
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
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
 
Old 07-04-2014, 06:59 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
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
 
1 members found this post helpful.
Old 07-04-2014, 08:11 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531
...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"}"
 
1 members found this post helpful.
Old 07-05-2014, 09:06 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
Quote:
Originally Posted by unSpawn View Post
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 .)
 
1 members found this post helpful.
Old 07-05-2014, 12:21 PM   #7
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
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
 
Old 07-06-2014, 11:19 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
Quote:
Originally Posted by apss_evaluator View Post
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
 
1 members found this post helpful.
Old 07-31-2014, 04:11 PM   #9
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
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
 
Old 07-31-2014, 04:17 PM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Code:
grep -iq "deployment failed" serverlog || echo "true" && echo "false"
 
1 members found this post helpful.
Old 07-31-2014, 04:43 PM   #11
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
Code:
grep -iq "deployment failed" serverlog || echo "true" && echo "false"
thanks Firerat! that was quick, I'll try this on
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 10 07-17-2012 10:36 AM
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 07:00 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 02:27 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 04:05 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM


All times are GMT -5. The time now is 09:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration