Special IF construct syntax in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Special IF construct syntax in UNIX
# 1  
Old 05-22-2015
HP Special IF construct syntax in UNIX

Hi,

I don't understand && and || in this context. I thought && is for logical 'AND' and || is for logical 'OR'.

Code:
[ ! -z "$var" ] && echo "Not empty" || echo "Empty"

Please help


Thank You
# 2  
Old 05-22-2015
Yes, it is AND and OR. They connect pipelines and execute them conditionally. man bash:
Quote:
AND and OR lists are sequences of one of more pipelines separated by the && and || control operators, respectively. AND and OR lists are executed with left associa‐
tivity. An AND list has the form

command1 && command2

command2 is executed if, and only if, command1 returns an exit status of zero.

An OR list has the form

command1 || command2

command2 is executed if and only if command1 returns a non-zero exit status. The return status of AND and OR lists is the exit status of the last command executed in
the list.
So - if the test result is TRUE, execute the first command list, if FALSE, the second. That's why it acts as a short if - then - else version. It works because the shell uses "Short Cut evaluation", which means that as soon as the program can determine that the expression is false No Further Evaluation takes place.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 05-22-2015
this is very tricky..I like it.
# 4  
Old 05-22-2015
Unfortunatelly this sequence is not a perfect if-then-else replacement. It works as expected in situations like:
Code:
 $ true && echo true || echo false
true
 $ false && echo true || echo false
false

But consider this:
Code:
 $ true && false || echo false
false

Ouch :-) Allthough the first expression ist true, both the "then" and the "else" part of the sequence is executed, because the "then" part evaluates to false.
This User Gave Thanks to hergp For This Post:
# 5  
Old 05-22-2015
Code:
[[ true || ! false ]] && echo yay || echo oh no

Smilie
# 6  
Old 05-23-2015
Quote:
Originally Posted by hergp
.
.
.
Code:
 $ true && false || echo false
false

Ouch :-) Allthough the first expression ist true, both the "then" and the "else" part of the sequence is executed, because the "then" part evaluates to false.
Absolutely. This might circumvent the trap (not sure if it opens others):
Code:
true && { false || :;  } || echo bad

@ sea: || ! false will never be executed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX Special Characters

Any time I do : ls *.txt > mytext.txt I get something like this in the output file: ^ Tue Jan 22 16:19:19 EST 2013 x86_64 x86_64 x86_64 GNU/Linux t1Fam_BrOv :~>alias | grep ls alias l.='ls -d .* --color=tty' alias lR='ls -R' alias la='ls -Al' alias lc='ls -ltcr' alias ldd='ls -ltr |... (5 Replies)
Discussion started by: genehunter
5 Replies

2. Shell Programming and Scripting

Windows to UNIX FTP Special characters!

I have a file that has the name in one of the lines as MARíA MENDOZA in Windows. When this gets FTPed over to UNIX it appears as MAR�A MENDOZA. Is there anyway to overcome this? Its causing a issue because the file is Postional and fields are getting pushed by 2 digits.. Any help would be... (4 Replies)
Discussion started by: venky338
4 Replies

3. UNIX for Dummies Questions & Answers

UNIX Special files

Hi, I'm looking for information about UNIX Special Files. I must write an essay connected with this topic, and I hope you'll tell me where can I found trusty information about it, because Google doesn't really help me. I'll be grateful for answer. (3 Replies)
Discussion started by: Madrid 7
3 Replies

4. Shell Programming and Scripting

Help with if-else construct

Hi all i have been trying to do a small 'question and answer' script using if-else statement and a combination of pipe. I have succeeded in allowing the user to login with user name and password stored in a sequence username/password in a file named "pass" like this: echo "please enter your... (14 Replies)
Discussion started by: arikutex
14 Replies

5. Shell Programming and Scripting

pattern matching on any special character in Unix

Hi, I have field in a file which would come with any special character, how do i check that field? Eg: @123TYtaasa>>>/ 131dfetr_~2 In the above example, how do I add pattern for any special character on the keyboard. Thanks (3 Replies)
Discussion started by: techmoris
3 Replies

6. Shell Programming and Scripting

syntax error in the if construct

Hi can anyone tell me why is this code giving error mode=$1 if ] || ] then echo "MODES:" exit 1 fi Thanks (5 Replies)
Discussion started by: Anteus
5 Replies

7. Shell Programming and Scripting

Unix Perl split special character $

All I'm trying to split a string at the $ into arrays @data:=<dataFile> a $3.33 b $4.44 dfg $0.56 The split command I have been playing with is: split(/\$/, @data) which results with a .33 b .44 dfg .56 any help with this is appreciated /r Rick (9 Replies)
Discussion started by: schultz2146
9 Replies

8. Shell Programming and Scripting

Perl Script Syntax to Extract Everything After Special Character

Hi, I am writing a Perl script that reads in many lines, if a line meets the criteria I want to edit, it. For example, the script will return the following example line... test=abc123 All I want to do is strip off the "test=" and just be left with the abc123. In my script I can easily... (3 Replies)
Discussion started by: edrichard
3 Replies

9. UNIX for Dummies Questions & Answers

Unix file does not display special characters

We have a unix file that contains special characters (ie. Ñ, °, É, ¿ , £ , ø ). When I try to read this file I get a codepage error and the characters are replaced by the # symbol. How do I keep the special characters from being read? Thanks. Ryan (3 Replies)
Discussion started by: Ryan2786
3 Replies

10. UNIX for Advanced & Expert Users

extracting info from Unix database to construct a visual diagram

Ok heres the situation, We use Solaris 8 at work with Sybase for the db. I need to be able to easily create visual diagrams of some of our more complex systems. I've been using Visio which is such a manual process and takes a while. I was thinking maybe using Visio somehow in conjunction... (0 Replies)
Discussion started by: fusion99
0 Replies
Login or Register to Ask a Question