LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-20-2012, 08:17 PM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 31
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
 
Old 11-20-2012, 09:40 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
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.
 
1 members found this post helpful.
Old 11-20-2012, 09:49 PM   #3
mandyapenguin
Member
 
Registered: Nov 2011
Location: India
Distribution: RedHat, Cent OS, Fedora, Debian, Ubuntu
Posts: 106

Rep: Reputation: Disabled
$? 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
 
Old 11-20-2012, 11:59 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
2 members found this post helpful.
Old 11-21-2012, 12:09 AM   #5
mandyapenguin
Member
 
Registered: Nov 2011
Location: India
Distribution: RedHat, Cent OS, Fedora, Debian, Ubuntu
Posts: 106

Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
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.
 
Old 11-21-2012, 09:52 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by ust View Post
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Run script error ust Linux - Newbie 2 03-25-2009 05:39 AM
error when tying to run python script(bash error?) shanenin Programming 5 01-10-2006 10:01 AM
run script error ust Linux - Software 3 01-04-2006 03:56 AM
run script error ust Linux - Software 7 09-09-2004 08:44 AM
Script does not want to run, error rhuser Programming 15 03-06-2003 05:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration