How does ||: evaluate? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How does ||: evaluate?

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-09-2013
glev2005 glev2005 is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 19 July 2014, 10:56 PM EDT
Posts: 276
Thanks: 20
Thanked 6 Times in 6 Posts
How does ||: evaluate?

In BASH, how does ||: get interpreted. I know || is logical or. And I believe : evaluates to true. Can someone give a thorough explanation for this usage?

Example

Code:
for i in $IGGY
	do
	    [ "$db" == "$i" ] && skipdb=1 || :

Sponsored Links
    #2  
Old 01-09-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 30 September 2014, 4:14 PM EDT
Location: Saskatchewan
Posts: 19,477
Thanks: 794
Thanked 3,286 Times in 3,081 Posts
&& and || are short-form if-then's.


Code:
true && echo "this will print"
false && echo "But this won't"

true || echo "This won't print"
false || echo "But this will"

When you chain them like that, it seems that, if && is false or || is true, it skips one and goes to the next, so you get this:


Code:
$ true && echo success || echo failure

succcess

$ false && echo success || echo failure

failure

$

I have no idea why they had || : there. : is a special command which does nothing. I'm guessing there used to be a command there, which someone tried to delete without deleting the ||, causing a syntax error. So they "fixed" it by putting : in its place.
Sponsored Links
    #3  
Old 01-09-2013
glev2005 glev2005 is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 19 July 2014, 10:56 PM EDT
Posts: 276
Thanks: 20
Thanked 6 Times in 6 Posts
Thanks for your answer. I have seen this in more than one bash script, so I assume it perhaps had a purpose in a prior version of the shell. Perhaps?
    #4  
Old 01-11-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 30 September 2014, 4:14 PM EDT
Location: Saskatchewan
Posts: 19,477
Thanks: 794
Thanked 3,286 Times in 3,081 Posts
You see many strange and redundant things in copied shell scripts, many of which do worse than nothing. People fiddle until it 'works' without really knowing the language.

This does literally nothing, but at least seems harmless.
Sponsored Links
    #5  
Old 01-11-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 30 September 2014, 4:06 PM EDT
Location: San Jose, CA, USA
Posts: 4,760
Thanks: 181
Thanked 1,588 Times in 1,352 Posts
Quote:
Originally Posted by Corona688 View Post
You see many strange and redundant things in copied shell scripts, many of which do worse than nothing. People fiddle until it 'works' without really knowing the language.

This does literally nothing, but at least seems harmless.
Given the 3 line shell script fragment:

Code:
for i in $IGGY
	do
	    [ "$db" == "$i" ] && skipdb=1 || :

we can't say that it does nothing. If the || : is omitted, the exit status of the remaining command line will be non-zero (failed) if db and i don't expand to identical strings. With it present, the exit status of this command line will always be zero (succeeded).

If the next line of code checks the exit status of this line of code (which I agree would be convoluted logic), then the results could change significantly.

Maybe the || : was put in to temporarily disable an error check in the following code, and wasn't removed when testing was completed. (I know this is still unlikely since it looks like skipdb could be used to test for this, but we don't see that skipdb is changed to a value other than 1 for subsequent times though the for loop once it gets set here.)
Sponsored Links
    #6  
Old 01-11-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 30 September 2014, 9:41 AM EDT
Location: NM
Posts: 10,219
Thanks: 278
Thanked 800 Times in 744 Posts
: evaluates in POSIX shells to the result of /usr/bin/true or true.

It is used like that to override a return status value that would affect subsequent processing.

For example, in Appworx (a batch control system) a false return code almost anywhere is detectable, so that if ||: were not there at the end of a line, appworx may see a false return and abort the job when the child process returns. Even though the job may have completed correctly.

Don't ask how, I'd have to guess.
Sponsored Links
    #7  
Old 01-11-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 30 September 2014, 4:06 PM EDT
Location: San Jose, CA, USA
Posts: 4,760
Thanks: 181
Thanked 1,588 Times in 1,352 Posts
Quote:
Originally Posted by jim mcnamara View Post
: evaluates in POSIX shells to the result of /usr/bin/true or true.

It is used like that to override a return status value that would affect subsequent processing.

For example, in Appworx (a batch control system) a false return code almost anywhere is detectable, so that if ||: were not there at the end of a line, appworx may see a false return and abort the job when the child process returns. Even though the job may have completed correctly.

Don't ask how, I'd have to guess.
A likely way to make this happen would be to use the command:
set -e
in a script that Appworx uses to start jobs, or for Appworx to use sh -e when it invokes a shell script.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to evaluate the value of a variable ? 915086731 Shell Programming and Scripting 4 08-11-2011 05:11 AM
How to evaluate which coding approach is best? jmvbxx Programming 4 12-14-2009 09:44 PM
Evaluate the value of a variable? Ilja Shell Programming and Scripting 3 02-02-2009 01:13 PM
how to get variable to re-evaluate itself? frustrated1 Shell Programming and Scripting 1 01-03-2006 08:51 AM



All times are GMT -4. The time now is 05:54 PM.