Bash & shell programming
Table of Contents
- shell programming bash
- bash boolean return value
- bash multi-line string varible
- bash iterate over array
- assert variable is defined {double square brackets -v} operator bashhabit
- bash get current script directory
- avoid passing flags in shebang (e.g.
#!/bin/bash -eu
) bashhabit
- interactive tricks & tips shell
- [A]
diff <(ls dir1) <(ls dir2)
shell - [B] use sigquit {Ctrl-\} to kill processes that are really stuck (when Ctrl-C isn't working) shellhabit
- [B] exec command on each file
find . -exec some command {} \;
shelldrill - TODO exec command in parallel
find /path -iname '*.pdf' -print0 | xargs -0 -n1 -i -P8 bash -c "echo 'Processing {}' && command '{}'"
shelldrill - [C]
ps aufx
to list all processes shell - [C] tree -pufi for file tree with permissions shellhabit
- [C] pretty print json
cat file.json | python -mjson.tool
shelljson - [D] detect if library is installed
ldconfig -p | grep libjpeg
orgcc -ljpeg
shell - [D] merging pdfs shell
- [D] {ln -i} for interactive symlink (prompts for removal) shellhabit
- [A]
- TODO [C] use whiptail for ncurses dialogs tuihabit
- bashhabit Where is the bash feature to open a command in $EDITOR documented? - Unix & Linux Stack Exchange
¶shell programming bash
¶ boolean return value bash
function whatever { if (x) then true else false fi } if whatever then ...
¶ multi-line string varible bash
MULTILINE=$(cat <<-END here goes multi line END )
¶ iterate over array bash
EXTRA_STOWS=("home-arbtt" "home-emacs" "home-anacron") for s in "${EXTRA_STOWS[@]}"
¶assert variable is defined {double square brackets -v} operator bashhabit
¶ get current script directory bash
DIR=$(dirname "$(readlink -f "$0")")
(might not work on mac)
¶avoid passing flags in shebang (e.g. #!/bin/bash -eu
) bashhabit
CREATED: [2018-06-21]
e.g. if you specify other flags in terminal, they would override everything
instead, better to use if [[ $0 == "$BASH_SOURCE" ]]; then set -eux; fi
¶interactive tricks & tips shell
priorities roughly indicate how useful I find each command
¶[A] diff <(ls dir1) <(ls dir2)
shell
¶[B] use sigquit {Ctrl-\} to kill processes that are really stuck (when Ctrl-C isn't working) shellhabit
CREATED: [2018-03-05]
¶[B] exec command on each file find . -exec some command {} \;
shelldrill
CREATED: [2019-06-19]
¶TODO exec command in parallel find /path -iname '*.pdf' -print0 | xargs -0 -n1 -i -P8 bash -c "echo 'Processing {}' && command '{}'"
shelldrill
CREATED: [2019-06-20]
¶[C] ps aufx
to list all processes shell
¶[C] tree -pufi for file tree with permissions shellhabit
CREATED: [2018-09-18]
¶[C] pretty print json cat file.json | python -mjson.tool
shelljson
¶[D] detect if library is installed ldconfig -p | grep libjpeg
or gcc -ljpeg
shell
¶[D] merging pdfs shell
pdftk out.pdf j1-process.pdf j1-process-2.pdf j1-process-3.pdf cat output merged.pdf pdftk full-pdf.pdf cat 1 1 1 2 3 4 12-15 output outfile_p12-15.pdf
¶[D] {ln -i} for interactive symlink (prompts for removal) shellhabit
CREATED: [2018-03-01]
¶TODO [C] use whiptail for ncurses dialogs tuihabit
¶ Where is the bash feature to open a command in $EDITOR documented? - Unix & Linux Stack Exchange bashhabit
edit-and-execute-command (C-xC-e)