diff --git a/Bash.md b/Bash.md index 4228030501127bcafc441548403ec0a38d788f3e..b74e476365e054e5d9516aeeedc87dc4b4cff173 100644 --- a/Bash.md +++ b/Bash.md @@ -14,7 +14,7 @@ _paginate: false Introduction to Bash Scripting<!--fit--> === -https://gogs.elic.ucl.ac.be/pbarriat/learning-bash +https://forge.uclouvain.be/barriat/learning-bash  @@ -131,6 +131,10 @@ By naming convention, bash scripts end with `.sh` A good practice is to define a `shebang` : first line of the script, `shebang` is simply an absolute path to the shell interpreter (see `echo $SHELL` result) > combination of `bash #` and `bang !` + + +--- + ### Comments start with `#` On a line, any characters after `#` will be ignored -- *with the exception of* `#!` @@ -140,13 +144,13 @@ echo "A comment will follow." # Comment here. # ^ Note whitespace before # ``` ---- - ### There is no standard indentation - Pick a standard in your team that you can all work to - Use something your editor makes easy (**Vim** uses `Tab`) +--- + ### Command separators Commands can be combined using **meta-characters** and **control operators** @@ -176,6 +180,10 @@ $ cd myfolder || ls # if failed cd to myfolder, `ls` will run --- +# Hands-on exercise + +--- + # Variables and data types in Bash Variables let you store data : **numeric values** or **character(s)** @@ -190,11 +198,11 @@ Set the variable values in the following ways : - access the variable value using `$`: `echo $greeting` - assign based on variable: `b=$a` - > !!! no space before or after `=` in the assignation !!! - > `myvar=Hello World` :boom: `-bash: World: command not found` - --- +**!!!** no space before or after `=` in the assignation **!!!** +> `myvar=Hello World` :boom: `-bash: World: command not found` + ### Quotes for character(s) `" '` Double will do variable substitution, single will not @@ -207,16 +215,6 @@ Double will do variable substitution, single will not myvar=$( ls ) ``` -### Export variables - -```bash -#!/bin/bash -var1=blah -# Make the variable `var1` available to child processes -export var1 -./script2.sh -``` - --- ## Variable naming conventions @@ -237,20 +235,6 @@ export var1 --- -| Special Variables | | -| ------------ | --------------- | -| `$0` | the name of the script | -| `$1` - `$9` | the first 9 arguments | -| `$#` | how many arguments were passed | -| `$@` | all the arguments supplied | -| `$$` | the process ID of the current script | -| `$?` | the exit status of the most recently run process | -| `$RANDOM` | returns a random number | -| `$LINENO` | returns the current line number | -| `$SECONDS` | the number of seconds since the script was started | - ---- - # Arithmetic | Operator | Operation | @@ -288,7 +272,7 @@ a=$( expr 10 - 3 ) ``` - double parentheses : return the result of the expression - > it is the preferred method + > it is the **preferred method** --- @@ -336,18 +320,17 @@ fi ### `test` command ```bash -test -s /proc/cpuinfo -echo $? +test -s /etc/hosts && echo "Exists and not empty" ``` | Operator | Description | | ------------ | --------------- | | `-d FILE` | FILE exists and is a directory | | `-e FILE` | FILE exists | -| `-r FILE` | FILE exists and the read permission is granted | -| `-s FILE` | FILE exists and it's size is greater than zero (ie. it is not empty)| -| `-w FILE` | FILE exists and the write permission is granted | -| `-x FILE` | FILE exists and the execute permission is granted | +| `-s FILE` | FILE exists and it's size is greater than zero (ie. it is **not empty**)| +| `-r FILE` | FILE exists and the read permission is granted | + +> `-w` and `-x` test the write and the execute permission --- @@ -409,6 +392,10 @@ esac --- +# Hands-on exercise + +--- + # Arrays ### Indexed arrays @@ -456,8 +443,7 @@ unset countries[BHR] echo ${countries[@]} ``` ---- - +<!-- ### About `declare` ```bash @@ -472,6 +458,7 @@ Examples : - force a character(s) variable to store all uppercase or lowercase letters - display the attributes and values of all variables - etc. +--> --- @@ -543,6 +530,8 @@ for i in $( cat file.txt ) --- +### How to Read a File Line By Line + ```bash #!/bin/bash # How to Read a File Line By Line @@ -554,11 +543,15 @@ do done < "$input" ``` -The internal field separator (`IFS`) is set to the empty string to preserve whitespace issues - > by default `read` removes all leading and trailing whitespace characters such as spaces and tabs +<!-- +`while IFS= read -r line` + +The internal field separator (`IFS`) is set to the empty string to preserve whitespace issues + The `-r` option is used not to allow backslashes to escape any characters +--> --- @@ -580,6 +573,10 @@ done --- +# Hands-on exercise + +--- + # Arguments - Positional Parameters How to pass command-line arguments to a bash script ? @@ -607,6 +604,17 @@ a b c d e --- +| Special Variables | | +| ------------ | --------------- | +| `$0` | the name of the script | +| `$1` - `$9` | the first 9 arguments | +| `$#` | how many arguments were passed | +| `$@` | all the arguments supplied | +| `$$` | the process ID of the current script | +| `$?` | the exit status of the most recently run process | + +--- + ### Flags ```bash @@ -668,7 +676,7 @@ date echo $? ``` -> [List of common exit codes for GNU/Linux](https://slg.ddnss.de/list-of-common-exit-codes-for-gnu-linux/) +> [List of special exit codes for GNU/Linux](https://tldp.org/LDP/abs/html/exitcodes.html) --- @@ -812,6 +820,10 @@ ls --- +# Hands-on exercise + +--- + # Shell vs Environment Variables Consider the script `test.sh` below : @@ -957,6 +969,10 @@ time ./manager_par.sh --- +# Hands-on exercise + +--- + # Debug Tips and techniques for debugging and troubleshooting Bash scripts diff --git a/Bash.pdf b/Bash.pdf index 648b55afe650adf209f05f9fd162b0aaef9390ed..8cb5993a64b75d471669475f421c0052383ce462 100644 Binary files a/Bash.pdf and b/Bash.pdf differ