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/04/15 08:46] – niziak | linux:bash [2023/11/23 06:57] (current) – niziak | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | == include/ | + | ====== 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 | ||
+ | |||
+ | <code bash> | ||
+ | VAR=077; echo $(($VAR+1)) | ||
+ | # 64 | ||
+ | |||
+ | # specify base method | ||
+ | VAR=077; echo $((10# | ||
+ | # 78 | ||
+ | |||
+ | # strip leading zeros method | ||
+ | VAR=077; VAR=${VAR# | ||
+ | # 78 | ||
+ | </ | ||
+ | |||
+ | ===== include/ | ||
Possible test scenario: | Possible test scenario: | ||
- call using from another directory full path / | - call using from another directory full path / | ||
Line 9: | Line 115: | ||
Another solutions: | Another solutions: | ||
- | < | + | < |
#!/bin/sh | #!/bin/sh | ||
MY_DIR=$(dirname $(readlink -f $0)) | MY_DIR=$(dirname $(readlink -f $0)) | ||
Line 15: | 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 38: | Line 145: | ||
- | == double brackets [[ ]] == | + | ===== double brackets [[ ]] ===== |
[[http:// | [[http:// | ||
Line 49: | 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> | ||
+ |