Minor markup formatting change

Andrew Janke 2015-05-29 22:39:50 -04:00
parent 55534bc2eb
commit 1cd6f5ce15

@ -63,7 +63,7 @@ $PWD
TODO: Add a list of all environment variables you can use. TODO: Add a list of all environment variables you can use.
###If / Loop### ### If / For / While ###
Put `; do` and `; then` on the same line as the `while`, `for` or `if`. Put `; do` and `; then` on the same line as the `while`, `for` or `if`.
@ -109,7 +109,7 @@ local UPPERCASE=""
UPPERCASE="" UPPERCASE=""
``` ```
Variables name should not clobber command names, such as `dir` or `pwd`. Variable names should not clobber command names, such as `dir` or `pwd`.
``` ```
# Bad: # Bad:
@ -150,9 +150,7 @@ echo "global_var = $global_var" # global_var = 37
# Good: # Good:
function func_good() { function func_good() {
local local_var="" local local_var=""
local_var=37 local_var=37
echo $local_var echo $local_var
} }
@ -165,7 +163,7 @@ global_var=$(func_good)
echo "global_var = $global_var" # move function result to global scope echo "global_var = $global_var" # move function result to global scope
``` ```
In the next example, lots of global variables are used over and over again, but the script "unfortunately" works anyway. The `parse_json()` function does not even return a value and the two functions shares their variables. You could also write all this without any function; this would have the same effect. In the next example, lots of global variables are used over and over again, but the script "unfortunately" works anyway. The `parse_json()` function does not even return a value and the two functions share their variables. You could also write all this without any function; this would have the same effect.
Bad-Example: with global variables Bad-Example: with global variables
``` ```
@ -268,7 +266,7 @@ Constants and anything exported to the environment should be capitalized.
# Constant # Constant
readonly PATH_TO_FILES='/some/path' readonly PATH_TO_FILES='/some/path'
# Both constant and environment # Both constant and exported
declare -xr ORACLE_SID='PROD' declare -xr ORACLE_SID='PROD'
``` ```
@ -374,27 +372,28 @@ function my_good_func() {
} }
``` ```
###Check return values### ### Return values ###
Always check return values and give informative return values. Always check return values and give informative error messages. For unpiped commands, use `$?` or check directly via an if statement to keep it simple. Use nonzero return values to indicate errors.
For unpiped commands, use `$?` or check directly via an if statement to keep it simple.
``` ```
# Bad: # Bad:
mv "${file_list}" "${dest_dir}/" mv "${file_list}" "${dest_dir}/"
# Good:
mv "${file_list}" "${dest_dir}/" || exit 1
# Good: # Good:
if ! mv "${file_list}" "${dest_dir}/"; then if ! mv "${file_list}" "${dest_dir}/"; then
echo "Unable to move ${file_list} to ${dest_dir}" >&2 echo "Unable to move ${file_list} to ${dest_dir}" >&2
exit "${E_BAD_MOVE}" exit 1
fi fi
# Good: use "$?" to get the last return value # Good: use "$?" to get the last return value
mv "${file_list}" "${dest_dir}/" mv "${file_list}" "${dest_dir}/"
if [[ "$?" -ne 0 ]]; then if [[ "$?" -ne 0 ]]; then
echo "Unable to move ${file_list} to ${dest_dir}" >&2 echo "Unable to move ${file_list} to ${dest_dir}" >&2
exit "${E_BAD_MOVE}" exit 1
fi fi
``` ```
@ -411,14 +410,14 @@ Nested backticks require escaping the inner ones with `\`. The `$(command)` form
var="`command \`command1\``" var="`command \`command1\``"
# Good: # Good:
var="$(command "$(command1)")" var="$(command \"$(command1)\")"
``` ```
### Eval ### ### Eval ###
Eval is evil! Eval munges the input when used for assignment to variables and can set variables without making it possible to check what those variables were. Avoid `eval` if possible. Eval is evil! Eval munges the input when used for assignment to variables and can set variables without making it possible to check what those variables were. Avoid `eval` if possible.
##Sources## ## References ##
- [Shell Style Guide](https://google-styleguide.googlecode.com/svn/trunk/shell.xml) - [Shell Style Guide](https://google-styleguide.googlecode.com/svn/trunk/shell.xml)
- [BASH Programming - Introduction HOW-TO](http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html) - [BASH Programming - Introduction HOW-TO](http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html)