merge of forgotten link

This commit is contained in:
waldek 2022-05-20 17:15:30 +02:00
parent f85ead5c4e
commit 97db0d9d07
3 changed files with 166 additions and 6 deletions

View File

@ -668,7 +668,90 @@ fi
## How does it work behind the scenes? ## 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). 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 before string2 lexicographically. |
| string1 > string2 | True if string1 sorts after string2 lexicographically. | | string1 > string2 | True if string1 sorts after string2 lexicographically. |
## Nested `if` statements ## Nested `if` statements
It's worth pointing out we can *nest* `if` statements *inside* other `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` ## 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 TODO
### `(( ))` ### `(( ))`
@ -799,7 +890,13 @@ TODO
### `&&` and `||` ### `&&` 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) [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. # 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) [Ryan's tutorials](https://ryanstutorials.net/bash-scripting-tutorial/bash-functions.php)
# User Interface - Make your scripts user friendly. # 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 ## Python

View File

@ -0,0 +1,44 @@
# `bash` login
TODO
## which files are sources when
TODO
# Prompt customization
TODO
# Alternative shells
TODO
## `zsh`
TODO
## `fish`
TODO
## `xonsh`
TODO
# Shell completion
TODO
# Frameworks
TODO
## oh my zsh
TODO
## oh my bash
TODO

View File

@ -1,12 +1,21 @@
# links and ideas # links and ideas
* [info](https://un.curl.dev/) about working in open source * [info](https://un.curl.dev/) about working in open source
* grep and regex [exercises](https://github.com/learnbyexample/Command-line-text-processing/tree/master/exercises/GNU_grep)
* systemd-nspawn doc and exercises * systemd-nspawn doc and exercises
* [hub](https://hub.nspawn.org/images/) * [hub](https://hub.nspawn.org/images/)
* [tutorial](https://blog.selectel.com/systemd-containers-introduction-systemd-nspawn/)
* [docker nspawn](https://seanmcgary.com/posts/run-docker-containers-with-systemd-nspawn)
* [debian](https://wiki.debian.org/nspawn)
* [arch](https://wiki.archlinux.org/title/systemd-nspawn#Use_a_%22macvlan%22_or_%22ipvlan%22_interface)
* [systemd-sysext](https://0pointer.net/blog/testing-my-system-code-in-usr-without-modifying-usr.html) * [systemd-sysext](https://0pointer.net/blog/testing-my-system-code-in-usr-without-modifying-usr.html)
* [history of command line arguments](https://blog.liw.fi/posts/2022/05/07/unix-cli/) * [history of command line arguments](https://blog.liw.fi/posts/2022/05/07/unix-cli/)
* do a mini class on barshrc customisation, prompts, zsh and ohmyzsh
* class on dpkg, apt, and .deb introspection * class on dpkg, apt, and .deb introspection
* class on compiling a simple program, maybe recompiling a kernel * class on compiling a simple program, maybe recompiling a kernel
* exercise on how to take a physical system and convert it to a VM * exercise on how to take a physical system and convert it to a VM
* munin plugin design exercise * munin plugin design exercise
* advanced [bash](https://tldp.org/LDP/abs/html/index.html) scripting
## started
* do a mini class on barshrc customisation, prompts, zsh and ohmyzsh