Tartalomjegyzék

bash

Beállítások

Alapvető hibakezelés:

set -eufo pipefail

  • Use set -o errexit (a.k.a. set -e) to make your script exit when a command fails.
  • Then add || true to commands that you allow to fail.
  • Use set -o nounset (a.k.a. set -u) to exit when your script tries to use undeclared variables.
  • Use set -o xtrace (a.k.a set -x) to trace what gets executed. Useful for debugging.
  • Use set -o pipefail in scripts to catch mysqldump fails in e.g. mysqldump |gzip. The exit status of the last command that threw a non-zero exit code is returned.

kvz.io

Billentyű parancsok

Hasonlóan az Emacs billentyű parancsokhoz

Argumentumok kompakt kiíratása

printf '"%b"\n' "$0" "$@" | nl -v0 -s": "

Karakter ismétlése sokszor

Alapértelmezett érték

Változók, tömbök

$param != null $param == null unset param
${param:-word} $param word word
${param:=word} $param =word =word
${param:?word} $param hiba hiba
${param:+word} $param null null

Fájl nyitása úgy, hogy az FD-jét elkérjük

exec {out_fd}>/tmp/en_fajlom
 
echo "${out_fd}"
#például 10

Pseudo fájlok

Parancs kimenetének használata, mint fájl leíró

Mókás példa

TODO:

Solution:

| Component                     | Explanation            
| ------------- | --------------- | ------ |                                                                         | Note |
| `echo 2 >a`                   | Write `2` to file **a**.                                                                        | Creates **a** file with `2` content |
| `<(echo 3)`                   | Run `echo 3` command in a sub-shell and get the path of the it's stdout fd (e.g. `/dev/fd/63`). | Creates a subshell |
| `exec {b}< /dev/fd/63`        | ???                                                                                                | ??? |
| `cat -`                        | `cat` reads from stdin instead of a file and writes everything to stdout                                                     ||
| `echo 1 | cat -`              | stdout of `echo 1` piped to `cat`                             | #1 input of `cat` |
| `cat - <a`                    | Read **a** file into stdin of `cat`                                                         | #2 input of `cat` |
| `cat - <&${b}`                | ??? | #3 input of `cat` |
| ```
cat - <<-EOF
    4
EOF
``` | Read multiline string while `EOF` with removal of <TAB> indents | #4 input of `cat` |
| `cat - <<<5`                    | Write `5` to stdin of `cat` | #5 input of `cat` |
| `cat 2>&1`                | merge stderr of cat into it's stdout |
| `cat >c`                    | `cat` writes the input data to file **c** | creates **c** file |
| `cat >|d`                    | `cat` writes the input data to file **d** if the **d** is not existed ??? | creates **d** file |
| `cat >>e`                    | `cat` appends the input data to file **e** | creates **e** file |

Háttérben futtatás

Escape