|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
&& 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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
|
: 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
|
|||
|
|||
|
Quote:
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|