LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-05-2009, 03:16 PM   #1
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,272

Rep: Reputation: 148Reputation: 148
match the set of words from file using shell script


I have to match "OAMEVN_STARTBOARD_DONE Finished" and "Board is started", if both line exist on the log file then end the loop, but this script return an error "too many arguments" at line
Quote:
"while [ test $oam_output1 != "OAMEVN_STARTBOARD_DONE Finished" ] && [ test $oam_output2 != "Board is started" ]"
please any body guide my how can i match these strings and let me know if there is any scripting loop hole at this script

PHP Code:
oamsys > /var/log/oamsys.log
cd 
/var/log || exit
oam_output1=`cat oamsys.log | grep "OAMEVN_STARTBOARD_DONE Finished"`
oam_output2=`cat oamsys.log | grep  "Board is started"`
while [ 
test $oam_output1 != "OAMEVN_STARTBOARD_DONE Finished" ] && [ test $oam_output2 != "Board is started" ]
do
  if [ 
test $oam_output1 != "OAMEVN_STARTBOARD_DONE Finished" ] && [ test $oam_output2 != "Board is started" ]
  
then
     
echo $echo_n "Staring board"
     
oamsys > /var/log/oamsys.log
     oam_output1
=`cat oamsys.log | grep "OAMEVN_STARTBOARD_DONE Finished"`
     
oam_output2=`cat oamsys.log | grep  "Board is started"`
  else
     echo 
"Borad started sucessfully"
  
fi
done
sleep 3 
Please any body help to short-out my problem.

Last edited by kirukan; 09-05-2009 at 03:17 PM.
 
Old 09-05-2009, 03:28 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Indeed there are a lot of bugs in your script. Let's try to explain a bit:

1. The square bracket you used in a while or if/then statement is a shell command which compares to the test command: using test inside square brackets is a redundancy and brings to unpredictable results

2. The logical AND in bash tests is -a.

3. When testing for a variable which contains blank spaces, you have to embed it in double quotes, otherwise you trigger the "too many arguments" error, since the shell interprets the different fields of the string as different items inside the test expression.

4. To grep the content of a file you can give the file name as argument to grep itself: no need to cat the file and pipe the output to grep.

Just some sparse idea to help you in debugging.
 
Old 09-05-2009, 04:21 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
An additional suggestion: Rather than creating oam_output1 and oam_output2 consider using
oam_output=$(egrep '(OAMEVN_STARTBOARD_DONE Finished|Board is started)' oamsys.log)
and a simple test like [ -z "$oam_output" ]

Also, just a suggestion, consider putting a sleep 10 inside your while loop so the script doesn't "eat" all your available CPU cycles.
 
Old 09-05-2009, 05:19 PM   #4
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,272

Original Poster
Rep: Reputation: 148Reputation: 148
Thanks a lot "colucix" to gave me some valuable idea.

PHP Code:
oamsys > /var/log/oamsys.log
cd 
/var/log || exit
oam_output1=`grep "OAMEVN_STARTBOARD_DONE Finished" oamsys.log`
oam_output2=`grep  "Board is started" oamsys.log`
#

while [ "$oam_output1!= "OAMEVN_STARTBOARD_DONE Finished" ] -"$oam_output2!= "Board is started" ]
do
  if [ 
"$oam_output1!= "OAMEVN_STARTBOARD_DONE Finished" ] -"$oam_output2!= "Board is started" ]
  
then
     
echo "Staring board"  
     
oamsys > /var/log/oamsys.log
     oam_output1
=`grep "OAMEVN_STARTBOARD_DONE Finished" oamsys.log`
     
oam_output2=`cat oamsys.log | grep  "Board is started" oamsys.log`
  else
     echo 
"Borad started sucessfully"
  
fi
done
sleep 3 
As this scripting order board start successfully and its write the entries in log file after that its check whether the board started successfully or not.
At this time i assume that the board started successfully but after that also the things are executing inside the loop(i mean Starting board message print again and again).
I am pretty sure when i execute this only oamsys > /var/log/oamsys.log its start the board successfully without any issue.

What wrongs at my script?
 
Old 09-05-2009, 07:36 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Try something like this:
PHP Code:
#/bin/bash

