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 [2016/03/15 11:10] niziaklinux:bash [2023/11/23 06:57] (current) niziak
Line 1: Line 1:
-=== reading in loop without subshell ===+====== 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> <code bash>
 var=0 var=0
Line 8: Line 72:
 </code> </code>
  
-=== leading zeros / octal ===+==== '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 To interpret a number as decimal, use 10#n form, eg. 10#09
  
Line 24: Line 105:
 </code> </code>
  
-=== include/source other scripts ===+===== 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 45: Line 126:
  
 <code bash> <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 bash> <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 63: 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 74: 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>
 +