The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can't interpret variable in if statement jwholey Shell Programming and Scripting 4 06-25-2008 01:09 PM
Variable problem in for loop with if statement ejdv Shell Programming and Scripting 6 06-17-2008 09:52 AM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 01:25 PM
Using variable in case statement fialia Shell Programming and Scripting 2 05-12-2008 05:54 AM
korn shell to bash - statement not working brdholman UNIX for Dummies Questions & Answers 5 10-15-2007 10:49 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-07-2008
aristegui aristegui is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 24
Bash: evaluating $? variable (if statement)

Hello, i'm unable to write a correct if... statement to evaluate the $? variable.

Could anybody send to me an example? for example, this lines of code didn't work...

if [ $? eq 0 ]; then
etc etc

if [ $? == 0 ]; then
etc etc

Thank you in advanced.
  #2 (permalink)  
Old 07-07-2008
Kweekwom Kweekwom is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 28
This code works fine for me.


Code:
if [ $? == 0 ]
then 
echo "hello"
else
echo "goobye"
fi

  #3 (permalink)  
Old 07-07-2008
aristegui aristegui is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 24
I'm using bash

#!/bin/bash
echo "Parameter $1"
echo "$?"
$SCHRODINGER/utilities/reagentprep -listfull | grep $1
echo "$?"

if [ $? == 1 ]then
echo "Error"
exit
fi

echo "OK"
exit

The output is always the same, with an incorrect (AAA) and an correct (Thiol_S_H) $1 parameter

Parameter AAA
0
Checkout succeeded: MMLIBS/0722 6816 F1B7 A2A5
License file: /opt/schrodinger/license
License Server: xxx@yyy.es
1
OK

and

Parameter Thiol_S_H
0
Checkout succeeded: MMLIBS/0722 6816 F1B7 A2A5
License file: /opt/schrodinger/license
License Server: xxx@yyy.es
23 Aryl_or_Vinyl_Thiol_S_H
38 Thiol_S_H
0
OK

Thankxxx!!!
  #4 (permalink)  
Old 07-07-2008
awk awk is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 134
And this is why we want to see the code


Code:
#!/bin/bash
echo "Parameter $1"
echo "$?"
$SCHRODINGER/utilities/reagentprep -listfull | grep $1
echo "$?"

if [ $? == 1 ]then
echo "Error"
exit
fi

the if statement is evaluating the echo Condition, not the grep as intended.

try instead


Code:
$SCHRODINGER/utilities/reagentprep -listfull | grep $1

STATUS=$?
echo "$STATUS"

if [ $STATUS == 1 ]then
echo "Error"
exit
fi

  #5 (permalink)  
Old 07-08-2008
aristegui aristegui is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 24
Quote:
Originally Posted by awk View Post
And this is why we want to see the code


Code:
#!/bin/bash
echo "Parameter $1"
echo "$?"
$SCHRODINGER/utilities/reagentprep -listfull | grep $1
echo "$?"

if [ $? == 1 ]then
echo "Error"
exit
fi

the if statement is evaluating the echo Condition, not the grep as intended.

try instead


Code:
$SCHRODINGER/utilities/reagentprep -listfull | grep $1

STATUS=$?
echo "$STATUS"

if [ $STATUS == 1 ]then
echo "Error"
exit
fi
Shame on me... thank you very much!!!
  #6 (permalink)  
Old 07-08-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Quote:
Originally Posted by aristegui View Post
Hello, i'm unable to write a correct if... statement to evaluate the $? variable.

Could anybody send to me an example? for example, this lines of code didn't work...

if [ $? eq 0 ]; then
etc etc

if [ $? == 0 ]; then
etc etc

Thank you in advanced.
Are you giving proper space after and before the brackets [] like this

Code:
if [ $? == 0 ];
 then
    echo "hello"
fi

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0