diff options
Diffstat (limited to 'content/html/gemfeed/2021-05-15-personal-bash-coding-style-guide.draft.html')
| -rw-r--r-- | content/html/gemfeed/2021-05-15-personal-bash-coding-style-guide.draft.html | 142 |
1 files changed, 113 insertions, 29 deletions
diff --git a/content/html/gemfeed/2021-05-15-personal-bash-coding-style-guide.draft.html b/content/html/gemfeed/2021-05-15-personal-bash-coding-style-guide.draft.html index ab2872b2..20411ade 100644 --- a/content/html/gemfeed/2021-05-15-personal-bash-coding-style-guide.draft.html +++ b/content/html/gemfeed/2021-05-15-personal-bash-coding-style-guide.draft.html @@ -65,16 +65,16 @@ h2, h3 { // \\ // \\ |===|| hjw "\__/"---------------"\__/"-+---+' </pre> -<p class="quote"><i>Written by Paul Buetow 2021-15-21</i></p> -<p>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 to also write my thoughts on that. I agree to that guide in most, but not in all points. </p> +<p class="quote"><i>Written by Paul Buetow 2021-05-16</i></p> +<p>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 to also write my own thoughts on that. I agree to that guide in most, but not in all points. </p> <a class="textlink" href="https://google.github.io/styleguide/shellguide.html">Google Shell Style Guide</a><br /> <h2>My modifications</h2> <p>These are my personal modifications of the Google Guide.</p> <h3>2 space soft-tabs indentation</h3> -<p>I know there have been many tab and soft-tab wars on this planet. Google recommends to use 2 space soft-tabs. </p> -<p>My own reality is I don't really care if I use 2 or 4 space indentations. I agree however that tabs should not be used. I personally tend to use 4 space soft-tabs as that's currently how my personal Vim is configured for any programming language. What matters most though is consistency within the same script/project.</p> -<p>Google also recommends to limit line length to 80 characters. For some people that seem's to be an ancient habit from the 80's, where all computer terminals couldn't display longer lines. But I think that the 80 character mark is still a good practise at least for shell scripts. For example I am often writing code on a Microsoft Go Tablet PC (running Linux of course) and it comes in very handy if the lines are not too long due to the relatively small display on the device. </p> -<p>I hit the 80 character line length quicker with the 4 spaces, but that makes me refactor the Bash code more aggressively which is actually a good thing. </p> +<p>I know there have been many tab and soft-tab wars on this planet. Google recommends to use 2 space soft-tabs for Bash scripts. </p> +<p>I personally don't really care if I use 2 or 4 space indentations. I agree however that tabs should not be used. I personally tend to use 4 space soft-tabs as that's currently how my Vim is configured for any programming language. What matters most though is consistency within the same script/project.</p> +<p>Google also recommends to limit line length to 80 characters. For some people that seem's to be an ancient habit from the 80's, where all computer terminals couldn't display longer lines. But I think that the 80 character mark is still a good practise at least for shell scripts. For example I am often writing code on a Microsoft Go Tablet PC (running Linux of course) and it comes in very handy if the lines are not too long due to the relatively small display on the device.</p> +<p>I hit the 80 character line length quicker with the 4 spaces than with 2 spaces, but that makes me refactor the Bash code more aggressively which is actually a good thing. </p> <h3>Breaking long pipes</h3> <p>Google recommends to break up long pipes like this:</p> <pre> @@ -87,7 +87,7 @@ command1 \ | command3 \ | command4 </pre> -<p>I however 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:</p> +<p>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:</p> <pre> # Long commands command1 | @@ -104,23 +104,23 @@ greet () { echo "${greeting} ${name}!" } </pre> -<p>In this particular example I agree that you should quote them as you don't really know what is 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 better if you do:</p> +<p>In this particular example I agree that you should quote them as you don't really know what is 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:</p> <pre> -greet () { +say_hello_to_paul () { local -r greeting=Hello local -r name=Paul echo "$greeting $name!" } </pre> -<p>You see I also omitted the curly braces { } around the variables. I also 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:</p> +<p>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:</p> <pre> declare FOO=bar # Curly braces around FOO are necessary echo "foo${FOO}baz" </pre> -<p>One word more about always quoting your variables: For the sake of consistency (and for the sake of making ShellCheck happy) I am not against to always quote everything you encounter. It's just that I won't do that for every small script I write.</p> +<p>One word more about always quoting the variables: For the sake of consistency (and for the sake of making ShellCheck happy) I am not against to always quote everything I encounter. It's just that I won't do that for every small script I write.</p> <h3>Prefer builtin commands over external commands</h3> -<p>Google prefers to use the builtin commands over external available commands where possible:</p> +<p>Google recommends using the builtin commands over external available commands where possible:</p> <pre> # Prefer this: addition=$(( X + Y )) @@ -130,13 +130,13 @@ substitution="${string/#foo/bar}" addition="$(expr "${X}" + "${Y}")" substitution="$(echo "${string}" | sed -e 's/^foo/bar/')" </pre> -<p>To some degree I somehow agree here, but not fully. The external commands (especially sed) are much more sophisticated. Sed can do much more than the Bash can ever do with native capabilities when it comes to text editing.</p> -<p>I prefer to do light text processing with the Bash builtins and more complicated text processing with the help of external programs such as sed, grep, awk, cut and tr. There is however also the case of medium-light text processing you would want to perform occasionally in a Bash script. I tend to use the external programs here too because I remember using them better than the Bash builtins. The Bash can get quite obscure here (even Perl will be more readable then - Side note: I love Perl).</p> -<p>Also you would like to use an external command for floating point calculation (e.g. bc) instead using the Bash builtins (worth noticing that ZSH supports builtin floating points).</p> -<p>I even didn't get started 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 respecting the real power of it. 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? ;-)</p> +<p>I don't agree fully here. The external commands (especially sed) are much more sophisticated and powerful than the Bash builtin versions. Sed can do much more than the Bash can ever do with native capabilities when it comes to text editing (the name "sed" stands for streaming editor after all).</p> +<p>I prefer to do light text processing with the Bash builtins and more complicated text processing with external programs such as sed, grep, awk, cut and tr. There is however also the case of medium-light text processing where I would want to use external programs. That is so because I remember using them better than the Bash builtins. The Bash can get quite obscure here (even Perl will be more readable then - Side note: I love Perl).</p> +<p>Also, you would like to use an external command for floating point calculation (e.g. bc) instead using the Bash builtins (worth noticing that ZSH supports builtin floating points).</p> +<p>I even didn't get started 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 respecting 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? ;-)</p> <h2>My additions</h2> <h3>Use of 'yes' and 'no'</h3> -<p>Bash does not support a boolean type. I tend to just use the strings 'yes' and 'no' here. For some time I used 0 for false and 1 for true, but I think that the yes/no strings are better readable. Yes, you would need to do string comparisons on every check, but if performance is important to you you wouldn't want to use a Bash script anyway.</p> +<p>Bash does not support a boolean type. I tend to just use the strings 'yes' and 'no' here. For some time I used 0 for false and 1 for true, but I think that the yes/no strings are better readable. Yes, you would need to do string comparisons on every check, but if performance is important to you, you wouldn't want to use a Bash script anyway, correct?</p> <pre> declare -r SUGAR_FREE=yes declare -r I_NEED_THE_BUZZ=no @@ -167,9 +167,9 @@ variable="$(eval some_function)" <p>However, if I want to read variables from another file I don't have to use eval here. I just source the file:</p> <pre> % cat vars.source.sh -local foo=bar -local bar=baz -local bay=foo +declare foo=bar +declare bar=baz +declare bay=foo % bash -c 'source vars.source.sh; echo $foo $bar $baz' bar baz foo @@ -186,8 +186,9 @@ END % bash -c 'source <(./vars.sh); echo "Hello $user, it is $date"' Hello paul, it is Sat 15 May 19:21:12 BST 2021 </pre> +<p>When downside is that ShellCheck won't be able to follow the dynamic sourcing anymore.</p> <h3>Prefer pipes over arrays for list processing</h3> -<p>When I do list processing in Bash I personally prefer to use pipes. You can chain then through Bash functions as well which is pretty neat. Usually my list processing scripts are of a structure like this:</p> +<p>When I do list processing in Bash, I prefer to use pipes. You can chain then through Bash functions as well which is pretty neat. Usually my list processing scripts are of a structure similar to the following example:</p> <pre> filter_lines () { echo 'Start filtering lines in a fancy way!' >&2 @@ -224,23 +225,106 @@ main () { main </pre> <p>The stdout is always passed as a pipe to the next following stage. The stderr is used for info logging.</p> +<h3>Assign-then-shift</h3> +<p>I often refactor existing Bash code. That leads me to adding and removing function arguments quite often. It's quite repetitive work changing the $1, $2.... function argument numbers every time you change the order or add/remove possible arguments.</p> +<p>The solution is to use of the "assign-then-shift"-pattern 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's is very useful when you constantly refactor your code and remove or add function arguments. It's something what I picked up from a colleague (a purely Bash wizard) some time ago:</p> +<pre> +some_function () { + local -r param_foo="$1"; shift + local -r param_baz="$1"; shift + local -r param_bay="$1"; shift + ... +} +</pre> +<p>Want to add a param_baz? Just do this:</p> +<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 + ... +} +</pre> +<p>Want to remove param_foo? Nothing easier than that:</p> +<pre> +some_function () { + local -r param_bar="$1"; shift + local -r param_baz="$1"; shift + local -r param_bay="$1"; shift + ... +} +</pre> +<p>As you can see I didn't need to change any other assignments within the function.</p> +<h3>Paranoid mode</h3> +<p>I call this the paranoid mode. The Bash will stop executing when a command exists with a status not equal to 0:</p> +<pre> +set -e +grep -q foo <<< bar +echo Jo +</pre> +<p>Here 'Jo' will never be printed out as the grep didn't find any match. It's unrealistic for most scripts to purely run in paranoid mode so there must be a way to add exceptions. Critical bash scripts of mine tend to look like this:</p> +<pre> +#!/bin/bash + +set -e + +some_function () { + .. some critical code + set +e + # Grep might fail, but that's OK now + grep .... + local ec=$? + set -e + .. critical code continues ... + if [[ $ec -ne 0 ]]; then + ... + fi + ... +} +</pre> <h2>Learned</h2> +<p>There are also a couple of things I've learned from Googles guide.</p> <h3>Unintended lexicographical comparison.</h3> +<p>The following looks like valid Bash code:</p> <pre> -# Probably unintended lexicographical comparison. if [[ "${my_var}" > 3 ]]; then # True for 4, false for 22. do_something fi </pre> -<p>if (( my_var > 3 )); then</p> -<p> do_something</p> -<p>fi</p> -<p>if [[ "${my_var}" -gt 3 ]]; then</p> -<p> do_something</p> -<p>fi</p> +<p>... but is Probably unintended lexicographical comparison. A correct way would be:</p> +<pre> +if (( my_var > 3 )); then + do_something +fi +</pre> +<p>or</p> +<pre> +if [[ "${my_var}" -gt 3 ]]; then + do_something +fi +</pre> <h3>PIPESTATUS</h3> -<h2>More</h2> +<p>What I have never used is the PIPESTATUS variable. The PIPESTATUS variable in Bash allows checking of the return code from all parts of a pipe. If it’s only necessary to check success or failure of the whole pipe, then the following is acceptable:</p> +<pre> +tar -cf - ./* | ( cd "${dir}" && tar -xf - ) +if (( PIPESTATUS[0] != 0 || PIPESTATUS[1] != 0 )); then + echo "Unable to tar files to ${dir}" >&2 +fi +</pre> +<p>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).</p> +<pre> +tar -cf - ./* | ( cd "${DIR}" && tar -xf - ) +return_codes=( "${PIPESTATUS[@]}" ) +if (( return_codes[0] != 0 )); then + do_something +fi +if (( return_codes[1] != 0 )); then + do_something_else +fi +</pre> +<h2>Advanced Bash learning pro tip</h2> <p>I also highly recommend to have a read through the "Advanced Bash-Scripting Guide" (which is not from Google). I use it as the universal Bash reference and learn something new every time I have a look at it.</p> <a class="textlink" href="https://tldp.org/LDP/abs/html/">Advanced Bash-Scripting Guide</a><br /> <p>E-Mail me your thoughts at comments@mx.buetow.org!</p> |
