looking for string on particular line - bash script
Linux - GeneralThis 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.
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.
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
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 $?.
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
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
#!/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
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
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.
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...............
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.