Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
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.
$? 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
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.