How to use a return code in an if statement?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to use a return code in an if statement?
# 1  
Old 09-10-2010
Question How to use a return code in an if statement?

Hi all,

After so many tries and searching online for ideas, I had trouble accomplishing this. Is it possible to do something like this in KSH to run an if statement on a return code?

Unfortunately the code below fails... Would anyone know how to fix the below attempt?
Code:
if [ "$`{pkginfo TestPackage}`" == "0" ]
then
  print "TestPackage DETECTED"
else
  print "TestPackage NOT detected"
fi

Other info:
When running "pkginfo TestPackage", I did notice when it does exist, afterward typing in "echo $?" will return 0 (success), and when it doesn't exist, it will return 1 (error).

But I wouldn't want the script to print the output of the "pkginfo TestPackage" to the user. I'm hoping to "only" output from the print command in the if statement.

Thanks again everyone for your help.
CG
# 2  
Old 09-10-2010
I would go for something like this:
Code:
pkginfo TestPackage > /dev/null 2>&1         # we do not want any output
if [ $? = 0 ]; then
   echo yeah
else
   echo doh
fi

And btw, this looks strange:
Code:
"$`{pkginfo TestPackage}`"

If you want to execute a command, don't write it like a variable, ie. leave out the $ and the {}. This is what works:
Code:
VAR=$(somecommand)
# or
VAR=`somecommand`

This User Gave Thanks to zaxxon For This Post:
# 3  
Old 09-10-2010
or use it inline

Code:
if pkginfo TestPackage > /dev/null 2>&1 
then
   echo yeah
else
   echo doh
fi

This User Gave Thanks to frank_rizzo For This Post:
# 4  
Old 09-10-2010
Thanks guys

Wow... so simple yet I tried everything but that (go figure). Thank you so much for your help!

cg
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to retrieve "case "statement return value ?

How do we retrieve case statement return value at point indicated in the attached snippet case "$FUN" in 1\ *) do_change_pass ;; 2\ *) do_network_menu ;; 3\ *) do_boot_menu ;; 4\ *) do_internationalisation_menu ;; 5\ *) do_ssh... (6 Replies)
Discussion started by: annacreek
6 Replies

2. Shell Programming and Scripting

If echo statement return false

I have this code that sometimes return a false value and the code inside the if statement gets executed and error out. Any idea why? thanks. So I set a debug and see what the value for $ScriptElapsedTime Here is the value I got ScriptElapsedTime='03:20'. Base on this value the if... (10 Replies)
Discussion started by: nugent
10 Replies

3. Shell Programming and Scripting

How could I use the value of return code

Hello, I am woring on a script where I am getting strange situation.This script actually fetch the source code and tar that code and send to NAS location.This code resides in MKS tool...and we are fetching the source code on checkpoint label basis and script is working fine.First it synch the... (0 Replies)
Discussion started by: anuragpgtgerman
0 Replies

4. Shell Programming and Scripting

::select statement return value with correct field size::

Hi Everyone, I am facing a problem regarding the select from sybase, the return with the incorrect size. For example, field is NAME(20). After i selected from sybase, the result is nicky. after i assign it to another declaration variable, it will be in actual name "nicky" , what i need... (10 Replies)
Discussion started by: ryanW
10 Replies

5. Shell Programming and Scripting

return code help

Hello folks, I have a question that if i type ls command and type echo $? it always show "0", how i could do this change that when i type ls it will show me 1, actually i want to change the return code of commands from 0 to 1. Thanks Bash (5 Replies)
Discussion started by: learnbash
5 Replies

6. Shell Programming and Scripting

Need help with return code 1...

Hi Guys,, I am having a unix script which is running the DB2 Insert command. For the insert command, there were no records to be updated. SQL0100W No row was found for FETCH, UPDATE or DELETE; or the result of a query is an empty table. SQLSTATE=02000 + + echo 1 STAGE_RC=1 + ] ... (6 Replies)
Discussion started by: mac4rfree
6 Replies

7. Shell Programming and Scripting

checking the return code

hi i have a file, i am reading line by line and checking a line contains a string , `grep "Change state" $LINE` if then echo "The line contains---" else echo "The line does not contains---" i need to check the return code , but i am getting an error ... (4 Replies)
Discussion started by: Satyak
4 Replies

8. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

9. Shell Programming and Scripting

asking about return code

hi all my system is linux red hat i have a script that runs some object . the object return some code to the system i see the code by writing echo $? i want to ask in the script if $? equals 14 how shell is do that in the script thanks (3 Replies)
Discussion started by: naamas03
3 Replies

10. UNIX for Advanced & Expert Users

Return code from PL/SQL Code

Hi Guys, I was just wondering if anybody can help me with this problem. OK, how we can get a value back from PL/SQL Script (not stored procedure/function) See the below example: (for example aaa.sh) #!/bin/ksh VALUE=`sqlplus -s user/password@test_id <<EOF @xxx.sq EOF` echo $VALUE ... (7 Replies)
Discussion started by: Shaz
7 Replies
Login or Register to Ask a Question