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/04/16 16:06] – [Default values] niziaklinux:bash [2026/05/05 12:13] (current) niziak
Line 1: Line 1:
 ====== Bash ====== ====== 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 ===== ===== Default values =====
Line 20: Line 70:
   echo $file; var=1   echo $file; var=1
 done < <(ls -1 /tmp/) 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> </code>
  
Line 38: Line 105:
 </code> </code>
  
-===== include/source other scripts ===== 
-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. 
- 
-Another solutions: 
-<code bash> 
-#!/bin/sh 
-MY_DIR=$(dirname $(readlink -f $0)) 
-$MY_DIR/other_script.sh 
-</code> 
- 
-<code bash> 
-DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) 
-</code> 
- 
-<code bash> 
-DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd -P "$( dirname "$SOURCE" )" && pwd )" 
-</code> 
- 
-<code bash> 
-cur_file="${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" 
-cur_dir="$(dirname "${cur_file}") 
-</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]}' 
-'' 
  
  
Line 92: Line 123:
 <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>
 +