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/21 13:43]
cyril
linux:shell [2024/12/11 09:40] (current)
cyril [Misc]
Line 72: Line 72:
 BASEDIR=$(dirname "$0") BASEDIR=$(dirname "$0")
 </code> </code>
-  * **echo to stderr**, just redirect the output:<code>+  * **echo to stderr**, just redirect the output:<code bash>
 echo "foo" 1>&2 echo "foo" 1>&2
 </code> </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>
 +  * **Remove extension from file name:**<code>
 +${f%.png} # with all shells
 +${f:r}    # only zsh
 +</code>
 +
 +
linux/shell.1713707037.txt.gz ยท Last modified: 2024/04/21 13:43 by cyril
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0