Bash Tricks
Three-Fingered Claw technique
These functions are *NIX OS and shell flavor-robust. Put them at the beginning of your script (bash or otherwise), try() your statement and code on.
Explanation (based on flying sheep comment).
yell: print the script name and all arguments to stderr:
$0 is the path to the script ;
$* are all arguments.
>&2 means > redirect stdout to & pipe 2. pipe 1 would be stdout itself.
die does the same as yell, but exits with a non-0 exit status, which means “fail”.
try uses the || (boolean OR), which only evaluates the right side if the left one failed.
$@ is all arguments again, but different.
yell() { echo "$0: $*" >&2; }
die() { yell "$*"; exit 111; }
try() { "$@" || die "cannot $*"; }Pipe Output to Arguments
Here is a short-liner which will prepend piped arguments to your script arguments list:
#!/bin/bash
args=$@
[[ -p /dev/stdin ]] && { mapfile -t; set -- "${MAPFILE[@]}"; set -- $@ $args; }
echo $@Example use:
Renaming files with mv
Linking
Hard Link
Soft/Symbolic Link
Soft linking is useful for adding programs to in PATH directories e.g. ~/bin/. Soft links are created with the ln command. For example, the following would create a soft link named link1 to a file named file1, both in the current directory
Unzip multiple ZIP files into their own directories
Last updated
Was this helpful?