LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 07:05 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Rep: Reputation: 32
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, 07: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, 07:42 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 07:46 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

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

Original Poster
Rep: Reputation: 32
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 08:56 AM.
 
Old 08-01-2008, 09:30 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 02:49 AM   #7
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
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, 06:00 AM   #8
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:26 PM.

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