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 [2015/08/10 08:10] – niziak | linux:bash [2023/11/23 06:57] (current) – niziak | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | === leading zeros / octal === | + | ====== 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 ===== | ||
| + | |||
| + | <code bash> | ||
| + | If parameter is unset or null, the expansion of word is substituted. | ||
| + |     Otherwise, the value of parameter is substituted.</ | ||
| + | |||
| + | <code bash> | ||
| + | |||
| + | Or, which will assign to VARIABLE as well: | ||
| + | |||
| + | < | ||
| + | |||
| + | ===== reading in loop without subshell ===== | ||
| + | <code bash> | ||
| + | var=0 | ||
| + | while read file | ||
| + | do | ||
| + | echo $file; var=1 | ||
| + | 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) ===== | ||
| + | <code bash> | ||
| + | ( command1 ; command2 ; command3 ) | cat | ||
| + | { command1 ; command2 ; command3 ; } > outfile.txt | ||
| + | </ | ||
| + | |||
| + | ===== leading zeros / octal ===== | ||
| To interpret a number as decimal, use 10#n form, eg. 10#09 | To interpret a number as decimal, use 10#n form, eg. 10#09 | ||
| Line 15: | Line 105: | ||
| </ | </ | ||
| - | === include/ | + | ===== include/ | 
| Possible test scenario: | Possible test scenario: | ||
|   - call using from another directory full path / |   - call using from another directory full path / | ||
| Line 25: | Line 115: | ||
| Another solutions: | Another solutions: | ||
| - | < | + | < | 
| #!/bin/sh | #!/bin/sh | ||
| MY_DIR=$(dirname $(readlink -f $0)) | MY_DIR=$(dirname $(readlink -f $0)) | ||
| Line 31: | Line 121: | ||
| </ | </ | ||
| - | < | + | < | 
| DIR=$( cd "$( dirname " | DIR=$( cd "$( dirname " | ||
| </ | </ | ||
| - | < | + | < | 
| - | DIR=" | + | # Works correctly if script is sourced from another one | 
| + | DIR=" | ||
| </ | </ | ||
| - | < | + | < | 
| cur_file=" | cur_file=" | ||
| - | cur_dir=" | + | cur_dir=" | 
| </ | </ | ||
| Line 54: | Line 145: | ||
| - | === double brackets [[ ]] === | + | ===== double brackets [[ ]] ===== | 
| [[http:// | [[http:// | ||
| Line 65: | Line 156: | ||
| < | < | ||
| - | === Check if string contains another string === | + | ===== Check if string contains another string  | 
| not compatible with sh | not compatible with sh | ||
| < | < | ||
| < | < | ||
| + | |||
| + | |||
| + | ==== Execute command until fail ==== | ||
| + | <code bash> | ||
| + | |||