diff options
| author | Paul Buetow <git@mx.buetow.org> | 2021-05-16 14:51:28 +0100 |
|---|---|---|
| committer | Paul Buetow <git@mx.buetow.org> | 2021-05-21 05:11:05 +0100 |
| commit | c83586f2c6cd8b046e7384a01c2e7c52e1fbb703 (patch) | |
| tree | a5c3efbc2d1e40d7beb8ff9d32c2380d260779b4 | |
| parent | cae4c7372929f0b386cb88f3a31cfe44c01863d6 (diff) | |
more fixes
| -rw-r--r-- | content/gemtext/gemfeed/2021-05-15-personal-bash-coding-style-guide.draft.gmi | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/content/gemtext/gemfeed/2021-05-15-personal-bash-coding-style-guide.draft.gmi b/content/gemtext/gemfeed/2021-05-15-personal-bash-coding-style-guide.draft.gmi index 1863fd7f..fea6c302 100644 --- a/content/gemtext/gemfeed/2021-05-15-personal-bash-coding-style-guide.draft.gmi +++ b/content/gemtext/gemfeed/2021-05-15-personal-bash-coding-style-guide.draft.gmi @@ -25,11 +25,11 @@ These are my personal modifications of the Google Guide. ### 2 space soft-tabs indentation -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. +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. 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. -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. +Google also recommends limiting the 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 practice 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. 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. @@ -70,7 +70,7 @@ greet () { } ``` -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: +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 this instead: ``` say_hello_to_paul () { @@ -88,7 +88,7 @@ declare FOO=bar echo "foo${FOO}baz" ``` -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. +A few more words on always quoting the variables: For the sake of consistency (and for the sake of making ShellCheck happy) I am not against quoting everything I encounter. I personally also think that the larger the Bash script becomes, the more important it becomes to always 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 it for example. It's just that I won't quote everything in every small script I write. ### Prefer builtin commands over external commands @@ -104,11 +104,11 @@ addition="$(expr "${X}" + "${Y}")" substitution="$(echo "${string}" | sed -e 's/^foo/bar/')" ``` -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). +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 natively when it comes to text manipulation (the name "sed" stands for streaming editor after all). 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 too. 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). -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). +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). 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? ;-) @@ -116,7 +116,7 @@ I even didn't get started what you can do with Awk (especially GNU Awk), a fully ### Use of 'yes' and 'no' -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? +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 easier to read. Yes, the Bash script would need to perform string comparisons on every check, but if performance is important to you, you wouldn't want to use a Bash script anyway, correct? ``` declare -r SUGAR_FREE=yes @@ -161,7 +161,7 @@ declare bay=foo bar baz foo ``` -And if I want to assign variables dynamically then I could just run an external script and source it's output (This is how you could do metaprogramming in Bash - write code which produces code for immediate execution): +And if I want to assign variables dynamically then I could just run an external script and source its output (This is how you could do metaprogramming in Bash - write code which produces code for immediate execution): ``` % cat vars.sh @@ -223,7 +223,7 @@ The stdout is always passed as a pipe to the next following stage. The stderr is 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. -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: +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 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 pure Bash wizard) some time ago: ``` some_function () { @@ -304,8 +304,8 @@ The following looks like valid Bash code: ``` if [[ "${my_var}" > 3 ]]; then - # True for 4, false for 22. - do_something + # True for 4, false for 22. + do_something fi ``` @@ -313,7 +313,7 @@ fi ``` if (( my_var > 3 )); then - do_something + do_something fi ``` @@ -321,18 +321,20 @@ or ``` if [[ "${my_var}" -gt 3 ]]; then - do_something + do_something fi ``` ### PIPESTATUS -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: +To be honest, I have never used the PIPESTATUS variable before. I knew that it's there, but I never bothered to fully understand it until now. + +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: ``` tar -cf - ./* | ( cd "${dir}" && tar -xf - ) if (( PIPESTATUS[0] != 0 || PIPESTATUS[1] != 0 )); then - echo "Unable to tar files to ${dir}" >&2 + echo "Unable to tar files to ${dir}" >&2 fi ``` @@ -342,16 +344,25 @@ However, as PIPESTATUS will be overwritten as soon as you do any other command, tar -cf - ./* | ( cd "${DIR}" && tar -xf - ) return_codes=( "${PIPESTATUS[@]}" ) if (( return_codes[0] != 0 )); then - do_something + do_something fi if (( return_codes[1] != 0 )); then - do_something_else + do_something_else fi ``` +## Use common sense and BE CONSISTENT. + +The following 2 paragraphs are completely quoted from the Google guidelines. But they hit the hammer on the head: + +> If you are editing code, take a few minutes to look at the code around you and determine its style. If they use spaces around their if clauses, you should, too. If their comments have little boxes of stars around them, make your comments have little boxes of stars around them too. + +> 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 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. + + ## Advanced Bash learning pro tip -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. +I also highly recommend having 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. => https://tldp.org/LDP/abs/html/ Advanced Bash-Scripting Guide |
