Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:shell [2024/04/19 09:45]
cyril
linux:shell [2024/04/22 00:07]
cyril [Misc]
Line 62: Line 62:
  
 search_dir "." search_dir "."
 +</code>
 +
 +===== Misc =====
 +
 +  * **Default value for variable**: <code bash>
 +${<variable>:-<default-value>}
 +</code>
 +  * **Get directory of current script** (for instance in order to run a script that is stored in the same directory, or in a known relative path):<code bash>
 +BASEDIR=$(dirname "$0")
 +</code>
 +  * **echo to stderr**, just redirect the output:<code bash>
 +echo "foo" 1>&2
 +</code>
 +  * **Convert dash-time to ISO** (2024-04-21_15-50-30 to 2024-04-21T15:50:30, for instance to use it as input for ''date''): <code bash>
 +sed -r "s/([0-9]{4})-([0-9]{2})-([0-9]{2})_([0-9]{2})-([0-9]{2})-([0-9]{2})/\1-\2\-3T\4:\5:\6/"
 +</code>
 +  * **Get target name of symbolic link**:<code bash>
 +readlink <symbolic-link>
 +</code>
 +  * **Execute command**:<code bash>
 +var=`cat file`
 +var=$(cat file)
 +</code>
 +  * **Evaluate mathematical expression** (''zsh'' also supports floating-point operations):<code bash>
 +echo $((2*3))
 +</code>
 +  * **time with redirected output** (also works for groups of commands):<code bash>
 +time (cat file1 > file2)
 +time (cat file1 > /dev/null; sync)
 +</code>
 +  * **Check number of arguments and print usage:**<code bash>
 +if [[ $# -lt 2 ]]; then
 +        echo "Usage: foo.sh <arg1> <arg2>" 1>&2
 +        exit 1
 +fi
 +</code>
 +  * Load command output as a file:<code>
 +diff <(grep x file1) <(grep y file2)
 </code> </code>
linux/shell.txt ยท Last modified: 2024/04/22 00:07 by cyril
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0