Question on using "[[ ]]" in lieu of IF Construct


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question on using "[[ ]]" in lieu of IF Construct
# 1  
Old 02-06-2009
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  
Old 02-06-2009
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  
Old 02-07-2009
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
# 4  
Old 02-07-2009
Quote:
Originally Posted by sunpraveen
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.
# 5  
Old 02-07-2009
Quote:
Originally Posted by cfajohnson

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")

# 6  
Old 02-09-2009
Thanks everyone, for your replies. It was very helpful.

Regards,

Praveen
# 7  
Old 02-09-2009
Quote:
Originally Posted by otheus
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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Solaris

"Estream construct failed" Error on Solaris i86pc

Hi Guys, From past some days, I am getting an error in /var/adm/messages which is as shown below. XXXXX02:/# cat /var/adm/messages |tail Sep 16 15:28:14 XXXX02 EV_AGENT: Agent Main --Estream construct failed. Err: EMULSocket::recv() Sep 16 15:31:49 XXXX02 EV_AGENT: Agent main --... (2 Replies)
Discussion started by: vivek.goel.piet
2 Replies

4. Shell Programming and Scripting

(CSH basic construct) "@" query

While going through a script, i came across few syntax which I am not aware of what they exactly means. @ cnt = 1 @ num_all = `echo $var` What is the significance of "@" here. What is it called? (3 Replies)
Discussion started by: animesharma
3 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Why stderr file descriptor redirection makes ksh's "select" construct hang.

I am trying to use one global declaration --> "exec 2>$ERR" to capture all stderr outputs that may occur anywhere in my script. Then close it at the end of the script using --> "exec 2<&-" I am using KSH on Solaris 8. KSH Version M-11/16/88i If I comment two "exec .." statements in the... (11 Replies)
Discussion started by: kchinnam
11 Replies

7. UNIX for Advanced & Expert Users

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have a problem about the Oracle related components. I'm not able to find any answer yet, and waiting for your responses... Here is the configuration of my system: * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or... (1 Reply)
Discussion started by: talipk
1 Replies

8. UNIX and Linux Applications

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or question of my own) is: Oracle tns listener, "CT_LISTENER", and the enterprise manager (EM) of the instance, which is uniq instance and called... (0 Replies)
Discussion started by: talipk
0 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question