@@ -131,6 +131,10 @@ By naming convention, bash scripts end with `.sh`
...
@@ -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)
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 !`
> combination of `bash #` and `bang !`
---
### Comments start with `#`
### Comments start with `#`
On a line, any characters after `#` will be ignored -- *with the exception of* `#!`
On a line, any characters after `#` will be ignored -- *with the exception of* `#!`
...
@@ -140,13 +144,13 @@ echo "A comment will follow." # Comment here.
...
@@ -140,13 +144,13 @@ echo "A comment will follow." # Comment here.
# ^ Note whitespace before #
# ^ Note whitespace before #
```
```
---
### There is no standard indentation
### There is no standard indentation
-Pick a standard in your team that you can all work to
-Pick a standard in your team that you can all work to
-Use something your editor makes easy (**Vim** uses `Tab`)
-Use something your editor makes easy (**Vim** uses `Tab`)
---
### Command separators
### Command separators
Commands can be combined using **meta-characters** and **control operators**
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
...
@@ -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 and data types in Bash
Variables let you store data : **numeric values** or **character(s)**
Variables let you store data : **numeric values** or **character(s)**
...
@@ -190,11 +198,11 @@ Set the variable values in the following ways :
...
@@ -190,11 +198,11 @@ Set the variable values in the following ways :
- access the variable value using `$`: `echo $greeting`
- access the variable value using `$`: `echo $greeting`
- assign based on variable: `b=$a`
- 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) `" '`
### Quotes for character(s) `" '`
Double will do variable substitution, single will not
Double will do variable substitution, single will not
...
@@ -207,16 +215,6 @@ Double will do variable substitution, single will not
...
@@ -207,16 +215,6 @@ Double will do variable substitution, single will not
myvar=$(ls)
myvar=$(ls)
```
```
### Export variables
```bash
#!/bin/bash
var1=blah
# Make the variable `var1` available to child processes
export var1
./script2.sh
```
---
---
## Variable naming conventions
## Variable naming conventions
...
@@ -237,20 +235,6 @@ export var1
...
@@ -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
# Arithmetic
| Operator | Operation |
| Operator | Operation |
...
@@ -288,7 +272,7 @@ a=$( expr 10 - 3 )
...
@@ -288,7 +272,7 @@ a=$( expr 10 - 3 )
```
```
- double parentheses : return the result of the expression
- double parentheses : return the result of the expression
> it is the preferred method
> it is the **preferred method**
---
---
...
@@ -336,18 +320,17 @@ fi
...
@@ -336,18 +320,17 @@ fi
### `test` command
### `test` command
```bash
```bash
test-s /proc/cpuinfo
test-s /etc/hosts &&echo"Exists and not empty"
echo$?
```
```
| Operator | Description |
| Operator | Description |
| ------------ | --------------- |
| ------------ | --------------- |
| `-d FILE` | FILE exists and is a directory |
| `-d FILE` | FILE exists and is a directory |
| `-e FILE` | FILE exists |
| `-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**)|
| `-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 FILE` | FILE exists and the write permission is granted |
|`-x FILE` | FILE exists and the execute permission is granted |
> `-w` and `-x` test the write and the execute permission
---
---
...
@@ -409,6 +392,10 @@ esac
...
@@ -409,6 +392,10 @@ esac
---
---
# Hands-on exercise
---
# Arrays
# Arrays
### Indexed arrays
### Indexed arrays
...
@@ -456,8 +443,7 @@ unset countries[BHR]
...
@@ -456,8 +443,7 @@ unset countries[BHR]
echo${countries[@]}
echo${countries[@]}
```
```
---
<!--
### About `declare`
### About `declare`
```bash
```bash
...
@@ -472,6 +458,7 @@ Examples :
...
@@ -472,6 +458,7 @@ Examples :
- force a character(s) variable to store all uppercase or lowercase letters
- force a character(s) variable to store all uppercase or lowercase letters
- display the attributes and values of all variables
- display the attributes and values of all variables
- etc.
- etc.
-->
---
---
...
@@ -543,6 +530,8 @@ for i in $( cat file.txt )
...
@@ -543,6 +530,8 @@ for i in $( cat file.txt )
---
---
### How to Read a File Line By Line
```bash
```bash
#!/bin/bash
#!/bin/bash
# How to Read a File Line By Line
# How to Read a File Line By Line
...
@@ -554,11 +543,15 @@ do
...
@@ -554,11 +543,15 @@ do
done < "$input"
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
> 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
The `-r` option is used not to allow backslashes to escape any characters
-->
---
---
...
@@ -580,6 +573,10 @@ done
...
@@ -580,6 +573,10 @@ done
---
---
# Hands-on exercise
---
# Arguments - Positional Parameters
# Arguments - Positional Parameters
How to pass command-line arguments to a bash script ?
How to pass command-line arguments to a bash script ?
...
@@ -607,6 +604,17 @@ a b c d e
...
@@ -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
### Flags
```bash
```bash
...
@@ -668,7 +676,7 @@ date
...
@@ -668,7 +676,7 @@ date
echo$?
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
...
@@ -812,6 +820,10 @@ ls
---
---
# Hands-on exercise
---
# Shell vs Environment Variables
# Shell vs Environment Variables
Consider the script `test.sh` below :
Consider the script `test.sh` below :
...
@@ -957,6 +969,10 @@ time ./manager_par.sh
...
@@ -957,6 +969,10 @@ time ./manager_par.sh
---
---
# Hands-on exercise
---
# Debug
# Debug
Tips and techniques for debugging and troubleshooting Bash scripts
Tips and techniques for debugging and troubleshooting Bash scripts