A kiválasztott változat és az aktuális verzió közötti különbségek a következők.
| Előző változat mindkét oldalon Előző változat | |||
|
tudasbazis:linux:bash [2021.08.02 11:30] tia |
tudasbazis:linux:bash [2025.05.19 08:52] (aktuális) tia |
||
|---|---|---|---|
| Sor 1: | Sor 1: | ||
| + | ====== bash ====== | ||
| + | * http://kvz.io/blog/2013/11/21/bash-best-practices/ | ||
| + | * http://wiki.bash-hackers.org/commands/builtin/printf | ||
| + | * https://github.com/dylanaraps/pure-bash-bible | ||
| + | |||
| + | |||
| + | ===== Beállítások ===== | ||
| + | Alapvető hibakezelés: <code bash> | ||
| + | set -eufo pipefail | ||
| + | </code> | ||
| + | |||
| + | <blockquote> | ||
| + | ... | ||
| + | |||
| + | * 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. | ||
| + | |||
| + | ... | ||
| + | <cite>kvz.io</cite> | ||
| + | </blockquote> | ||
| + | |||
| + | |||
| + | ===== Billentyű parancsok ===== | ||
| + | * https://ss64.com/bash/syntax-keyboard.html | ||
| + | |||
| + | <note tip>Hasonlóan az [[tudasbazis:linux:emacs|Emacs]] billentyű parancsokhoz</note> | ||
| + | |||
| + | |||
| + | ===== Argumentumok kompakt kiíratása ===== | ||
| + | <code bash> | ||
| + | printf '"%b"\n' "$0" "$@" | nl -v0 -s": " | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== Karakter ismétlése sokszor ===== | ||
| + | * http://stackoverflow.com/q/5799303/1108919 | ||
| + | * http://stackoverflow.com/q/5349718/1108919 | ||
| + | | ||
| + | * 120 db szóköz: <code bash> | ||
| + | printf '%120s' | ||
| + | </code> | ||
| + | * 120 db tetszőleges karakter (pl. "=" jel): <code bash> | ||
| + | printf '%120s' | tr ' ' '=' | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== Alapértelmezett érték ===== | ||
| + | * http://stackoverflow.com/a/26899206/1108919 | ||
| + | * http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion | ||
| + | |||
| + | * Ha nincs deklarálva a ''$parameter'' változó, akkor legyen lecserélve a "default" szóra: <code bash> | ||
| + | echo "${parameter='default'}" | ||
| + | </code> | ||
| + | * Ha nincs deklarálva, vagy üres string a ''$parameter'' változó, akkor legyen lecserélve a "default" szóra: <code bash> | ||
| + | echo "${parameter:='default'}" | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== Változók, tömbök ===== | ||
| + | * http://unix.stackexchange.com/a/122848/21185 | ||
| + | * http://www.thegeekstuff.com/2010/06/bash-array-tutorial | ||
| + | |||
| + | ^ ^ ''$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 ===== | ||
| + | <code bash> | ||
| + | exec {out_fd}>/tmp/en_fajlom | ||
| + | |||
| + | echo "${out_fd}" | ||
| + | #például 10 | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== Pseudo fájlok ===== | ||
| + | * http://unix.stackexchange.com/a/64011/21185 | ||
| + | * http://tldp.org/LDP/abs/html/process-sub.html | ||
| + | |||
| + | Parancs kimenetének használata, mint fájl leíró | ||
| + | |||
| + | * <code bash> | ||
| + | cat <( date ) | ||
| + | </code> | ||
| + | * <code bash> | ||
| + | date >( cat ) | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== Mókás példa ===== | ||
| + | * https://gist.github.com/andras-tim/f8aebf9243cecf3719d27d020a718ef8 | ||
| + | |||
| + | TODO: <code> | ||
| + | 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 | | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== Háttérben futtatás ===== | ||
| + | * https://unix.stackexchange.com/questions/3886/difference-between-nohup-disown-and | ||
| + | |||
| + | * Háttérben indítás: <code bash> | ||
| + | # a szülő process megszűnésekor a parancs is leáll | ||
| + | command & | ||
| + | |||
| + | # a szülő process megszűnésekor a parancs tovább él | ||
| + | command &! | ||
| + | # == | ||
| + | nohup command | ||
| + | </code> | ||
| + | * Előtérben indított program háttérbe szüneteltetése: <key>Ctrl+Z</key> | ||
| + | * Szüneteltetett program kezeléseÉ <code bash> | ||
| + | # folytatás előtérben | ||
| + | fg | ||
| + | |||
| + | # folytatás háttérben | ||
| + | bg | ||
| + | </code> | ||
| + | * Háttérben indított program utólagos leválasztás a szülőről: <code bash> | ||
| + | disown | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ==== Escape ==== | ||
| + | * https://stackoverflow.com/a/41940626/1108919 | ||