LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 06-04-2014, 06:33 PM   #1
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Rep: Reputation: Disabled
grep script help


I want to keep running a command until the word up is found in the file "status" but no more than a maximum of 10 times.

How to do this? Something like

until grep "up" status.txt
do
my command
sleep 2
done

?

thank you
 
Old 06-04-2014, 06:54 PM   #2
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 490Reputation: 490Reputation: 490Reputation: 490Reputation: 490
I would use a for loop 10 times, and break out as soon as you find it (if grep; then break; ).
 
Old 06-05-2014, 06:26 AM   #3
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thank you, I had a look on t'inernet and have come up with this, is it correct?


Code:
for i in {1..10}
do
grep "up" /opt/tmp/vpnclientstatus
if [ $? -eq 0 ] ; then
	break
fi
	run startvpn.sh
done
 
Old 06-05-2014, 07:20 AM   #4
TommyC7
Member
 
Registered: Mar 2012
Distribution: Slackware, CentOS, OpenBSD, FreeBSD
Posts: 454

Rep: Reputation: Disabled
I feel I should mention that your first script will go on forever until "up" is matched.

Anyways, yes your new script will work (although I don't think you need or can use the "run" unless you have something in your $PATH with that name).

What metaschima stated is that your script can be shortened as such:

Code:
# bash and ksh
for i in {1..10}; do
  if grep "up" /opt/tmp/vpnclientstatus; then
    break
  else
    run startvpn.sh
  fi
done
You don't really need the "else", but I put it in for a bit of clarity.

Since "$?" holds the return code of the last executed command (in your case the grep) your version will still work.

Also, if you do choose to use [ and you have access to bash I recommend using [[ for extra functionality and a bit more usefulness in certain scenarios.

Last edited by TommyC7; 06-05-2014 at 07:22 AM.
 
Old 06-05-2014, 07:51 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
so status.txt is growing? (If I understood it well). Try the following:
tail -f status.txt | awk ' /up/ { a++ } a>10 { exit }' && run startvpn.sh
 
Old 06-05-2014, 07:55 AM   #6
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
Excellent, thank you but you have lost me with the [[ is that meaning extra square brackets around the if?

Also I have been tinkering and would like to log the result, can I replace break with exit so that the script will exit after logging the vpn as up and also log if it has not come up after 10 tries as below. Presumably the break command will just stop the loop and run the rest of this script including the last line

Code:
for i in {1..10}; do
	if grep "up" /opt/tmp/vpnclientstatus; then
	logger -t "($(basename $0))" $$ VPNbook restablished " $0${*:+ $*$
		exit
	else
		service start_vpnclient1
		sleep 10
	fi
done
logger -t "($(basename $0))" $$ VPNbook NOT restablished " $0${*:+ $*$
 
Old 06-05-2014, 07:59 AM   #7
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
so status.txt is growing? (If I understood it well). Try the following:
tail -f status.txt | awk ' /up/ { a++ } a>10 { exit }' && run startvpn.sh
The status file just has one word in it at all times being replaced depending on vpn up or down two other scripts with "echo up/down > vpnclientstatus"
 
Old 06-05-2014, 08:05 AM   #8
TommyC7
Member
 
Registered: Mar 2012
Distribution: Slackware, CentOS, OpenBSD, FreeBSD
Posts: 454

Rep: Reputation: Disabled
Quote:
bilbonix:
Excellent, thank you but you have lost me with the [[ is that meaning extra square brackets around the if?
Yes. It's a bash-ism (that is, it's only for bash) in contrast to [ which is a command on the system.

Yes you can prefer to exit rather than break if you wish. You seem to know what it will do already.

Also, instead of using basename, I would recommend using parameter expansions (they're faster and 99% of the time, do the same thing as dirname and basename, the 1% are a few exceptions when the variables are weird).

As for the rest of the line, you have an unterminated quote, and I'm not quite sure what you're trying to do with this:
Code:
${*:+ $*$
 
Old 06-05-2014, 08:17 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
I still do not understand it. The code in post #6 will not count the "up"s, but will try to start vpn ten times (if "up" was not found). And also at the tenth try it will finish the for loop and report "VPNbook NOT restablished " without checking the result.
 
Old 06-05-2014, 08:24 AM   #10
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
My copy paste skills fail me, should be

logger -t "($(basename $0))" $$ VPNbook restablished " $0${*:+ $*}."

which will log something like this in syslog:

Jun 5 13:18:54 (vpn.sh): 1378 VPNbook restablished vpn.sh.
 
Old 06-05-2014, 08:30 AM   #11
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
I still do not understand it. The code in post #6 will not count the "up"s, but will try to start vpn ten times (if "up" was not found). And also at the tenth try it will finish the for loop and report "VPNbook NOT restablished " without checking the result.
If the vpn drops the status file is overwritten with only the word "down" and vice versa. I see what you mean about the last loop, mmmm.
 
Old 06-05-2014, 10:01 AM   #12
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
Revised, is this okay:

Code:
for i in {1..10}; do
		service start_vpnclient1
		sleep 10
	if grep "up" /opt/tmp/vpnclientstatus; then
		logger -t "($(basename $0))" $$ VPNbook restablished " $0${*:+ $*}."
		exit
	fi
done
logger -t "($(basename $0))" $$ VPNbook NOT restablished " $0${*:+ $*}."
 
Old 06-05-2014, 10:06 AM   #13
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
that will try start vpn ten times and will exit if it was successful (first time)....
 
Old 06-05-2014, 10:28 AM   #14
bilbonix
LQ Newbie
 
Registered: Nov 2012
Posts: 21

Original Poster
Rep: Reputation: Disabled
so if the vpn does not start until say the 5th attempt, the script will exit after the 5th attempt and not try to start the vpn again?

If the vpn fails to start after 10 attempts "VPNbook NOT restablishe" will be logged and the script will exit?

Many thanks.
 
Old 06-05-2014, 10:33 AM   #15
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
if vpn starts the script will exit. It will try to start it 10 times.
If "up" is found (only once) the script will not continue but exit.
 
  


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
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 03:07 PM
[bash-script] A question about using grep in the script thomas2004ch Linux - Software 2 03-05-2012 04:27 AM
Using grep for a script iongrey Programming 3 03-24-2009 08:55 PM
grep script kaiserbeto Linux - Newbie 2 11-15-2006 02:50 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM


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