Bash: evaluating $? variable (if statement)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: evaluating $? variable (if statement)
# 1  
Old 07-07-2008
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  
Old 07-07-2008
This code works fine for me.

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

# 3  
Old 07-07-2008
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  
Old 07-07-2008
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  
Old 07-08-2008
Quote:
Originally Posted by aristegui
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

# 6  
Old 07-08-2008
Quote:
Originally Posted by awk
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!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bash statement equivalent

Hi all, i need a equivalent for the statement i run in bash, so it would also run in other shells. Specially i need it for ksh to run on AIX. Here the statements: exec > >(tee -a $log) exec 2> >(tee -a $log >&2) Thanks. (5 Replies)
Discussion started by: Kosak
5 Replies

2. Shell Programming and Scripting

[Solved] If statement in bash

I have the following code in bash, however "set red frmt" is not displayed. echo "iarg_rd = $iarg_rd" iarg_rd="2" if ; then echo "Hello World" fi if ; then frmt="${gap}${!frmt_titl_yl}" elif ; then frmt="${gap}${!frmt_titl_bk}" elif ; then echo... (2 Replies)
Discussion started by: kristinu
2 Replies

3. Shell Programming and Scripting

BASH - case statement

Hi Gurus, I have the below BASH code which does not works for upper case alphabets except Z (upper case Z). What may be the reason. Also escape sequences like \n, \t, \b, \033(1m \033(0m (For bold letter) are not working. case $var in ) echo "Lower case alphabet" ;; ... (7 Replies)
Discussion started by: GaneshAnanth
7 Replies

4. UNIX for Dummies Questions & Answers

Bash - OR within an IF statement

Hey guys, Currently trying to write a wee script that runs only when logged in as one of two users. The rest of the script is working fine, but no matter what user I try to run it as, it always fails! This is the puzzling part:if ]; then echo "Run script as admin " exit 1 else... (6 Replies)
Discussion started by: jimbob01
6 Replies

5. Shell Programming and Scripting

bash if statement help needed

Hi I need a script with an if statement that goes. I need it to search through all files within a directory with the extension .test if it finds the string '71502FSC1206' then do sed 's/71502FSC1206/\n&/g' > send.test If it finds the string '715MCH' or '715JAC' then I need it to move the... (1 Reply)
Discussion started by: firefox2k2
1 Replies

6. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

7. Shell Programming and Scripting

evaluating a variable inside a variable

Hi there, i think im getting myself a little confused and need some help :wall: I am reading in a bunch of variables to my script from an external file and need to validate that a value has been set for each so if you can imagine, the user is required to pass in 4 values... (3 Replies)
Discussion started by: rethink
3 Replies

8. Shell Programming and Scripting

Evaluating a variable

Does anyone know of a way to force a variable name held in another variable to return the value of the first variable? Best if I give an example, that does not work: /usr/local/bin >cat mike.sh NUM1ref=16 NUM2ref=32 echo "==============" for VAR in NUM1 NUM2 do XXXX=${VAR}ref echo $XXXX... (4 Replies)
Discussion started by: mikejordan
4 Replies

9. UNIX for Dummies Questions & Answers

Conditional statement in bash

I want to combine 2 conditional statements by using -o in bash, but it won't work. if ; then echo "The number needs to be between 0 and $nr" fi Each time i execute the file it says: ./selectCitaat: line 10: syntax error near unexpected token `$1' (3 Replies)
Discussion started by: doc.arne
3 Replies

10. Shell Programming and Scripting

K Shell evaluating value to a variable

Hi, I have the following requirement. V="First" R="V" echo $$R The output should be First. How do i achieve this. how do we evaluate the $R and evaluate it to $V as $R contains V and $V is First. Thanks Vijay (2 Replies)
Discussion started by: vijaykrc
2 Replies
Login or Register to Ask a Question