LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 05-29-2015, 04:41 AM   #1
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,301

Rep: Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698
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?
 
Old 05-29-2015, 06:53 AM   #2
kmhuntly
Member
 
Registered: Mar 2015
Location: Cheektowaga, NY
Distribution: ArchLinux
Posts: 34

Rep: Reputation: 7
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.
 
Old 05-29-2015, 06:56 AM   #3
sgrlscz
Member
 
Registered: Aug 2008
Posts: 123

Rep: Reputation: 84
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.
 
Old 06-02-2015, 03:11 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,301

Original Poster
Rep: Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698Reputation: 7698
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....
 
Old 06-02-2015, 10:05 AM   #5
sgrlscz
Member
 
Registered: Aug 2008
Posts: 123

Rep: Reputation: 84
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash syntax question screwzm Linux - Software 3 11-14-2012 03:45 AM
[SOLVED] Syntax question for a command within a bash script kaplan71 Linux - Software 11 10-09-2012 08:38 PM
[SOLVED] Question about bash script syntax musonio Linux - Software 9 09-10-2009 08:28 PM
[SOLVED] [bash] if - then syntax question. RaptorX Programming 2 08-12-2009 06:55 PM
bash syntax question babag Linux - Newbie 14 05-30-2008 06:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration