Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
skripting-section:skripting-grundlagen:bash-grundlagen [2019/05/21 10:07] – michael | skripting-section:skripting-grundlagen:bash-grundlagen [2019/10/22 08:45] (current) – michael | ||
---|---|---|---|
Line 5: | Line 5: | ||
Skripte, die für jeden Nutzer zugänglich sein sollen kann man in ''/ | Skripte, die für jeden Nutzer zugänglich sein sollen kann man in ''/ | ||
+ | {{: | ||
===== Basis Knowhow und Aufbau eines Skriptes ===== | ===== Basis Knowhow und Aufbau eines Skriptes ===== | ||
Line 15: | Line 16: | ||
**__Syntax: | **__Syntax: | ||
- | * /bin/bash [/ | + | * '' |
- | * source [/ | + | * '' |
- | * ./script.sh | + | * '' |
Line 46: | Line 47: | ||
==== Aufbau eines Skripts ==== | ==== Aufbau eines Skripts ==== | ||
+ | <sxh bash> | ||
+ | #!/bin/bash | ||
+ | # Name: add.sh | ||
+ | # Addiere zwei Werte | ||
+ | let summe=$1+$2 | ||
+ | echo "Die Summe von $1 + $2 = $summe" | ||
+ | </ | ||
+ | | ||
+ | In der ersten Zeile befindet sich das She Bang Zeichen, das zeigt, dass es sich um ein ausführbares Programm handelt und definiert mit welchem Interpreter das Skript ausgeführt wird. Danach folgt der Dateiname (Optional) und eine Kurzbeschreibung (Optional). Ab Zeile vier folgen die Anweisungen. | ||
+ | |||
+ | |||
+ | Bei „init“ Skripte sollte zur besseren Übersicht ein kompletter „head“ erstellt werden. | ||
+ | |||
+ | Beispiel: | ||
+ | |||
+ | <sxh bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | # xxx-Script | ||
+ | |||
+ | ### BEGIN INIT INFO | ||
+ | # Provides: | ||
+ | # Required-Start: | ||
+ | # Required-Stop: | ||
+ | # Should-Start: | ||
+ | # Should-Stop: | ||
+ | # Default-Start: | ||
+ | # Default-Stop: | ||
+ | # Short-Description: | ||
+ | ### END INIT INFO | ||
+ | |||
+ | # Anweisungen | ||
+ | ... | ||
+ | </ | ||
</ | </ | ||
Line 52: | Line 87: | ||
---- | ---- | ||
+ | <WRAP center box 100%> | ||
+ | ==== Übergabevariablen und Rückgabewerte ==== | ||
+ | Wenn beim Start eines Skripts Werte übergeben, oder ein Rückgabewert ausgegeben werden soll, können diese im Skript als Variablen aufgefangen werden. Dafür gibt es Automatische Variablen in der Shell. | ||
+ | |||
+ | |||
+ | **__Übergabevariablen: | ||
+ | * '' | ||
+ | |||
+ | |||
+ | |||
+ | **__Rückgabewerte: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | |||
+ | **__Beispiel: | ||
+ | <sxh bash> | ||
+ | #!/bin/bash | ||
+ | # Uerbergabevariablen ausgeben. | ||
+ | |||
+ | echo "Das Skript wurde mit dem Kommando $0 gestartet" | ||
+ | echo "Die Prozessnummer des Skripts ($0) lautet: $$" | ||
+ | echo "Es wurden $# Parameter uebergeben" | ||
+ | echo "Der erste Parameter lautet: $1" | ||
+ | echo "Der zweite Parameter lautet: $2" | ||
+ | Huchhh... | ||
+ | echo "Der aktuelle Errorlevel lautet: $?" | ||
+ | exit 0 | ||
+ | </ | ||
+ | |||
+ | <sxh bash; gutter: | ||
+ | michael@home: | ||
+ | Das Script wurde mit dem Kommando ./uvar.sh gestartet | ||
+ | Die Prozessnummer des Scripts (./uvar.sh) lautet: 3332 | ||
+ | Es wurden 2 Parameter uebergeben | ||
+ | Der erste Parameter lautet: 3 | ||
+ | Der zweite Parameter lautet: 7 | ||
+ | ./uvar.sh: Zeile 8: Huchhh...: Kommando nicht gefunden. | ||
+ | Der aktuelle Errorlevel lautet: 127 | ||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | <WRAP center box 100%> | ||
+ | ==== Werte und Dateitypen Vergleichen mit " | ||
+ | |||
+ | „test“ ist ein Programm zum Dateitypen überprüfen und Werte vergleichen. Das Programm wird mit einem Exit-Status gemäss „AUSDRUCK“ beendet. | ||
+ | |||
+ | |||
+ | **__Syntax: | ||
+ | * '' | ||
+ | |||
+ | |||
+ | |||
+ | **__Ausdruck: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | |||
+ | <WRAP center round important 100%> | ||
+ | Ausser den Tests '' | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <WRAP center box 100%> | ||
+ | ==== Escape Sequenzen ==== | ||
+ | |||
+ | Der Parameter '' | ||
+ | |||
+ | **__Escape Zeichen: | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
Line 78: | Line 243: | ||
read b | read b | ||
- | if [ $operator = a ]; then | + | if [[ $operator = a ]]; then |
let c=a+b | let c=a+b | ||
| | ||
echo $a + $b = $c | echo $a + $b = $c | ||
- | elif [ $operator = s ]; then | + | elif [[ $operator = s ]]; then |
let c=a-b | let c=a-b | ||
| | ||
echo $a - $b = $c | echo $a - $b = $c | ||
- | elif [ $operator = m ]; then | + | elif [[ $operator = m ]]; then |
let c=a*b | let c=a*b | ||
| | ||
echo $a " | echo $a " | ||
- | elif [ $operator = d ]; then | + | elif [[ $operator = d ]]; then |
let c=a/b | let c=a/b | ||
| | ||
Line 250: | Line 415: | ||
</ | </ | ||
+ | |||
+ | |||
+ | ===== Bash Rules ===== | ||
+ | |||
+ | <WRAP center box 100%> | ||
+ | ==== Big Rules ==== | ||
+ | |||
+ | * Always double quote variables, including subshells. No naked '' | ||
+ | * This rule gets you pretty far. Read [[http:// | ||
+ | * All code goes in a function. Even if it's one function, '' | ||
+ | * Unless a library script, you can do global script settings and call '' | ||
+ | * Avoid global variables. Though when defining constants use '' | ||
+ | * Always have a '' | ||
+ | * If script is also usable as library, call it using '' | ||
+ | * Always use '' | ||
+ | * Exception being rare cases when you are intentionally setting a variable in an outer scope. | ||
+ | * Variable names should be lowercase unless exported to environment. | ||
+ | * Always use '' | ||
+ | * Use '' | ||
+ | * Never use deprecated style. Most notably: | ||
+ | * Define functions as '' | ||
+ | * Always use ''< | ||
+ | * Never use backticks, use '' | ||
+ | * See [[http:// | ||
+ | * Prefer absolute paths (leverage $PWD), always qualify relative paths with '' | ||
+ | * Always use '' | ||
+ | * Example: '' | ||
+ | * The exception is when defining variadic functions. See below. | ||
+ | * Use '' | ||
+ | * Warnings and errors should go to STDERR, anything parsable should go to STDOUT. | ||
+ | * Try to localize '' | ||
+ | |||
+ | //If you know what you're doing, you can bend or break some of these rules, but generally they will be right and be extremely helpful.// | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <WRAP center box 100%> | ||
+ | ==== Best Practices and Tips ==== | ||
+ | |||
+ | * Use Bash variable substitution if possible before awk/sed. | ||
+ | * Generally use double quotes unless it makes more sense to use single quotes. | ||
+ | * For simple conditionals, | ||
+ | * Don't be afraid of '' | ||
+ | * Put '' | ||
+ | * Skip '' | ||
+ | * Use '' | ||
+ | * Put complex one-liners of '' | ||
+ | * Good idea to include '' | ||
+ | * Design for simplicity and obvious usage. | ||
+ | * Avoid option flags and parsing, try optional environment variables instead. | ||
+ | * Use subcommands for necessary different " | ||
+ | * In large systems or for any CLI commands, add a description to functions. | ||
+ | * Use '' | ||
+ | * This can be queried/ | ||
+ | * Be conscious of the need for portability. Bash to run in a container can make more assumptions than Bash made to run on multiple platforms. | ||
+ | * When expecting or exporting environment, | ||
+ | * Use hard tabs. Heredocs ignore leading tabs, allowing better indentation. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | **Good References and Help**: | ||
+ | |||
+ | * [[http:// | ||
+ | * Especially [[http:// | ||
+ | * http:// | ||
+ | * Tips for interactive Bash: [[http:// | ||
+ | * For reference, Google' | ||
+ | </ | ||