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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
12-12-2005, 10:05 PM
|
#1
|
Member
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58
Rep:
|
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
|
|
|
12-12-2005, 10:08 PM
|
#2
|
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Rep: 
|
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: 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 $?.
|
|
|
12-12-2005, 10:10 PM
|
#3
|
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Rep: 
|
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 to see the return code.
Last edited by anomie; 12-12-2005 at 10:11 PM.
|
|
|
12-12-2005, 10:44 PM
|
#4
|
Member
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58
Original Poster
Rep:
|
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
|
|
|
12-13-2005, 07:47 AM
|
#5
|
Member
Registered: Aug 2004
Distribution: Slack10 & curr. tried numerous
Posts: 189
Rep:
|
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
|
|
|
12-13-2005, 06:59 PM
|
#6
|
Member
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58
Original Poster
Rep:
|
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
|
|
|
12-13-2005, 10:29 PM
|
#7
|
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Rep: 
|
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?
|
|
|
12-13-2005, 10:45 PM
|
#8
|
Member
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58
Original Poster
Rep:
|
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.
|
|
|
12-14-2005, 09:09 AM
|
#9
|
Member
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540
Rep: 
|
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.
|
|
|
12-14-2005, 05:43 PM
|
#10
|
Member
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58
Original Poster
Rep:
|
thank you everyone it is finally working
thanks for all your help
cheers
Tara 
|
|
|
All times are GMT -5. The time now is 06:49 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|