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 [2021/04/02 11:18] niziaklinux:bash [2023/11/23 06:57] (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 ===== ===== Output =====
Line 19: Line 43:
 # Use double colons to pass argument with spaces # Use double colons to pass argument with spaces
 nice ionice -c idle btrfs filesystem defragment -v -r -czlib "${DST}" 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> </code>
  
Line 97: 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>