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 [2026/05/05 12:13] (current) – niziak | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | === leading zeros / octal === | + | ====== Bash ====== |
| - | To interpret a number | + | |
| + | ===== re-run | ||
| <code bash> | <code bash> | ||
| - | VAR=077; echo $(($VAR+1)) | + | if [ $UID == 0 ]; then |
| - | # 64 | + | exec su -c "$0" john |
| + | fi | ||
| + | </ | ||
| - | # specify base method | + | ===== Check if command is installed ===== |
| - | VAR=077; echo $((10# | + | |
| - | # 78 | + | |
| - | # strip leading zeros method | + | <code bash> |
| - | VAR=077; VAR=${VAR#0}; echo $(($VAR+1)) | + | if ! command -v "${TOOL}" &>/ |
| - | # 78 | + | |
| + | exit 1 | ||
| + | fi | ||
| </ | </ | ||
| - | === include/ | + | and use it inside loop: |
| - | Possible test scenario: | + | <code bash> |
| - | - call using from another directory full path / | + | for tool in awk bc sed; do |
| - | - add / | + | .... |
| - | - create symlink to script and call using symlink. Check for relative and absolute symlinks. | + | done |
| - | - it should work also if it is sourced " | + | </ |
| - | The best is to create setup/ | + | ===== Output ===== |
| - | Another solutions: | ||
| <code bash> | <code bash> | ||
| - | #!/bin/sh | + | DEBUG() { |
| - | MY_DIR=$(dirname $(readlink | + | if [ $DEBUG_ENABLE |
| - | $MY_DIR/ | + | >&2 echo " |
| + | fi | ||
| + | } | ||
| </ | </ | ||
| + | |||
| + | ===== Variables with space ===== | ||
| <code bash> | <code bash> | ||
| - | DIR=$( cd "$( dirname | + | # 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> | <code bash> | ||
| - | DIR="$( cd "$( dirname | + | QUOTED_VAR="${VAR@Q}" |
| </ | </ | ||
| + | |||
| + | ===== 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> | ||
| - | cur_file="${BASH_SOURCE[${# | + | var=0 |
| - | cur_dir="$(dirname "${cur_file}") | + | 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#0}; echo $(($VAR+1)) | ||
| + | # 78 | ||
| </ | </ | ||
| - | '' | ||
| - | An array variable whose members are the source filenames where the | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | '' | ||
| - | === double brackets [[ ]] === | + | ===== double brackets [[ ]] ===== |
| [[http:// | [[http:// | ||
| Line 65: | 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> | ||
| + | |||