LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   looking for string on particular line - bash script (https://www.linuxquestions.org/questions/linux-general-1/looking-for-string-on-particular-line-bash-script-392165/)

tara 12-12-2005 10:05 PM

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 :confused:

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

anomie 12-12-2005 10:08 PM

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 $?.

anomie 12-12-2005 10:10 PM

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.

tara 12-12-2005 10:44 PM

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 :confused: :confused:

cheers
Tara

PDock 12-13-2005 07:47 AM

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


tara 12-13-2005 06:59 PM

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:confused: :confused: :confused: :confused:

thanks
Tara

anomie 12-13-2005 10:29 PM

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?

tara 12-13-2005 10:45 PM

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.

berbae 12-14-2005 09:09 AM

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...............

tara 12-14-2005 05:43 PM

thank you everyone it is finally working :) :) :)


thanks for all your help

cheers

Tara :cool:


All times are GMT -5. The time now is 07:07 PM.