run script error
I have a script as below
script1 if [[ $? = 0 ]]; then echo abc fi I found that if the output of script1 includes "0" , then it still echo abc , for example , if the output of script1 is below , it still echo abc . $script1 Permission denied Permission denied 0 Can advise if I want it will echo abc only when the output have the word "0" , do not have other word , what can I do ? thanks |
With 1000+ posts, I think you should know to put code tags around you code.
The $? shell variable holds the exit status of the last executed command. As far as I know, all BASH commands returns an exit status of 0 upon successful execution. Unless your script sets an exit status via the exit command, it may (I say may), always return an exit status of 0 or success, which means that the script exited successfully, not that it necessarily did what it was meant to do. Since you did not give an example of script1, hopefully this would explain it: script1.sh: Code:
#! /bin/bash Code:
$ ./script1.sh ; if [[ $? = 0 ]]; then echo "abc" ; else echo "def" ; fi Hope it helps. |
$? is the exit status of previous command, where is the previous command(s). Can you post the whole script.
For permission denied error make sure the commands that you can run, which are before "if [[ $? = 0 ]]; then" line. Code:
bash script1 Code:
ls -l script1 Code:
chmod u+x script1 Code:
./script1 echo abc. if [[ $? = 0 ]]; then echo abc fi Check this sample exit status Code:
cat ping |
I'd also like to point out that $? is a numeric integer value and should be tested with -eq, not '=' (used for strings)
http://tldp.org/LDP/abs/html/comparison-ops.html I'd also recommend [[ ]] instead of [ ] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS |
Quote:
This is very useful to me and once again thank you very much for remembering my mistakes. |
Quote:
At what point are you actually going to start LEARNING something on your own, rather than asking to be spoon-fed answers??? And as far as I can tell, you've never even ACKNOWLEDGED that you've received help, said thanks, or come back to post a solution. Why should anyone here answer your questions, when you behave like this? |
All times are GMT -5. The time now is 04:15 PM. |