The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
A question/problem about oracle "tns listener" and "enterprise manager" talipk UNIX for Advanced & Expert Users 1 12-03-2008 07:55 AM
A question/problem about oracle "tns listener" and "enterprise manager" talipk UNIX and Linux Applications 0 12-01-2008 03:08 PM
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 03:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 04:15 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 02-06-2009
sunpraveen's Avatar
sunpraveen sunpraveen is offline
Registered User
  
 

Join Date: Jan 2009
Location: Pruthvi
Posts: 85
Question on using "[[ ]]" in lieu of IF Construct

Unix gurus,

I have a piece of code as below.

Code:
[[ -n ${ORACLE_SID} ]] && INST="${ORACLE_SID}" || INST="${TWO_TASK}"
I know that the above code can be used in lieu of an IF construct. I also know that the above code can be extended for the "true" condition to include more than one command (as below):

Code:
[[ -n ${ORACLE_SID} ]] && INST="${ORACLE_SID}" && echo "INST=${INST}" || INST="${TWO_TASK}"
But I am unable to extend the "false" condition to include more than one command.

Desired Output using "[[ ]]" instead of IF construct:

Code:
if the variable ORACLE_SID is set, then
command 1;
command 2;
else
command 1;
command 2;
How can I achieve it? Any tips or suggestions?


TIA,

Regards,

Praveen
  #2 (permalink)  
Old 02-06-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
Actually, it's not the ""[[ ]]" construct nor the "[ ]" construct, but the && construct that does the trick. There is no standard way, however, of doing negation in the UNIX shell using the && || constructs. One can, however, do something like this:
Code:
not() { 
  "$@"
  if [ $? = 0 ]; then return 1; else return 0; fi
}

# not true || echo false
false
# not false && echo true
true
You can also group things together between { }:
Code:
# not true || { echo false; date; echo really false; false; } || echo Total failure.
false
really false
Total failure.
# not true || { echo false; echo really false; true; } && echo not a total failure.
false
really false
not a total failure.
Does that help?

You should be able now to do:
Code:
command_A && { command_A_worked; command_A_worked_report; true; } || { command_A_failed; command_A_failed_report; false; }
  #3 (permalink)  
Old 02-09-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by otheus View Post
There is no standard way, however, of doing negation in the UNIX shell using the && || constructs. One can, however, do something like this:
Code:
not() { 
  "$@"
  if [ $? = 0 ]; then return 1; else return 0; fi
}

# not true || echo false
false
# not false && echo true
true

What's wrong with the negation operator?

Code:
if ! some_command
then
  echo command failed
fi
  #4 (permalink)  
Old 02-07-2009
ddreggors ddreggors is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 91
Code:
TRUE="FALSE"
[[ $TRUE = "TRUE" ]] && echo "true" || (echo "false"; echo "false2")
This outputs:
false
flase2

Now we change this to:

Code:
TRUE="TRUE"
[[ $TRUE = "TRUE" ]] && echo "true" || (echo "false"; echo "false2")
and this ouputs:
true
  #5 (permalink)  
Old 02-07-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by sunpraveen View Post
Unix gurus,

I have a piece of code as below.

Code:
[[ -n ${ORACLE_SID} ]] && INST="${ORACLE_SID}" || INST="${TWO_TASK}"

The [[ ... ]] syntax is non-standard. In almost all cases, you can use the standard [ ... ].
Quote:

I know that the above code can be used in lieu of an IF construct. I also know that the above code can be extended for the "true" condition to include more than one command (as below):

Code:
[[ -n ${ORACLE_SID} ]] && INST="${ORACLE_SID}" && echo "INST=${INST}" || INST="${TWO_TASK}"
But I am unable to extend the "false" condition to include more than one command.

Code:
[ -n "$ORACLE_SID" ] && {
 INST=$ORACLE_SID"
 echo "INST=$INST"
} || {
 INST=$TWO_TASK
 echo "INST=$INST"
}
However, you should note that, in the general case, that is not the same as:

Code:
if [ -n "$ORACLE_SID" ]
then
 : do whatever
else
 : do something else
fi
A simplified example:

Code:
[ -n "$qwerty" ] && false || echo "FAIL!"
"FAIL!" will always be printed. The command after || is executed if either of the preceding commands fails.
  #6 (permalink)  
Old 02-07-2009
ddreggors ddreggors is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 91
Quote:
Originally Posted by cfajohnson View Post

The [[ ... ]] syntax is non-standard. In almost all cases, you can use the standard [ ... ].
OK, then we can still use this. Only change needed was to double quote the variable $TRUE and remove the extra brackets.
Code:
[ "$TRUE" = "TRUE" ] && echo "true" || (echo "false"; echo "false2")
  #7 (permalink)  
Old 02-09-2009
sunpraveen's Avatar
sunpraveen sunpraveen is offline
Registered User
  
 

Join Date: Jan 2009
Location: Pruthvi
Posts: 85
Thanks everyone, for your replies. It was very helpful.

Regards,

Praveen
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 11:29 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