LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   run script error (http://www.linuxquestions.org/questions/linux-newbie-8/run-script-error-4175438045/)

ust 11-20-2012 08:17 PM

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

towheedm 11-20-2012 09:40 PM

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

read -p "Select option 0, 1, 2, 3: "
case $REPLY in
  0)
    echo -e "Exiting with success and a 0\n0"
    exit 0    # Exit with success
    ;;
  1)
    echo -e "Exiting with success and Hello\nHello"
    exit 0  # Exit with success
    ;;
  2)
    echo -e "Exiting with failure and a 0\n0"
    exit 1  # Exit with failure
    ;;
  3)
    echo -e "Exiting with failure and Hello\nHello"
    exit 1  # Exit with failure
    ;;
esac

Code:

$ ./script1.sh ; if [[ $? = 0 ]]; then echo "abc" ; else echo "def" ; fi
Select option 0, 1, 2, 3: 0
Exiting with success and a 0
0
abc
$ ./script1.sh ; if [[ $? = 0 ]]; then echo "abc" ; else echo "def" ; fi
Select option 0, 1, 2, 3: 1
Exiting with success and Hello
Hello
abc
$ ./script1.sh ; if [[ $? = 0 ]]; then echo "abc" ; else echo "def" ; fi
Select option 0, 1, 2, 3: 2
Exiting with failure and a 0
0
def
$ ./script1.sh ; if [[ $? = 0 ]]; then echo "abc" ; else echo "def" ; fi
Select option 0, 1, 2, 3: 3
Exiting with failure and Hello
Hello
def

Check the exit status of the last command in scripts and set an exit status.

Hope it helps.

mandyapenguin 11-20-2012 09:49 PM

$? 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
Check that you have right permission to run it
Code:

ls -l script1
sudo chown $USER script1

if excecution permission is not there, then set using
Code:

chmod u+x script1
and run
Code:

./script1
If your script really have only these 3 lines then defenitely it will
echo abc.
if [[ $? = 0 ]]; then
echo abc
fi

Check this sample exit status
Code:

cat ping
#!/bin/bash
ping -c2 google.com >/dev/null
if [ $? = 0 ]; then # here is the exit status of previous command
echo "Success!"
else
echo "Failed"
fi


chrism01 11-20-2012 11:59 PM

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

mandyapenguin 11-21-2012 12:09 AM

Quote:

Originally Posted by chrism01 (Post 4833767)
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

Thanks chrism01.
This is very useful to me and once again thank you very much for remembering my mistakes.

TB0ne 11-21-2012 09:52 AM

Quote:

Originally Posted by ust (Post 4833686)
I have a script as below
Code:

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

ust, you've been here NINE YEARS...you've asked DOZENS of questions about scripting, and they've been answered. You've been pointed to tutorials MANY times.

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 09:28 PM.