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 [2026/05/05 12:13] (current) niziak
Line 1: Line 1:
-=== reading in loop without subshell ===+====== Bash ====== 
 + 
 +===== re-run as other user ===== 
 <code bash> <code bash>
-var=0 +if [ $UID == 0 ]then 
-while read file +    exec su -c "$0" john 
-do  +fi
-  echo $filevar=1 +
-done < <(ls -1 /tmp/)+
 </code> </code>
  
-=== 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 "${TOOL}" &>/dev/nullthen 
-# 64+    echo " Command ${TOOL} not found. Please install it" 
 +    exit 
 +fi 
 +</code>
  
-# specify base method +and use it inside loop: 
-VAR=077echo $((10#$VAR+1)) +<code bash> 
-# 78+for tool in awk bc seddo 
 +    .... 
 +done 
 +</code>
  
-# strip leading zeros method +===== Output ===== 
-VAR=077; VAR=${VAR#0}; echo $(($VAR+1)) + 
-# 78+<code bash> 
 +DEBUG() { 
 +    if [ $DEBUG_ENABLE -eq 1 ]then 
 +    >&echo "DBG: $@" 
 +    fi 
 +}
 </code> </code>
  
-=== include/source other scripts === +===== Variables with space =====
-Possible test scenario: +
-  - call using from another directory full path /home/user/bin/myscripth.sh +
-  - add /home/user/bin to PATH and then call from another directory by "myscript.sh" +
-  - create symlink to script and call using symlink. Check for relative and absolute symlinks. +
-  - it should work also if it is sourced "source" or "."+
  
-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 to pass argument with spaces 
 +nice ionice -c idle btrfs filesystem defragment -v -r -czlib "${DST}" 
 +</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/other_script.sh+
 </code> </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>
-DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )+var=
 +while read file 
 +do  
 +  echo $file; var=1 
 +done < <(ls -1 /tmp/)
 </code> </code>
  
 +==== 'read' insid loop ====
 +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 "$SOURCE" )" && pwd )"+TTY=`tty` 
 +while read ... 
 +do 
 +  read CTRLC < ${TTY} 
 +done < somedata
 </code> </code>
  
 +
 +===== concatenate output (subshell) =====
 <code bash> <code bash>
-cur_file="${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" +( command1 ; command2 ; command3 ) | cat 
-cur_dir="$(dirname "${cur_file}")+command1 ; command2 ; command3 ; > outfile.txt
 </code> </code>
  
-''`BASH_SOURCE' +===== leading zeros / octal ===== 
-     An array variable whose members are the source filenames where the +To interpret a number as decimal, use 10#n form, eg10#09
-     corresponding shell function names in the `FUNCNAME' array +
-     variable are defined The shell function `${FUNCNAME[$i]}' is +
-     defined in the file `${BASH_SOURCE[$i]}' and called from +
-     `${BASH_SOURCE[$i+1]}' +
-''+
  
 +<code bash>
 +VAR=077; echo $(($VAR+1))
 +# 64
  
 +# specify base method
 +VAR=077; echo $((10#$VAR+1))
 +# 78
  
-=== double brackets [[ ]] ===+# strip leading zeros method 
 +VAR=077; VAR=${VAR#0}; echo $(($VAR+1)) 
 +# 78 
 +</code> 
 + 
 + 
 + 
 + 
 +===== 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 119:
 <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>
 +