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 |
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=$? 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 Code:
echo $? |
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 |
try this
Code:
#!/bin/sh |
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 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 thanks Tara |
Quote:
You know you don't have to reboot the machine just to restart a program, yes? |
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:
|
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............... |
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. |