match the set of words from file using shell script
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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" ] -a [ "$oam_output2" != "Board is started" ] do if [ "$oam_output1" != "OAMEVN_STARTBOARD_DONE Finished" ] -a [ "$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.
ss7_load () { ss7_fini="ISUP General configuration successful|ISUP NSAP configuration successful" ss7_exe=$(which ss7load) cd /var/log || exit ss7_output=$(grep "$ss7_fini" ss7load.log) if [ -z "$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
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.