|
|
|
@ -668,7 +668,90 @@ fi
|
|
|
|
|
|
|
|
|
|
## How does it work behind the scenes?
|
|
|
|
|
|
|
|
|
|
TODO - explain `test` and exit status
|
|
|
|
|
## exit status
|
|
|
|
|
|
|
|
|
|
Every command you execute on the command line has an **exit code**.
|
|
|
|
|
You can [read up](https://en.wikipedia.org/wiki/Exit_status) a bit on what they are but the most important things to know are:
|
|
|
|
|
|
|
|
|
|
* the code is always a **number**
|
|
|
|
|
* on our systems it's a [uint8](https://en.wikipedia.org/wiki/Integer_(computer_science)#Common_integral_data_types) which means a value between **0** and **255**
|
|
|
|
|
* the [**convention**](https://tldp.org/LDP/abs/html/exitcodes.html) is that `0` means **success**, everything else is an **error**
|
|
|
|
|
|
|
|
|
|
In your bash shell, the variable `$?` always references the *last* exit status code.
|
|
|
|
|
We can discover it's behaviour as follows.
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
waldek@debian:~$ ls does_exist
|
|
|
|
|
does_exist
|
|
|
|
|
waldek@debian:~$ echo $?
|
|
|
|
|
0
|
|
|
|
|
waldek@debian:~$ ls does_not_exist
|
|
|
|
|
ls: cannot access 'does_not_exist': No such file or directory
|
|
|
|
|
waldek@debian:~$ echo $?
|
|
|
|
|
2
|
|
|
|
|
waldek@debian:~$ echo $?
|
|
|
|
|
0
|
|
|
|
|
waldek@debian:~$
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The **first** `echo $?` prints the exit code of `ls does_exist`.
|
|
|
|
|
The **second** prints the exit code of the *failed* command `ls does_not_exist`.
|
|
|
|
|
The **third** prints the exit code of the `echo $?` that failed!
|
|
|
|
|
|
|
|
|
|
## `test`
|
|
|
|
|
|
|
|
|
|
The presence of exit codes means we can **evaluate** their value and make **decisions** based on the outcome.
|
|
|
|
|
The main workhorse for this is a builtin called `test`.
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
waldek@debian:~$ whatis test
|
|
|
|
|
test (1) - check file types and compare values
|
|
|
|
|
waldek@debian:~$
|
|
|
|
|
```
|
|
|
|
|
I **highly** recommend you take some time to read the `man test`.
|
|
|
|
|
Because the convention of exit codes is *no news, good news* there are two tiny programs that just serve to output `true` and `false`, where true is `0` and false is `1`.
|
|
|
|
|
A little demonstration.
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
waldek@debian:~$ which true
|
|
|
|
|
/usr/bin/true
|
|
|
|
|
waldek@debian:~$ which false
|
|
|
|
|
/usr/bin/false
|
|
|
|
|
waldek@debian:~$ man true
|
|
|
|
|
waldek@debian:~$ whatis true
|
|
|
|
|
true (1) - do nothing, successfully
|
|
|
|
|
waldek@debian:~$ whatis false
|
|
|
|
|
false (1) - do nothing, unsuccessfully
|
|
|
|
|
waldek@debian:~$ true
|
|
|
|
|
waldek@debian:~$ echo $?
|
|
|
|
|
0
|
|
|
|
|
waldek@debian:~$ false
|
|
|
|
|
waldek@debian:~$ echo $?
|
|
|
|
|
1
|
|
|
|
|
waldek@debian:~$ test true == true
|
|
|
|
|
waldek@debian:~$ echo $?
|
|
|
|
|
0
|
|
|
|
|
waldek@debian:~$ test true == false
|
|
|
|
|
waldek@debian:~$ echo $?
|
|
|
|
|
1
|
|
|
|
|
waldek@debian:~$
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Again, I highly advise you to read the `man test`.
|
|
|
|
|
If you did this then the following will make a lot of sense.
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
waldek@debian:~$ test -a does_exist
|
|
|
|
|
waldek@debian:~$ echo $?
|
|
|
|
|
0
|
|
|
|
|
waldek@debian:~$ test -d does_exist
|
|
|
|
|
waldek@debian:~$ echo $?
|
|
|
|
|
1
|
|
|
|
|
waldek@debian:~$ test -a does_not_exist
|
|
|
|
|
waldek@debian:~$ echo $?
|
|
|
|
|
1
|
|
|
|
|
waldek@debian:~$
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The table below is taken from the bash reference manual you can find [here](https://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions).
|
|
|
|
|
|
|
|
|
@ -709,8 +792,6 @@ The table below is taken from the bash reference manual you can find [here](http
|
|
|
|
|
| string1 < string2 | True if string1 sorts before string2 lexicographically. |
|
|
|
|
|
| string1 > string2 | True if string1 sorts after string2 lexicographically. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Nested `if` statements
|
|
|
|
|
|
|
|
|
|
It's worth pointing out we can *nest* `if` statements *inside* other `if` statements.
|
|
|
|
@ -789,8 +870,18 @@ fi
|
|
|
|
|
|
|
|
|
|
## A *modern* version of `test`
|
|
|
|
|
|
|
|
|
|
I'll be the first to admit that the syntax of `bash` can be confusing and is rarely reader friendly.
|
|
|
|
|
A nice, but brief, explication of the nuances of single and double brackets can be found in [this](https://unix.stackexchange.com/questions/306111/what-is-the-difference-between-the-bash-operators-vs-vs-vs) stack overflow post.
|
|
|
|
|
The double bracket command are called [compound commands](https://www.gnu.org/software/bash/manual/html_node/Compound-Commands.html).
|
|
|
|
|
|
|
|
|
|
### `[[ ]]`
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
waldek@debian:~$ [[ 3 = [[:digit:]] ]] ; echo $?
|
|
|
|
|
0
|
|
|
|
|
waldek@debian:~$ [ 3 = [[:digit:]] ] ; echo $?
|
|
|
|
|
1
|
|
|
|
|
```
|
|
|
|
|
TODO
|
|
|
|
|
|
|
|
|
|
### `(( ))`
|
|
|
|
@ -799,7 +890,13 @@ TODO
|
|
|
|
|
|
|
|
|
|
### `&&` and `||`
|
|
|
|
|
|
|
|
|
|
TODO
|
|
|
|
|
```
|
|
|
|
|
waldek@debian:~$ test true == true && echo "yes sir!" || echo "nope..."
|
|
|
|
|
yes sir!
|
|
|
|
|
waldek@debian:~$ test true == false && echo "yes sir!" || echo "nope..."
|
|
|
|
|
nope...
|
|
|
|
|
waldek@debian:~$
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[Ryan's tutorials](https://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php)
|
|
|
|
|
|
|
|
|
@ -813,11 +910,21 @@ Rename all files in a folder with an prefix or postfix.
|
|
|
|
|
|
|
|
|
|
# Functions - Reuse code to make life easier.
|
|
|
|
|
|
|
|
|
|
## defining a function
|
|
|
|
|
|
|
|
|
|
## global vs local variable
|
|
|
|
|
|
|
|
|
|
## return values
|
|
|
|
|
|
|
|
|
|
## the `command` builtin
|
|
|
|
|
|
|
|
|
|
[Ryan's tutorials](https://ryanstutorials.net/bash-scripting-tutorial/bash-functions.php)
|
|
|
|
|
|
|
|
|
|
# User Interface - Make your scripts user friendly.
|
|
|
|
|
|
|
|
|
|
[Ryan's tutorials](https://ryanstutorials.net/bash-scripting-tutorial/bash-user-interfaces.php)
|
|
|
|
|
* [dialog tutorial](https://www.linuxjournal.com/article/2807)
|
|
|
|
|
* [better dialog tutorial](https://linuxcommand.org/lc3_adv_dialog.php)
|
|
|
|
|
* [Ryan's tutorials](https://ryanstutorials.net/bash-scripting-tutorial/bash-user-interfaces.php)
|
|
|
|
|
|
|
|
|
|
## Python
|
|
|
|
|
|
|
|
|
|