# Local variables
out=/var/log/oamsys.log
exe
=$(which oamsys)
limit=10
fini
="OAMEVN_STARTBOARD_DONE Finished|Board is started"
# Sanity checks
[ -"$exe] && echo "The $exe command could not be located." && exit 1
$exe 
$out
[ $? -ne 0 ] && echo $(basename $exefailed. && exit 1
[ -f $out ] || (echo "$(basename $exe) ran, but no output was created.";exit 2)
# Initialize
grep_out=$(egrep -/"$fini""$out2>/dev/null)
# Last sanity check . . .
[ $? -ne 0 ] && echo egrep -/"$fini""$outfailed to run. && exit 4
#
i=1
while [ -"$grep_out]
do
     
i=$((i+1))
     [ 
-gt $limit ] && echo $(basename $exefailed to start in $limit tries. && exit 3
     
echo "Staring board (Attempt $i)"  
     
$exe "$out"
     
grep_out=$(egrep -/"$fini""$out")
     
sleep 3
done
echo Board successfully started 
Warning: The above is untested code. Use at your own risk.
 
Old 09-07-2009, 04:15 AM   #6
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,272

Original Poster
Rep: Reputation: 148Reputation: 148
I have modified the script are as follows

PHP Code:
ss7_load ()
{
ss7_fini="ISUP General configuration successful|ISUP NSAP configuration successful"
ss7_exe=$(which ss7load)
cd /var/log || exit
ss7_output=$(grep "$ss7_finiss7load.log)
if [ -
"$ss7_output
 
then
      $ss7_exe 
> /var/log/ss7load.log
      sleep 10
      ss7_load
 
else
   echo 
"ss7 started already"
fi 
sleep 3

As i have referred from the web for string comparison ( -z string1 -> string1 is NULL and does exist).
In my case if $ss7_output null only then it will enter into the if condition, but it happening even the $ss7_out exist.

how can i compare the strings?
Please anybody guide me in this further

Last edited by kirukan; 09-07-2009 at 04:16 AM.
 
Old 09-07-2009, 05:30 AM   #7
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,272

Original Poster
Rep: Reputation: 148Reputation: 148
The mistake was identified, its because of pipe
Quote:
ss7_fini="ISUP General configuration successful|ISUP NSAP configuration successful"
how can i compare these both lines from log file?

I need to check whether these two lines exist in log file
Quote:
ISUP General configuration successful
ISUP NSAP configuration successful
 
Old 09-07-2009, 05:38 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You have to use egrep, as in the example by PTrenholme, not simply grep. You need to interpret the pattern as an extended regular expression and egrep (alias for grep -E) can accomplish this task.
 
Old 09-07-2009, 07:06 AM   #9
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,272

Original Poster
Rep: Reputation: 148Reputation: 148
Fine. I would like to thank colucix and PTrenholme. I have got valuable idea from both of you. One more thing please, How can i print dots till service start successfully because i have seen kind of things when we start some service.
 
Old 09-07-2009, 07:59 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
The most simple way is to launch a loop in background, do your stuff and finally kill the loop. Note that the special variable $! retrieves the PID of the last process run in background. Here is an example:
Code:
#!/bin/bash
while true
do
  sleep 1
  echo -n ". "
done  &

DOTS_PID=$!

echo -n "work in progress "

#
#  Do your stuff here (sleep 10 just to simulate)
#
sleep 10

kill $DOTS_PID
echo ok
 
Old 09-07-2009, 08:13 AM   #11
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
For that I'd rather take another approach.

Instead of using a recursive function I'd just use a loop. Something like

Code:
while ! grep <whatever>; do
  whatever > logfile
    for i in $(seq 1 20); do
      echo -n .
      sleep 0.5
  done
done
If the "whatever" command doesn't run instantly you can fork it to the background using &, otherwise you are going to get a delay each 20 dots.

Last edited by i92guboj; 09-07-2009 at 08:17 AM. Reason: indentation and last "done"
 
  


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
How do I set an icon for a shell script? M$ISBS Linux - Newbie 3 06-27-2009 09:56 PM
Huge Data Set Analysis, Shell Script to copy specific HEX Pairs into a separate file telecom_is_me Programming 11 06-29-2008 11:48 PM
shell script to find an word or words from a line rakesh.tandur Linux - General 5 05-13-2008 02:57 PM
Shell script - return boolean if match found apairudin Programming 4 08-06-2007 08:07 AM
Procmail: match pattern then pass to shell script essdeeay Linux - Software 1 11-08-2004 03:19 PM


All times are GMT -5. The time now is 08:32 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