meta data for this page
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| linux:bash [2016/11/22 22:08] – niziak | linux:bash [2026/05/05 12:13] (current) – niziak | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Bash ====== | ====== Bash ====== | ||
| + | |||
| + | ===== re-run as other user ===== | ||
| + | |||
| + | <code bash> | ||
| + | if [ $UID == 0 ]; then | ||
| + | exec su -c " | ||
| + | fi | ||
| + | </ | ||
| + | |||
| + | ===== Check if command is installed ===== | ||
| + | |||
| + | <code bash> | ||
| + | if ! command -v " | ||
| + | echo " Command ${TOOL} not found. Please install it" | ||
| + | exit 1 | ||
| + | fi | ||
| + | </ | ||
| + | |||
| + | and use it inside loop: | ||
| + | <code bash> | ||
| + | for tool in awk bc sed; do | ||
| + | .... | ||
| + | done | ||
| + | </ | ||
| + | |||
| + | ===== Output ===== | ||
| + | |||
| + | <code bash> | ||
| + | DEBUG() { | ||
| + | if [ $DEBUG_ENABLE -eq 1 ]; then | ||
| + | >&2 echo "DBG: $@" | ||
| + | fi | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Variables with space ===== | ||
| + | |||
| + | <code bash> | ||
| + | # Treat arguments as parts of one argument | ||
| + | declare DST=$* | ||
| + | |||
| + | # Use double colons to pass argument with spaces | ||
| + | nice ionice -c idle btrfs filesystem defragment -v -r -czlib " | ||
| + | </ | ||
| + | |||
| + | Quote problematic characters to use in shell invocation: | ||
| + | <code bash> | ||
| + | QUOTED_VAR=" | ||
| + | </ | ||
| + | |||
| ===== Default values ===== | ===== Default values ===== | ||
| Line 21: | Line 71: | ||
| done < <(ls -1 /tmp/) | done < <(ls -1 /tmp/) | ||
| </ | </ | ||
| + | |||
| + | ==== ' | ||
| + | In loop where stdin is redirected the easiest way to use read is: | ||
| + | <code bash> | ||
| + | TTY=`tty` | ||
| + | while read ... | ||
| + | do | ||
| + | read CTRLC < ${TTY} | ||
| + | done < somedata | ||
| + | </ | ||
| + | |||
| ===== concatenate output (subshell) ===== | ===== concatenate output (subshell) ===== | ||
| Line 44: | Line 105: | ||
| </ | </ | ||
| - | ===== include/ | ||
| - | Possible test scenario: | ||
| - | - call using from another directory full path / | ||
| - | - add / | ||
| - | - create symlink to script and call using symlink. Check for relative and absolute symlinks. | ||
| - | - it should work also if it is sourced " | ||
| - | |||
| - | The best is to create setup/ | ||
| - | |||
| - | Another solutions: | ||
| - | <code bash> | ||
| - | #!/bin/sh | ||
| - | MY_DIR=$(dirname $(readlink -f $0)) | ||
| - | $MY_DIR/ | ||
| - | </ | ||
| - | |||
| - | <code bash> | ||
| - | DIR=$( cd "$( dirname " | ||
| - | </ | ||
| - | |||
| - | <code bash> | ||
| - | DIR=" | ||
| - | </ | ||
| - | |||
| - | <code bash> | ||
| - | cur_file=" | ||
| - | cur_dir=" | ||
| - | </ | ||
| - | |||
| - | '' | ||
| - | An array variable whose members are the source filenames where the | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | '' | ||
| Line 98: | Line 123: | ||
| < | < | ||
| < | < | ||
| + | |||
| + | |||
| + | ==== Execute command until fail ==== | ||
| + | <code bash> | ||
| + | |||