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/03/15 11:10] – niziak | linux:bash [2026/05/05 12:13] (current) – niziak | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | === reading in loop without subshell | + | ====== |
| + | |||
| + | ===== re-run as other user ===== | ||
| <code bash> | <code bash> | ||
| - | var=0 | + | if [ $UID == 0 ]; then |
| - | while read file | + | exec su -c " |
| - | do | + | fi |
| - | echo $file; var=1 | + | |
| - | done < < | + | |
| </ | </ | ||
| - | === leading zeros / octal === | + | ===== Check if command is installed ===== |
| - | To interpret a number as decimal, use 10#n form, eg. 10#09 | + | |
| <code bash> | <code bash> | ||
| - | VAR=077; echo $(($VAR+1)) | + | if ! command -v " |
| - | # 64 | + | |
| + | exit 1 | ||
| + | fi | ||
| + | </ | ||
| - | # specify base method | + | and use it inside loop: |
| - | VAR=077; echo $((10# | + | <code bash> |
| - | # 78 | + | for tool in awk bc sed; do |
| + | .... | ||
| + | done | ||
| + | </ | ||
| - | # strip leading zeros method | + | ===== Output ===== |
| - | VAR=077; VAR=${VAR#0}; echo $(($VAR+1)) | + | |
| - | # 78 | + | <code bash> |
| + | DEBUG() | ||
| + | if [ $DEBUG_ENABLE -eq 1 ]; then | ||
| + | >& | ||
| + | fi | ||
| + | } | ||
| </ | </ | ||
| - | === include/ | + | ===== Variables with space ===== |
| - | 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/installer script which hardcode path to one shared script. | + | <code bash> |
| + | # Treat arguments as parts of one argument | ||
| + | declare DST=$* | ||
| + | |||
| + | # Use double colons | ||
| + | nice ionice -c idle btrfs filesystem defragment -v -r -czlib " | ||
| + | </code> | ||
| - | Another solutions: | + | Quote problematic characters to use in shell invocation: |
| <code bash> | <code bash> | ||
| - | #!/bin/sh | + | QUOTED_VAR="${VAR@Q}" |
| - | MY_DIR=$(dirname $(readlink -f $0)) | + | |
| - | $MY_DIR/ | + | |
| </ | </ | ||
| + | |||
| + | ===== 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> | <code bash> | ||
| - | DIR=$( cd "$( dirname " | + | 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> | <code bash> | ||
| - | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd -P "$( dirname " | + | TTY=`tty` |
| + | while read ... | ||
| + | do | ||
| + | read CTRLC < ${TTY} | ||
| + | done < somedata | ||
| </ | </ | ||
| + | |||
| + | ===== concatenate output (subshell) ===== | ||
| <code bash> | <code bash> | ||
| - | cur_file=" | + | ( command1 ; command2 ; command3 ) | cat |
| - | cur_dir=" | + | { command1 ; command2 ; command3 ; } > outfile.txt |
| </ | </ | ||
| - | '' | + | ===== leading zeros / octal ===== |
| - | An array variable whose members are the source filenames where the | + | 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 | ||
| - | === double brackets [[ ]] === | + | # strip leading zeros method |
| + | VAR=077; VAR=${VAR# | ||
| + | # 78 | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== double brackets [[ ]] ===== | ||
| [[http:// | [[http:// | ||
| Line 74: | Line 119: | ||
| < | < | ||
| - | === 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> | ||
| + | |||