LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-01-2008, 08:05 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 205

Rep: Reputation: 30
Strange if statement behaviour when using bash/bash script


Hi,

I have a strange behaviour I don't seem to understand.

Bash version is:
Code:
$ bash --version
GNU bash, version 3.00.15(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.
In my prompt i do the following:
Code:
]$ echo $?
1
$ if [ $? -eq 1 ] ; then
> echo good
> else
> echo bummer
> fi
good
So far so good. Logic seem right, BUT in my script whre I have exactly the same code:
Code:
echo $?
if [ $? -eq 1 ] ; then
   echo good
else
   echo bummer
fi
I get:
Code:
$./do
1
bummer
If I change the logic in my script (Completely the opposite):
Code:
echo $?
if [ $? -ne 1 ] ; then
   echo good
else
   echo bummer
fi
I get:
Code:
1
good
Now, for me this is very odd. Am I missing something?

Thanks,
Indy
 
Old 08-01-2008, 08:38 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Code:
# ls no_such_file
# echo $?
> 1
# echo $?
> 0
 
Old 08-01-2008, 08:42 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
line 0:  some_failed_command
line 1:  echo $?
line 2:  if [ $? -eq 1 ] ; then
The $? special variable is the exit status of the last executed command. In line 1 the exit status is from a previous command in line 0. But in line 2 it is the exit status of the command in line 1. If the echo command is successfull, as in your examples, the $? variable is updated to its exit status, that is 0.
 
Old 08-01-2008, 08:46 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 659

Rep: Reputation: 160Reputation: 160
I think you want $* not $?
 
Old 08-01-2008, 09:55 AM   #5
freeindy
Member
 
Registered: Nov 2002
Posts: 205

Original Poster
Rep: Reputation: 30
oh yeah that's right.

But still, it doesn't change if i change the code to:
Code:
retval=$?
echo $retval
if [ $retval -ne 1 ] ; then
   echo good
else
   echo bummer
fi
And the output still confuses me:
Code:
1
bummer
AnanthaP: No, I want $? because I'm evaluating a return from a funtion

Last edited by freeindy; 08-01-2008 at 09:56 AM.
 
Old 08-01-2008, 10:30 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
if [ $retval -ne 1 ] ; then
   echo good
else
   echo bummer
fi
You used "not equal". The output must be "bummer".
 
Old 08-04-2008, 03:49 AM   #7
freeindy
Member
 
Registered: Nov 2002
Posts: 205

Original Poster
Rep: Reputation: 30
oh my, would you believe it.
Thanks colucix. Ofcourse it must be bummer. I guess i was too tired to think.

Indy
 
Old 08-04-2008, 07:00 AM   #8
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,244

Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
I copied your first script, and it was working as expected by you. I wonder if you posted the complete script or only this fragment.

Anyway, in cases like this run your script with:
Code:
sh -x yourscript.sh
Indispensable to show the flow within questionable tests cases.

jlinkels
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash script shows strange behaviour when started at boot time michael_kaeppler Linux - General 2 05-11-2008 09:37 PM
Strange behaviour in bash - while loop Guttorm Programming 4 06-08-2007 03:11 PM
problem with if statement in bash script crewblunts Programming 3 03-11-2006 01:38 PM
strange behaviour when bash starts Y0jiMb0 Linux - General 8 07-09-2004 07:29 AM
Need help adding an IF / ELSE statement to my Bash Script Relix Linux - General 1 08-01-2002 03:03 PM


All times are GMT -5. The time now is 11:52 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration