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/08/10 08:10] niziaklinux:bash [2026/05/05 12:13] (current) niziak
Line 1: Line 1:
-=== leading zeros / octal === +====== Bash ====== 
-To interpret a number as decimal, use 10#n form, eg. 10#09+ 
 +===== re-run as other user =====
  
 <code bash> <code bash>
-VAR=077echo $(($VAR+1)) +if [ $UID == 0 ]then 
-# 64+    exec su -c "$0" john 
 +fi 
 +</code>
  
-# specify base method +===== Check if command is installed =====
-VAR=077; echo $((10#$VAR+1)) +
-# 78+
  
-# strip leading zeros method +<code bash> 
-VAR=077; VAR=${VAR#0}; echo $(($VAR+1)) +if ! command -v "${TOOL}" &>/dev/nullthen 
-# 78+    echo " Command ${TOOL} not found. Please install it" 
 +    exit 
 +fi
 </code> </code>
  
-=== include/source other scripts === +and use it inside loop
-Possible test scenario+<code bash> 
-  - call using from another directory full path /home/user/bin/myscripth.sh +for tool in awk bc sed; do 
-  - add /home/user/bin to PATH and then call from another directory by "myscript.sh" +    .... 
-  - create symlink to script and call using symlinkCheck for relative and absolute symlinks+done 
-  - it should work also if it is sourced "source" or "."+</code>
  
-The best is to create setup/installer script which hardcode path to one shared script.+===== Output =====
  
-Another solutions: 
 <code bash> <code bash>
-#!/bin/sh +DEBUG() { 
-MY_DIR=$(dirname $(readlink -$0)) +    if [ $DEBUG_ENABLE -eq 1 ]; then 
-$MY_DIR/other_script.sh+    >&2 echo "DBG: $@" 
 +    fi 
 +}
 </code> </code>
 +
 +===== Variables with space =====
  
 <code bash> <code bash>
-DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )+# 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> </code>
  
 +Quote problematic characters to use in shell invocation:
 <code bash> <code bash>
-DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd -P "$( dirname "$SOURCE" )" && pwd )"+  QUOTED_VAR="${VAR@Q}"
 </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>
-cur_file="${BASH_SOURCE[${#BASH_SOURCE[@]- 1]}" +var=
-cur_dir="$(dirname "${cur_file}")+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> </code>
  
-''`BASH_SOURCE' 
-     An array variable whose members are the source filenames where the 
-     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]}' 
-'' 
  
  
  
-=== 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 65: 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>
 +