meta data for this page
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
linux:bash [2017/04/24 06:50] – niziak | linux:bash [2022/11/30 09:04] – niziak | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Bash ====== | ====== Bash ====== | ||
+ | ===== rerun 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 | ||
+ | </ | ||
+ | |||
+ | ===== Output ===== | ||
+ | |||
+ | <code bash> | ||
+ | DEBUG() { | ||
+ | if [ $DEBUG_ENABLE -eq 1 ]; then | ||
+ | >&2 echo "DBG: $@" | ||
+ | fi | ||
+ | } | ||
+ | </ | ||
===== Variables with space ===== | ===== Variables with space ===== | ||
Line 9: | Line 35: | ||
# Use double colons to pass argument with spaces | # Use double colons to pass argument with spaces | ||
nice ionice -c idle btrfs filesystem defragment -v -r -czlib " | nice ionice -c idle btrfs filesystem defragment -v -r -czlib " | ||
+ | </ | ||
+ | |||
+ | Quote problematic characters to use in shell invocation: | ||
+ | <code bash> | ||
+ | QUOTED_VAR=" | ||
</ | </ | ||
Line 32: | Line 63: | ||
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 76: | Line 118: | ||
<code bash> | <code bash> | ||
- | DIR=" | + | # Works correctly if script is sourced from another one |
+ | DIR=" | ||
</ | </ | ||
<code bash> | <code bash> | ||
cur_file=" | cur_file=" | ||
- | cur_dir=" | + | cur_dir=" |
</ | </ | ||