diff options
Diffstat (limited to 'gemfeed/2021-05-16-personal-bash-coding-style-guide.html')
| -rw-r--r-- | gemfeed/2021-05-16-personal-bash-coding-style-guide.html | 379 |
1 files changed, 236 insertions, 143 deletions
diff --git a/gemfeed/2021-05-16-personal-bash-coding-style-guide.html b/gemfeed/2021-05-16-personal-bash-coding-style-guide.html index 72c24941..f34a8170 100644 --- a/gemfeed/2021-05-16-personal-bash-coding-style-guide.html +++ b/gemfeed/2021-05-16-personal-bash-coding-style-guide.html @@ -8,10 +8,17 @@ <link rel="stylesheet" href="style-override.css" /> </head> <body> -<h1 style='display: inline'>Personal Bash coding style guide</h1><br /> +<p class="header"> +<a href="https://foo.zone">Home</a> | <a href="https://codeberg.org/snonux/foo.zone/src/branch/content-md/gemfeed/2021-05-16-personal-bash-coding-style-guide.md">Markdown</a> | <a href="gemini://foo.zone/gemfeed/2021-05-16-personal-bash-coding-style-guide.gmi">Gemini</a> +</p> +<h1 style='display: inline' id='personal-bash-coding-style-guide'>Personal Bash coding style guide</h1><br /> <br /> <span class='quote'>Published at 2021-05-16T14:51:57+01:00</span><br /> <br /> +<span>Lately, I have been polishing and writing a lot of Bash code. Not that I never wrote a lot of Bash, but now as I also looked through the Google Shell Style Guide, I thought it is time also to write my thoughts on that. I agree with that guide in most, but not in all points. </span><br /> +<br /> +<a class='textlink' href='https://google.github.io/styleguide/shellguide.html'>Google Shell Style Guide</a><br /> +<br /> <pre> .---------------------------. /,--..---..---..---..---..--. `. @@ -25,29 +32,53 @@ "\__/"---------------"\__/"-+---+' </pre> <br /> -<span>Lately, I have been polishing and writing a lot of Bash code. Not that I never wrote a lot of Bash, but now as I also looked through the Google Shell Style Guide, I thought it is time also to write my thoughts on that. I agree with that guide in most, but not in all points. </span><br /> -<br /> -<a class='textlink' href='https://google.github.io/styleguide/shellguide.html'>Google Shell Style Guide</a><br /> -<br /> -<h2 style='display: inline'>My modifications</h2><br /> +<h2 style='display: inline' id='table-of-contents'>Table of Contents</h2><br /> +<br /> +<ul> +<li><a href='#personal-bash-coding-style-guide'>Personal Bash coding style guide</a></li> +<li>⇢ <a href='#my-modifications'>My modifications</a></li> +<li>⇢ ⇢ <a href='#shebang'>Shebang</a></li> +<li>⇢ ⇢ <a href='#two-space-soft-tabs-indentation'>Two space soft-tabs indentation</a></li> +<li>⇢ ⇢ <a href='#breaking-long-pipes'>Breaking long pipes</a></li> +<li>⇢ ⇢ <a href='#quoting-your-variables'>Quoting your variables</a></li> +<li>⇢ ⇢ <a href='#prefer-built-in-commands-over-external-commands'>Prefer built-in commands over external commands</a></li> +<li>⇢ <a href='#my-additions'>My additions</a></li> +<li>⇢ ⇢ <a href='#use-of--yes--and--no-'>Use of 'yes' and 'no'</a></li> +<li>⇢ ⇢ <a href='#non-evil-alternative-to-variable-assignments-via-eval'>Non-evil alternative to variable assignments via eval</a></li> +<li>⇢ ⇢ <a href='#prefer-pipes-over-arrays-for-list-processing'>Prefer pipes over arrays for list processing</a></li> +<li>⇢ ⇢ <a href='#assign-then-shift'>Assign-then-shift</a></li> +<li>⇢ ⇢ <a href='#paranoid-mode'>Paranoid mode</a></li> +<li>⇢ <a href='#learned'>Learned</a></li> +<li>⇢ ⇢ <a href='#unintended-lexicographical-comparison'>Unintended lexicographical comparison.</a></li> +<li>⇢ ⇢ <a href='#pipestatus'>PIPESTATUS</a></li> +<li>⇢ <a href='#use-common-sense-and-be-consistent'>Use common sense and BE CONSISTENT.</a></li> +<li>⇢ <a href='#advanced-bash-learning-pro-tip'>Advanced Bash learning pro tip</a></li> +</ul><br /> +<h2 style='display: inline' id='my-modifications'>My modifications</h2><br /> <br /> <span>These are my modifications to the Google Guide.</span><br /> <br /> -<h3 style='display: inline'>Shebang</h3><br /> +<h3 style='display: inline' id='shebang'>Shebang</h3><br /> <br /> <span>Google recommends using always...</span><br /> <br /> -<pre> -#!/bin/bash +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="silver">#!/bin/bash </font></i> </pre> <br /> <span>... as the shebang line, but that does not work on all Unix and Unix-like operating systems (e.g., the *BSDs don't have Bash installed to /bin/bash). Better is:</span><br /> <br /> -<pre> -#!/usr/bin/env bash +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="silver">#!/usr/bin/env bash</font></i> </pre> <br /> -<h3 style='display: inline'>Two space soft-tabs indentation</h3><br /> +<h3 style='display: inline' id='two-space-soft-tabs-indentation'>Two space soft-tabs indentation</h3><br /> <br /> <span>I know there have been many tab- and soft-tab wars on this planet. Google recommends using two space soft-tabs for Bash scripts. </span><br /> <br /> @@ -57,15 +88,18 @@ <br /> <span>I hit the 80 character line length quicker with the four spaces than with two spaces, but that makes me refactor the Bash code more aggressively, which is a good thing. </span><br /> <br /> -<h3 style='display: inline'>Breaking long pipes</h3><br /> +<h3 style='display: inline' id='breaking-long-pipes'>Breaking long pipes</h3><br /> <br /> <span>Google recommends breaking up long pipes like this:</span><br /> <br /> -<pre> -# All fits on one line +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="silver"># All fits on one line</font></i> command1 | command2 -# Long commands +<i><font color="silver"># Long commands</font></i> command1 \ | command2 \ | command3 \ @@ -74,58 +108,75 @@ command1 \ <br /> <span>I think there is a better way like the following, which is less noisy. The pipe | already indicates the Bash that another command is expected, thus making the explicit line breaks with \ obsolete:</span><br /> <br /> -<pre> -# Long commands +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="silver"># Long commands</font></i> command1 | command2 | command3 | command4 </pre> <br /> -<h3 style='display: inline'>Quoting your variables</h3><br /> +<span class='quote'>Update: It's 2023 now, and I have changed my mind. I think Google's way is the better one. It may be a bit more to type, but the leading <span class='inlinecode'>|</span> are a nice eye catcher, so you know immediately what is going on!</span><br /> +<br /> +<h3 style='display: inline' id='quoting-your-variables'>Quoting your variables</h3><br /> <br /> <span>Google recommends always quote your variables. Generally, it would be best if you did that only for variables where you are unsure about the content/values of the variables (e.g., content is from an external input source and may contain whitespace or other special characters). In my opinion, the code will become quite noisy when you always quote your variables like this:</span><br /> <br /> -<pre> -greet () { - local -r greeting="${1}" - local -r name="${2}" - echo "${greeting} ${name}!" +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre>greet () { + <b><u><font color="#000000">local</font></u></b> -r greeting=<font color="#808080">"${1}"</font> + <b><u><font color="#000000">local</font></u></b> -r name=<font color="#808080">"${2}"</font> + echo <font color="#808080">"${greeting} ${name}!"</font> } </pre> <br /> <span>In this particular example, I agree that you should quote them as you don't know the input (are there, for example, whitespace characters?). But if you are sure that you are only using simple bare words, then I think that the code looks much cleaner when you do this instead:</span><br /> <br /> -<pre> -say_hello_to_paul () { - local -r greeting=Hello - local -r name=Paul - echo "$greeting $name!" +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre>say_hello_to_paul () { + <b><u><font color="#000000">local</font></u></b> -r greeting=Hello + <b><u><font color="#000000">local</font></u></b> -r name=Paul + echo <font color="#808080">"$greeting $name!"</font> } </pre> <br /> <span>You see, I also omitted the curly braces { } around the variables. I only use the curly braces around variables when it makes the code either easier/clearer to read or if it is necessary to use them:</span><br /> <br /> -<pre> -declare FOO=bar -# Curly braces around FOO are necessary -echo "foo${FOO}baz" +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><b><u><font color="#000000">declare</font></u></b> FOO=bar +<i><font color="silver"># Curly braces around FOO are necessary</font></i> +echo <font color="#808080">"foo${FOO}baz"</font> </pre> <br /> <span>A few more words on always quoting the variables: For the sake of consistency (and for making ShellCheck happy), I am not against quoting everything I encounter. I also think that the larger the Bash script becomes, the more critical it becomes always to quote variables. That's because it will be more likely that you might not remember that some of the functions don't work on values with spaces in them, for example. It's just that I won't quote everything in every small script I write. </span><br /> <br /> -<h3 style='display: inline'>Prefer built-in commands over external commands</h3><br /> +<h3 style='display: inline' id='prefer-built-in-commands-over-external-commands'>Prefer built-in commands over external commands</h3><br /> <br /> <span>Google recommends using the built-in commands over available external commands where possible:</span><br /> <br /> -<pre> -# Prefer this: +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="silver"># Prefer this:</font></i> addition=$(( X + Y )) -substitution="${string/#foo/bar}" +substitution=<font color="#808080">"${string/#foo/bar}"</font> -# Instead of this: -addition="$(expr "${X}" + "${Y}")" -substitution="$(echo "${string}" | sed -e 's/^foo/bar/')" +<i><font color="silver"># Instead of this:</font></i> +addition=<font color="#808080">"$(expr "</font>${X}<font color="#808080">" + "</font>${Y}<font color="#808080">")"</font> +substitution=<font color="#808080">"$(echo "</font>${string}<font color="#808080">" | sed -e 's/^foo/bar/')"</font> </pre> <br /> <span>I can't entirely agree here. The external commands (especially sed) are much more sophisticated and powerful than the built-in Bash versions. Sed can do much more than the Bash can ever do by itself when it comes to text manipulation (the name "sed" stands for streaming editor, after all).</span><br /> @@ -136,41 +187,46 @@ substitution="$(echo "${string}" | sed -e 's/^foo/bar/')" <br /> <span>I even didn't get started with what you can do with awk (especially GNU Awk), a fully-fledged programming language. Tiny Awk snippets tend to be used quite often in Shell scripts without honouring the real power of Awk. But if you did everything in Perl or Awk or another scripting language, then it wouldn't be a Bash script anymore, wouldn't it? ;-)</span><br /> <br /> -<h2 style='display: inline'>My additions</h2><br /> +<h2 style='display: inline' id='my-additions'>My additions</h2><br /> <br /> -<h3 style='display: inline'>Use of 'yes' and 'no'</h3><br /> +<h3 style='display: inline' id='use-of--yes--and--no-'>Use of 'yes' and 'no'</h3><br /> <br /> <span>Bash does not support a boolean type. I tend just to use the strings 'yes' and 'no' here. I used 0 for false and 1 for true for some time, but I think that the yes/no strings are easier to read. Yes, the Bash script would need to perform string comparisons on every check, but if performance is crucial to you, you wouldn't want to use a Bash script anyway, correct?</span><br /> <br /> -<pre> -declare -r SUGAR_FREE=yes -declare -r I_NEED_THE_BUZZ=no +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><b><u><font color="#000000">declare</font></u></b> -r SUGAR_FREE=yes +<b><u><font color="#000000">declare</font></u></b> -r I_NEED_THE_BUZZ=no buy_soda () { - local -r sugar_free=$1 + <b><u><font color="#000000">local</font></u></b> -r sugar_free=$1 - if [[ $sugar_free == yes ]]; then - echo 'Diet Dr. Pepper' - else - echo 'Pepsi Coke' - fi + <b><u><font color="#000000">if</font></u></b> [[ $sugar_free == yes ]]; <b><u><font color="#000000">then</font></u></b> + echo <font color="#808080">'Diet Dr. Pepper'</font> + <b><u><font color="#000000">else</font></u></b> + echo <font color="#808080">'Pepsi Coke'</font> + <b><u><font color="#000000">fi</font></u></b> } buy_soda $I_NEED_THE_BUZZ </pre> <br /> -<h3 style='display: inline'>Non-evil alternative to variable assignments via eval</h3><br /> +<h3 style='display: inline' id='non-evil-alternative-to-variable-assignments-via-eval'>Non-evil alternative to variable assignments via eval</h3><br /> <br /> <span>Google is in the opinion that eval should be avoided. I think so too. They list these examples in their guide:</span><br /> <br /> -<pre> -# What does this set? -# Did it succeed? In part or whole? -eval $(set_my_variables) - -# What happens if one of the returned values has a space in it? -variable="$(eval some_function)" +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="silver"># What does this set?</font></i> +<i><font color="silver"># Did it succeed? In part or whole?</font></i> +<b><u><font color="#000000">eval</font></u></b> $(set_my_variables) +<i><font color="silver"># What happens if one of the returned values has a space in it?</font></i> +variable=<font color="#808080">"$(eval some_function)"</font> </pre> <br /> <span>However, if I want to read variables from another file, I don't have to use eval here. I only have to source the file:</span><br /> @@ -201,32 +257,35 @@ Hello paul, it is Sat 15 May 19:21:12 BST 2021 <br /> <span>The downside is that ShellCheck won't be able to follow the dynamic sourcing anymore.</span><br /> <br /> -<h3 style='display: inline'>Prefer pipes over arrays for list processing</h3><br /> +<h3 style='display: inline' id='prefer-pipes-over-arrays-for-list-processing'>Prefer pipes over arrays for list processing</h3><br /> <br /> <span>When I do list processing in Bash, I prefer to use pipes. You can chain them through Bash functions as well, which is pretty neat. Usually, my list processing scripts are of a structure like this:</span><br /> <br /> -<pre> -filter_lines () { - echo 'Start filtering lines in a fancy way!' >&2 +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre>filter_lines () { + echo <font color="#808080">'Start filtering lines in a fancy way!'</font> >&<font color="#000000">2</font> grep ... | sed .... } process_lines () { - echo 'Start processing line by line!' >&2 - while read -r line; do - ... do something and produce a result... - echo "$result" - done + echo <font color="#808080">'Start processing line by line!'</font> >&<font color="#000000">2</font> + <b><u><font color="#000000">while</font></u></b> <b><u><font color="#000000">read</font></u></b> -r line; <b><u><font color="#000000">do</font></u></b> + ... <b><u><font color="#000000">do</font></u></b> something and produce a result... + echo <font color="#808080">"$result"</font> + <b><u><font color="#000000">done</font></u></b> } -# Do some post-processing of the data +<i><font color="silver"># Do some post-processing of the data</font></i> postprocess_lines () { - echo 'Start removing duplicates!' >&2 + echo <font color="#808080">'Start removing duplicates!'</font> >&<font color="#000000">2</font> sort -u } genreate_report () { - echo 'My boss wants to have a report!' >&2 + echo <font color="#808080">'My boss wants to have a report!'</font> >&<font color="#000000">2</font> tee outfile.txt wc -l outfile.txt } @@ -243,139 +302,172 @@ main <br /> <span>The stdout is always passed as a pipe to the next following stage. The stderr is used for info logging.</span><br /> <br /> -<h3 style='display: inline'>Assign-then-shift</h3><br /> +<h3 style='display: inline' id='assign-then-shift'>Assign-then-shift</h3><br /> <br /> <span>I often refactor existing Bash code. That leads me to add and removing function arguments quite often. It's pretty repetitive work changing the $1, $2.... function argument numbers every time you change the order or add/remove possible arguments.</span><br /> <br /> <span>The solution is to use of the "assign-then-shift"-method, which goes like this: "local -r var1=$1; shift; local -r var2=$1; shift". The idea is that you only use "$1" to assign function arguments to named (better readable) local function variables. You will never have to bother about "$2" or above. That is very useful when you constantly refactor your code and remove or add function arguments. It's something that I picked up from a colleague (a pure Bash wizard) some time ago:</span><br /> <br /> -<pre> -some_function () { - local -r param_foo="$1"; shift - local -r param_baz="$1"; shift - local -r param_bay="$1"; shift - ... +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre>some_function () { + <b><u><font color="#000000">local</font></u></b> -r param_foo=<font color="#808080">"$1"</font>; <b><u><font color="#000000">shift</font></u></b> + <b><u><font color="#000000">local</font></u></b> -r param_baz=<font color="#808080">"$1"</font>; <b><u><font color="#000000">shift</font></u></b> + <b><u><font color="#000000">local</font></u></b> -r param_bay=<font color="#808080">"$1"</font>; <b><u><font color="#000000">shift</font></u></b> + + <i><font color="silver"># ...</font></i> } </pre> <br /> <span>Want to add a param_baz? Just do this:</span><br /> <br /> -<pre> -some_function () { - local -r param_foo="$1"; shift - local -r param_bar="$1"; shift - local -r param_baz="$1"; shift - local -r param_bay="$1"; shift - ... +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre>some_function () { + <b><u><font color="#000000">local</font></u></b> -r param_foo=<font color="#808080">"$1"</font>; <b><u><font color="#000000">shift</font></u></b> + <b><u><font color="#000000">local</font></u></b> -r param_bar=<font color="#808080">"$1"</font>; <b><u><font color="#000000">shift</font></u></b> + <b><u><font color="#000000">local</font></u></b> -r param_baz=<font color="#808080">"$1"</font>; <b><u><font color="#000000">shift</font></u></b> + <b><u><font color="#000000">local</font></u></b> -r param_bay=<font color="#808080">"$1"</font>; <b><u><font color="#000000">shift</font></u></b> + + <i><font color="silver"># ...</font></i> } </pre> <br /> <span>Want to remove param_foo? Nothing easier than that:</span><br /> <br /> -<pre> -some_function () { - local -r param_bar="$1"; shift - local -r param_baz="$1"; shift - local -r param_bay="$1"; shift - ... +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre>some_function () { + <b><u><font color="#000000">local</font></u></b> -r param_bar=<font color="#808080">"$1"</font>; <b><u><font color="#000000">shift</font></u></b> + <b><u><font color="#000000">local</font></u></b> -r param_baz=<font color="#808080">"$1"</font>; <b><u><font color="#000000">shift</font></u></b> + <b><u><font color="#000000">local</font></u></b> -r param_bay=<font color="#808080">"$1"</font>; <b><u><font color="#000000">shift</font></u></b> + + <i><font color="silver"># ...</font></i> } </pre> <br /> <span>As you can see, I didn't need to change any other assignments within the function. Of course, you would also need to change the function argument lists at every occasion where the function is invoked - you would do that within the same refactoring session.</span><br /> <br /> -<h3 style='display: inline'>Paranoid mode</h3><br /> +<h3 style='display: inline' id='paranoid-mode'>Paranoid mode</h3><br /> <br /> <span>I call this the paranoid mode. The Bash will stop executing when a command exits with a status not equal to 0:</span><br /> <br /> -<pre> -set -e +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><b><u><font color="#000000">set</font></u></b> -e grep -q foo <<< bar echo Jo </pre> <br /> <span>Here 'Jo' will never be printed out as the grep didn't find any match. It's unrealistic for most scripts to run in paranoid mode purely, so there must be a way to add exceptions. Critical Bash scripts of mine tend to look like this:</span><br /> <br /> -<pre> -#!/usr/bin/env bash +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><i><font color="silver">#!/usr/bin/env bash</font></i> -set -e +<b><u><font color="#000000">set</font></u></b> -e some_function () { - .. some critical code - ... + <i><font color="silver"># .. some critical code</font></i> + <i><font color="silver"># ...</font></i> - set +e - # Grep might fail, but that's OK now + <b><u><font color="#000000">set</font></u></b> +e + <i><font color="silver"># Grep might fail, but that's OK now</font></i> grep .... - local -i ec=$? - set -e + <b><u><font color="#000000">local</font></u></b> -i ec=$? + <b><u><font color="#000000">set</font></u></b> -e - .. critical code continues ... - if [[ $ec -ne 0 ]]; then - ... - fi - ... + <i><font color="silver"># .. critical code continues ...</font></i> + <b><u><font color="#000000">if</font></u></b> [[ $ec -ne <font color="#000000">0</font> ]]; <b><u><font color="#000000">then</font></u></b> + : <i><font color="silver"># ...</font></i> + <b><u><font color="#000000">fi</font></u></b> + <i><font color="silver"># ...</font></i> } </pre> <br /> -<h2 style='display: inline'>Learned</h2><br /> +<h2 style='display: inline' id='learned'>Learned</h2><br /> <br /> <span>There are also a couple of things I've learned from Google's guide.</span><br /> <br /> -<h3 style='display: inline'>Unintended lexicographical comparison.</h3><br /> +<h3 style='display: inline' id='unintended-lexicographical-comparison'>Unintended lexicographical comparison.</h3><br /> <br /> <span>The following looks like a valid Bash code:</span><br /> <br /> -<pre> -if [[ "${my_var}" > 3 ]]; then - # True for 4, false for 22. +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><b><u><font color="#000000">if</font></u></b> [[ <font color="#808080">"${my_var}"</font> > <font color="#000000">3</font> ]]; <b><u><font color="#000000">then</font></u></b> + <i><font color="silver"># True for 4, false for 22.</font></i> do_something -fi +<b><u><font color="#000000">fi</font></u></b> </pre> <br /> <span>... but it is probably an unintended lexicographical comparison. A correct way would be:</span><br /> <br /> -<pre> -if (( my_var > 3 )); then +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><b><u><font color="#000000">if</font></u></b> (( my_var > <font color="#000000">3</font> )); <b><u><font color="#000000">then</font></u></b> do_something -fi +<b><u><font color="#000000">fi</font></u></b> </pre> <br /> <span>or</span><br /> <br /> -<pre> -if [[ "${my_var}" -gt 3 ]]; then +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre><b><u><font color="#000000">if</font></u></b> [[ <font color="#808080">"${my_var}"</font> -gt <font color="#000000">3</font> ]]; <b><u><font color="#000000">then</font></u></b> do_something -fi +<b><u><font color="#000000">fi</font></u></b> </pre> <br /> -<h3 style='display: inline'>PIPESTATUS</h3><br /> +<h3 style='display: inline' id='pipestatus'>PIPESTATUS</h3><br /> <br /> <span>I have never used the PIPESTATUS variable before. I knew that it's there, but I never bothered to understand how it works until now thoroughly.</span><br /> <br /> <span>The PIPESTATUS variable in Bash allows checking of the return code from all parts of a pipe. If it's only necessary to check the success or failure of the whole pipe, then the following is acceptable:</span><br /> <br /> -<pre> -tar -cf - ./* | ( cd "${dir}" && tar -xf - ) -if (( PIPESTATUS[0] != 0 || PIPESTATUS[1] != 0 )); then - echo "Unable to tar files to ${dir}" >&2 -fi +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre>tar -cf - ./* | ( cd <font color="#808080">"${dir}"</font> && tar -xf - ) +<b><u><font color="#000000">if</font></u></b> (( PIPESTATUS[<font color="#000000">0</font>] != <font color="#000000">0</font> || PIPESTATUS[<font color="#000000">1</font>] != <font color="#000000">0</font> )); <b><u><font color="#000000">then</font></u></b> + echo <font color="#808080">"Unable to tar files to ${dir}"</font> >&<font color="#000000">2</font> +<b><u><font color="#000000">fi</font></u></b> </pre> <br /> <span>However, as PIPESTATUS will be overwritten as soon as you do any other command, if you need to act differently on errors based on where it happened in the pipe, you'll need to assign PIPESTATUS to another variable immediately after running the command (don't forget that [ is a command and will wipe out PIPESTATUS).</span><br /> <br /> -<pre> -tar -cf - ./* | ( cd "${DIR}" && tar -xf - ) -return_codes=( "${PIPESTATUS[@]}" ) -if (( return_codes[0] != 0 )); then +<!-- Generator: GNU source-highlight 3.1.9 +by Lorenzo Bettini +http://www.lorenzobettini.it +http://www.gnu.org/software/src-highlite --> +<pre>tar -cf - ./* | ( cd <font color="#808080">"${DIR}"</font> && tar -xf - ) +return_codes=( <font color="#808080">"${PIPESTATUS[@]}"</font> ) +<b><u><font color="#000000">if</font></u></b> (( return_codes[<font color="#000000">0</font>] != <font color="#000000">0</font> )); <b><u><font color="#000000">then</font></u></b> do_something -fi -if (( return_codes[1] != 0 )); then +<b><u><font color="#000000">fi</font></u></b> +<b><u><font color="#000000">if</font></u></b> (( return_codes[<font color="#000000">1</font>] != <font color="#000000">0</font> )); <b><u><font color="#000000">then</font></u></b> do_something_else -fi +<b><u><font color="#000000">fi</font></u></b> </pre> <br /> -<h2 style='display: inline'>Use common sense and BE CONSISTENT.</h2><br /> +<h2 style='display: inline' id='use-common-sense-and-be-consistent'>Use common sense and BE CONSISTENT.</h2><br /> <br /> <span>The following two paragraphs are thoroughly quoted from the Google guidelines. But they hit the hammer on the head:</span><br /> <br /> @@ -384,26 +476,27 @@ fi <span class='quote'>The point of having style guidelines is to have a common vocabulary of coding so people can concentrate on what you are saying rather than on how you are saying it. We present global style rules here, so people know the vocabulary. But local style is also important. If the code you add to a file looks drastically different from the existing code around it, the discontinuity throws readers out of their rhythm when they go to read it. Try to avoid this.</span><br /> <br /> <br /> -<h2 style='display: inline'>Advanced Bash learning pro tip</h2><br /> +<h2 style='display: inline' id='advanced-bash-learning-pro-tip'>Advanced Bash learning pro tip</h2><br /> <br /> <span>I also highly recommend having a read through the "Advanced Bash-Scripting Guide" (not from Google). I use it as the universal Bash reference and learn something new every time I look at it.</span><br /> <br /> <a class='textlink' href='https://tldp.org/LDP/abs/html/'>Advanced Bash-Scripting Guide</a><br /> <br /> +<span>E-Mail your comments to <span class='inlinecode'>paul@nospam.buetow.org</span> :-)</span><br /> +<br /> <span>Other related posts are:</span><br /> <br /> -<a class='textlink' href='./2021-05-16-personal-bash-coding-style-guide.html'>2021-05-16 Personal Bash coding style guide (You are currently reading this)</a><br /> -<a class='textlink' href='./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html'>2021-06-05 Gemtexter - One Bash script to rule it all</a><br /> -<a class='textlink' href='./2021-11-29-bash-golf-part-1.html'>2021-11-29 Bash Golf Part 1</a><br /> +<a class='textlink' href='./2023-12-10-bash-golf-part-3.html'>2023-12-10 Bash Golf Part 3</a><br /> <a class='textlink' href='./2022-01-01-bash-golf-part-2.html'>2022-01-01 Bash Golf Part 2</a><br /> -<br /> -<span>E-Mail your comments to <span class='inlinecode'>paul@nospam.buetow.org</span> :-)</span><br /> +<a class='textlink' href='./2021-11-29-bash-golf-part-1.html'>2021-11-29 Bash Golf Part 1</a><br /> +<a class='textlink' href='./2021-06-05-gemtexter-one-bash-script-to-rule-it-all.html'>2021-06-05 Gemtexter - One Bash script to rule it all</a><br /> +<a class='textlink' href='./2021-05-16-personal-bash-coding-style-guide.html'>2021-05-16 Personal Bash coding style guide (You are currently reading this)</a><br /> <br /> <a class='textlink' href='../'>Back to the main site</a><br /> <p class="footer"> -Generated by <a href="https://codeberg.org/snonux/gemtexter">Gemtexter 2.1.0-release</a> | -served by <a href="https://www.OpenBSD.org">OpenBSD</a>/<a href="https://man.openbsd.org/httpd.8">httpd(8)</a> | -<a href="https://www.foo.zone/site-mirrors.html">Site Mirrors</a> +Generated with <a href="https://codeberg.org/snonux/gemtexter">Gemtexter 3.0.1-develop</a> | +served by <a href="https://www.OpenBSD.org">OpenBSD</a>/<a href="https://man.openbsd.org/relayd.8">relayd(8)</a>+<a href="https://man.openbsd.org/httpd.8">httpd(8)</a> | +<a href="https://foo.zone/site-mirrors.html">Site Mirrors</a> </p> </body> </html> |
