LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-12-2005, 10:05 PM   #1
tara
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58

Rep: Reputation: 15
looking for string on particular line - bash script


Hi

I'm trying to write a script that tests if the script itself contains a particular line.

This script contains the line 'reboot', but sometimes it contains '#reboot' when the reboot command is not required.

This is the code

#!/bin/sh
SUCCESS=1

grep ^#reboot$ program_running.sh
echo "Grep of #reboot returned a :"
echo $?
if [ $? -ne "$SUCCESS" ]
then
#reboot
else
echo "Reboot is commented out"
fi



Ever time i run the script it always return 0, even when the #reboot is not even in the script. Is this because it is picking up the #reboot in the grep command?? Or have i got it completely wrong

Is there a way to only look at a certain line in the script so that way i could just look at the line #reboot is on and not the whole script and thus avoid picking up the #reboot in the grep.

Is a return value of a 0 for a grep command mean that the value was found or was not found? i find the grep manpage hard to read

sorry i'm not a very good scripter

thanks
Tara
 
Old 12-12-2005, 10:08 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
The special $? variable is getting filled with a zero by the echo command you're running. Immediately after the grep statement, you need to assign it to another variable, like so:
Code:
RC1=$?
Then perform your echo commands, and then evaluate $RC1 -ne $SUCCESS.

Get it? The echo command is successful, so it will return a 0 into $?.
 
Old 12-12-2005, 10:10 PM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
P.S. Unless you need variable substitution to occur (and it doesn't look like you do), it's a good idea to put your regular expressions in single quotes.

Example:
Code:
grep '^someval' some-file
P.P.S. You can easily test your grep command from the command line before using it in the script. Very good way to isolate a problem. And after running it from the command line, simply run
Code:
echo $?
to see the return code.

Last edited by anomie; 12-12-2005 at 10:11 PM.
 
Old 12-12-2005, 10:44 PM   #4
tara
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58

Original Poster
Rep: Reputation: 15
thanks for the help

i can get it working if i do the grep on the command line.
i can get it working in the script if the #reboot or reboot command is in another file, but i cannot get it to work if the grep is on the same file that i calling the grep from

cheers
Tara
 
Old 12-13-2005, 07:47 AM   #5
PDock
Member
 
Registered: Aug 2004
Distribution: Slack10 & curr. tried numerous
Posts: 189

Rep: Reputation: 37
try this
Code:
#!/bin/sh
IAM=$0

if [ `grep -e '^#reboot' $IAM` ] ;then
echo "Grep of #reboot true " 
#change to single # for true
##reboot               
else
echo "Grep of #reboot false"
fi
 
Old 12-13-2005, 06:59 PM   #6
tara
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58

Original Poster
Rep: Reputation: 15
thanks for that it worked nicely, now my problem is that i need to test another condition before i want the machine to run the reboot command. I have a program called control that i need to test whether it is running, if it is not running i want the machine to reboot so the program will restart on boot up, unless i have the reboot command commented out.

This is what i have at the moment but it doesn't work.


Code:
#!/bin/sh

CONTROL=control
IAM=$0

pidno=$(/sbin/pidof -s $CONTROL)

if [ `grep -e '^reboot$' $IAM` ] #check if reboot is enabled
then
   if [ -z "$pidno" ] #if no pid, then process not running
   then
		echo "ready to rebboot"
		#write to log files
		reboot
	else
		echo "ADS-B Bypass is running"
	fi
else
	echo "Reboot is commented out"
fi


i also tried if [ `grep -e '^reboot$' $IAM` ] && [ -z "$pidno" ] but that didn't work?? i need both the program control failed and the reboot not commented out for the machine to reboot it's self.

If seems that the reboot command needs to be in the direct loop as the if statement with the grep. Is this right??

if i put the following at the bottom of the file

Code:
ABC=1
if [ "$ABC" -eq 24 ]
then
echo "equal to 24"
reboot
else
echo "not equal to 24"
fi
it makes the reboot command in the top if statements work!!! - it doesn't make sense

thanks
Tara
 
Old 12-13-2005, 10:29 PM   #7
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
it makes the reboot command in the top if statements work!!!
Can you explain what you mean by this?

You know you don't have to reboot the machine just to restart a program, yes?
 
Old 12-13-2005, 10:45 PM   #8
tara
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58

Original Poster
Rep: Reputation: 15
i can't explain it, but the reboot in the actual code doesn't seem to be recognised by the grep command, but the grep will pick up the reboot in the ABC if statement.

Quote:
You know you don't have to reboot the machine just to restart a program, yes?
yes, this machine is currently designed to reboot when the program fails.
 
Old 12-14-2005, 09:09 AM   #9
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
grep -e '^reboot$' $IAM
this means the word reboot at the beginning of the line like that:
reboot
not like that :
...................reboot
(I replace the blank or tabulation with a point character)
for detecting the latter you must write
grep -e '^[ \t]*reboot$' $IAM
and it's even safer to write
grep -e '^[ \t]*reboot[ \t]*$' $IAM
in case of
...................reboot...............

Last edited by berbae; 12-14-2005 at 09:13 AM.
 
Old 12-14-2005, 05:43 PM   #10
tara
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58

Original Poster
Rep: Reputation: 15
thank you everyone it is finally working


thanks for all your help

cheers

Tara
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
Parse String in a Bash script jimwelc Linux - Newbie 8 11-09-2012 07:47 AM
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
Bash Script String Splitting MurrayL Linux - Newbie 1 09-21-2004 03:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02: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
Open Source Consulting | Domain Registration