meta data for this page
  •  

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
linux:bash [2015/04/15 08:46] niziaklinux:bash [2023/11/23 06:57] (current) niziak
Line 1: Line 1:
-== include/source other scripts ==+====== Bash ====== 
 + 
 +===== re-run as other user ===== 
 + 
 +<code bash> 
 +if [ $UID == 0 ]; then 
 +    exec su -c "$0" john 
 +fi 
 +</code> 
 + 
 +===== Check if command is installed ===== 
 + 
 +<code bash> 
 +if ! command -v "${TOOL}" &>/dev/null; then 
 +    echo " Command ${TOOL} not found. Please install it" 
 +    exit 1 
 +fi 
 +</code> 
 + 
 +and use it inside loop: 
 +<code bash> 
 +for tool in awk bc sed; do 
 +    .... 
 +done 
 +</code> 
 + 
 +===== Output ===== 
 + 
 +<code bash> 
 +DEBUG() { 
 +    if [ $DEBUG_ENABLE -eq 1 ]; then 
 +    >&2 echo "DBG: $@" 
 +    fi 
 +
 +</code> 
 + 
 +===== 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 "${DST}" 
 +</code> 
 + 
 +Quote problematic characters to use in shell invocation: 
 +<code bash> 
 +  QUOTED_VAR="${VAR@Q}" 
 +</code> 
 + 
 + 
 +===== Default values ===== 
 + 
 +<code bash>${parameter:-word} 
 +    If parameter is unset or null, the expansion of word is substituted.  
 +    Otherwise, the value of parameter is substituted.</code> 
 +     
 +<code bash>FOO=${VARIABLE:-default}</code> 
 + 
 +Or, which will assign to VARIABLE as well: 
 + 
 +<code>FOO=${VARIABLE:=default}</code> 
 + 
 +===== reading in loop without subshell ===== 
 +<code bash> 
 +var=0 
 +while read file 
 +do  
 +  echo $file; var=1 
 +done < <(ls -1 /tmp/) 
 +</code> 
 + 
 +==== 'read' insid loop ==== 
 +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 
 +</code> 
 + 
 + 
 +===== concatenate output (subshell) ===== 
 +<code bash> 
 +( command1 ; command2 ; command3 ) | cat 
 +{ command1 ; command2 ; command3 ; } > outfile.txt 
 +</code> 
 + 
 +===== 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#$VAR+1)) 
 +# 78 
 + 
 +# strip leading zeros method 
 +VAR=077; VAR=${VAR#0}; echo $(($VAR+1)) 
 +# 78 
 +</code> 
 + 
 +===== include/source other scripts =====
 Possible test scenario: Possible test scenario:
   - call using from another directory full path /home/user/bin/myscripth.sh   - call using from another directory full path /home/user/bin/myscripth.sh
Line 9: Line 115:
  
 Another solutions: Another solutions:
-<code>+<code bash>
 #!/bin/sh #!/bin/sh
 MY_DIR=$(dirname $(readlink -f $0)) MY_DIR=$(dirname $(readlink -f $0))
Line 15: Line 121:
 </code> </code>
  
-<code>+<code bash>
 DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
 </code> </code>
  
-<code> +<code bash> 
-DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd -P "$( dirname "$SOURCE" )" && pwd )"+# Works correctly if script is sourced from another one 
 +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
 </code> </code>
  
-<code>+<code bash>
 cur_file="${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" cur_file="${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}"
-cur_dir="$(dirname "${cur_file}")+cur_dir="$(dirname "${cur_file}")"
 </code> </code>
  
Line 38: Line 145:
  
  
-== double brackets [[ ]] ==+===== double brackets [[ ]] =====
 [[http://tldp.org/LDP/abs/html/testconstructs.html#DBLBRACKETS|DBLBRACKETS]] [[http://tldp.org/LDP/abs/html/testconstructs.html#DBLBRACKETS|DBLBRACKETS]]
  
Line 49: Line 156:
 <code>[ -e "$file_name" ]</code> <code>[ -e "$file_name" ]</code>
  
-== Check if string contains another string ==+===== Check if string contains another string =====
 not compatible with sh not compatible with sh
 <code>if [[ $string == *"word1 word2"* ]] ...</code> <code>if [[ $string == *"word1 word2"* ]] ...</code>
 <code>if [[ $string =~ .*word.* ]] ...</code> <code>if [[ $string =~ .*word.* ]] ...</code>
 +
 +
 +==== Execute command until fail ====
 +<code bash>while command; do :; done</code>
 +