How does ||: evaluate?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How does ||: evaluate?
# 1  
Old 01-09-2013
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 || :

# 2  
Old 01-09-2013
&& 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.
# 3  
Old 01-09-2013
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
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.
# 5  
Old 01-11-2013
Quote:
Originally Posted by Corona688
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.)
# 6  
Old 01-11-2013
: 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.
# 7  
Old 01-11-2013
Quote:
Originally Posted by jim mcnamara
: 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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to evaluate expression under awk?

I have to display only those subscribers which are in "unconnected state" and the date is 90 days older than today's date. Below command is used for this purpose: cat vfsubscriber_20170817.csv | sed -e 's/^"//' -e '1d' | \ nawk -F '",' '{if ( (substr($11,2,4) == 2017) && ( substr($11,2,8)... (1 Reply)
Discussion started by: dia
1 Replies

2. Shell Programming and Scripting

Evaluate Expression within awk

I want to create a conditional expression string and pass in an awk script. My script is as below... comm="\$3 == "hello"" awk -F "^T" -v command="${comm}" ' { if ( command ) { print "hye" } }' testBut the statement "if ( command )" always evaluates to true which is not... (5 Replies)
Discussion started by: Saikat123
5 Replies

3. Shell Programming and Scripting

Evaluate Variable At Runtime

Hi, I am trying to set a variable that has time the format desired. And my intention is to echo variable (instead of actual date command) everytime I like to echo date. Please take a look at below code. $NOW='' echo $NOW After 5 minutes $echo $NOW Issue here is , I am not... (2 Replies)
Discussion started by: vinay4889
2 Replies

4. Shell Programming and Scripting

How to evaluate the value of a variable ?

How to evaluate the value of a variable ? For example: a=var $a=value !!!error happens!!! I want to evaluate var=value, how to realize it? Thanks! ---------- Post updated at 03:37 AM ---------- Previous update was at 02:22 AM ---------- I am using linux bash. a=var $a=value... (4 Replies)
Discussion started by: 915086731
4 Replies

5. Shell Programming and Scripting

cron does not evaluate the quotes

Hi all, I have a script that runs perfectly from cmd, but in the cron it gives a strange ':::::::::::::::' output instead of evaluating the part inside the quotes. this is the script: bash-3.00# more test #!/bin/ksh #-----swap--- TEMP_FILE=/HealthCheck/test/file.txt swap -s | tee... (1 Reply)
Discussion started by: kerrygold
1 Replies

6. Programming

How to evaluate which coding approach is best?

Let's say for example that we have two different ways was can code the exact same program to achieve the same result. What is the best way to determine which of the two methods is the best solution? Is it as simple as basing it on how long the program takes to run or is there a more... (4 Replies)
Discussion started by: jmvbxx
4 Replies

7. Shell Programming and Scripting

Evaluate the value of a variable?

I have variables: FOO="Text" BAR="FOO" I'd like to be able to evaluate the variable named as the value of $BAR. echo $FOO Text echo $BAR FOO This is what I'd like to do: echo ${$BAR} (this won't work) Text (3 Replies)
Discussion started by: Ilja
3 Replies

8. Shell Programming and Scripting

How to evaluate the value read from a file?

Hi, Could someone please help me with how to do the following? Say I have a flat file test.lst and the content of the file is: Report Date - `date '+%m%d%Y'` I'm trying the following while read myLine do echo ${myLine} done<test.lst This prints Report Date - `date... (1 Reply)
Discussion started by: arunsoman80
1 Replies

9. Shell Programming and Scripting

how to get variable to re-evaluate itself?

Probably a simple one. Basically I am retrieving a number from a file - setting a variable against it and then incrementing this by 1 and using this as an entry number in a log file for messages. I need the variable to re-evalute itself each time I call it so I get the latest number in the file -... (1 Reply)
Discussion started by: frustrated1
1 Replies
Login or Register to Ask a Question