Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-29-2015, 04:41 AM
|
#1
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,301
|
bash syntax question
I found the following syntax and do not really understand how does it work.
So I have a command, which can be more or less anything and a counter, which is usually a grep -c pattern or wc -l or similar.
Here is a construct:
Code:
COUNT=$(<command> | <counter> || :)
The strange thing: COUNT is always set even if the <command> itself is incorrect and cannot be executed. For example:
Code:
COUNT=$(lasd-l/tmp | wc -l || :)
# COUNT is now 0
So in such cases what's happened? Is this syntax ok? Does it work using any bash versions?
|
|
|
05-29-2015, 06:53 AM
|
#2
|
Member
Registered: Mar 2015
Location: Cheektowaga, NY
Distribution: ArchLinux
Posts: 34
Rep:
|
bash syntax question
I'm not sure what's after your || , LQ made it a smiley face. however, generally || means "do this of the command fails", so that's probably what's making it zero
Also if you're typesetting as an integer it'll always be zero unless your command makes it different
Last edited by kmhuntly; 05-29-2015 at 06:59 AM.
|
|
|
05-29-2015, 06:56 AM
|
#3
|
Member
Registered: Aug 2008
Posts: 123
Rep:
|
With
Code:
COUNT=$(<command> | <counter> || :)
you get a 0 in because wc counts the lines of output from lasd-l/tmp, which results in output to stderr, but nothing to stdout, so no lines written to the pipe, so 'wc -l' is 0, and the || never gets used.
In
Code:
$(<command> | <counter> || :)
if <command> | <counter> fails, then the || part of the expression is executed, and that is just a :, which is a special builtin meaning true.
|
|
|
06-02-2015, 03:11 AM
|
#4
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,301
Original Poster
|
so the correct answer looks like: regardless of <command> (even if it was not executable, broken pipe, set -e) <counter> will be run and return something.
|| : has no any effect on it. Looks like the return code is 0 anyway. set -o pipefail will force to set exitcode, that can be escaped by || :
But if you have any comments on it....
|
|
|
06-02-2015, 10:05 AM
|
#5
|
Member
Registered: Aug 2008
Posts: 123
Rep:
|
One purpose of ||: is to ensure that you always get a successful return status.
The construct is useful when you're using 'set -o errexit'. It ensures that specific pieces of code won't generate an error.
For example, if your script uses 'set -o errexit', then without the ||:, your script would exit if either <counter> failed for some reason or if <command> failed and you have pipefail set. With the ||:, setting COUNT will always have a successful return code, so it won't stop your script.
|
|
|
All times are GMT -5. The time now is 09:41 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|