diff options
| Commit | 2baec5c780378429ee0e5e4d91e873322581eb48 (patch) | |
|---|---|---|
| Author | 2023-02-26 20:41:17 +0200 | |
| Committer | 2023-02-26 20:41:17 +0200 | |
| Files | b7b04b106fb88283622bc6167f0cf32b0149a707 /src | |
| syndamia.com-2baec5c780378429ee0e5e4d91e873322581eb48.tar syndamia.com-2baec5c780378429ee0e5e4d91e873322581eb48.tar.gz syndamia.com-2baec5c780378429ee0e5e4d91e873322581eb48.zip | ||
| Parent | afe42033920f4c05748cc85e41010f16fda08867 (diff) | |
[talks/lbs] Added variables and data types slides for shells
1 files changed, 127 insertions, 0 deletions
| -rw-r--r-- | src/talks/linuxgraphy-by-strabo/index.md | 127 |
diff --git a/src/talks/linuxgraphy-by-strabo/index.md b/src/talks/linuxgraphy-by-strabo/index.md index 9d828b0..f5748c6 100644 --- a/src/talks/linuxgraphy-by-strabo/index.md +++ b/src/talks/linuxgraphy-by-strabo/index.md @@ -658,6 +658,133 @@ In summary, it installs bash, coreutils and shadow "packages" (from [http://s.mi ## 3.1. Shells +&&lineb2 + +### Variables and data types + +Before talking about the different features newer shells support, we'll roughly cover variables and data types (mostly [bash](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameters) specifics). + +A variable, also called parameter, is created with the syntax (NO spaces between the =): + +``` +name=value +``` + +Where `name` is either a combination of letters (upper and lower case), numbers and the underscore character, which always begins with a letter, or one of select few **positional** and **special** parameters. + +&&/slide + + + +&&slide &&font35 + +#### Positional and special parameters + +[Positional parameters](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Positional-Parameters) are numbers from 1 to N, where each one corresponds to a shell (or function, or script) argument. + +Some of the [special parameters](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Special-Parameters) are: + +- `0`, correspoinding to the shell's or script's name, +- `@` is all positional parameters, separated by spaces +- `#` is the number of positional parameters +- `?` is the last executed (foreground) process +- `$` is process ID of the current shell. + +`value`, without any special surounding characters (more on that later), is treated as a string (but it mustn't have spaces!), however it may also be omitted, in which case it is the empty string `""`. + +&&/font &&/slide + + + +&&slide &&font35 + +#### Data types + +In most shells, there are 3 data types: strings, integers and (one-dimentional) indexed arrays. +[bash has more](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#index-declare), like references and associative arrays, but those aren't universal. + +A value is a **string** when surrounded by single `'` or double `"` quotes, +an **integer** when it isn't surrounded by anything and composed only of digits (if there are letters, it is a string; it's [quite tricky](https://tldp.org/LDP/abs/html/untyped.html) when a value is interpreted as a string or integer) and +an **array** when surrounded by braces `()`, where inside elements are strings or integers, separated by spaces. +An indexed array can also be created by specifying the value at any index: + +``` +name[index]=value +``` + +&&/font &&/slide + + + +&&slide + +#### Mixing types and operations + +``` +name=value +``` + +| Type |Value with| string operation | integer operation | array operation | +|-------|----------|---------------------------------------|---------------------------------------|---------------------------------------------| +|string | |value |0 |value when index is 0, empty string otherwise| +|integer| |empty string, but sometimes value |value |value when index is 0, empty string otherwise| +|array | |operation is done on element at index 0|operation is done on element at index 0|value | + +&&/slide + + + +&&slide + +#### Parameter expansion + +With [parameter expansion](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameter-Expansion), actions are done with the general variable (entity) or on strings and arrays values. +Operations on integers are done with [arithmetic expansion](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Arithmetic-Expansion). + +Parameter expansion is started with the character `$` and either a name or curly braces. +Retrieving a value is done with the forms `$name`, `${name}` and `${name[index]}` for arrays. + +Some parameter expansions in bash: + +- `${#parameter}` length of characters +- `${parameter:offset}` and `${parameter:offset:length}` return substring from index offset +- `${parameter/pattern/string}` return a string, where every match on pattern is replaced by string +- `${!name[@]}` a (space separated) sequence of indecies from 0 to length (for defined values) of array name + +&&/slide + + + +&&slide + +#### Arithmetic expansion + +Arithmetic expansions allow evaluation of [arithmetic expressions](https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Arithmetic). +They're in the form `$(( expression ))`. + +Some useful expressions are: + +- `++name`, `name++`, `--name`, `name++` postfix and prefix incrementation and decrementation +- `+`, `-`, `*`, `/`, `%` normal arithmetic operators, `**` exponentiation +- `~`, `<<`, `>>`, `&`, `^`, `|` bitwise operators +- `==`, `!=`, `>`, `>=`, `<`, `<=` logical comparison +- `!`, `&&`, `||` logical not, and, or +- `expr ? exprTrue : exprFalse` conditional +- `=`, `+=`, ..., `<<=`, ... assignment operators +- `expr1 , expr2` consequtive expressions and `(expr)` bracing + +Expressions work with both variable names and integers. + +&&/slide + + + +&&clide + +### Bourne shell + +Most modern shells support a lot of the more defying features of the [Bourne Shell](https://en.wikipedia.org/wiki/Bourne_shell), which include: + - [Job control](https://en.wikipedia.org/wiki/Job_control_%28Unix%29): Management of currently running groups of processes + `&`: A process can be ran in the background with an ampersand (&) at the end of the command: |