shellscript
Table of Contents
- link
- Shell or cli
1. command exists
if ! [ -x "$(command -v git)" ]; then echo "Error: $cmd is not installed." >&2 exit 1 fi
2. remove trail slash
tmp=/a/b/c/ echo ${tmp%/}
3. define function
f() { # use `echo` like return echo "$1 $2" } a=$(f hello sh) echo $a
4. string contains
a="hello-world" if [[ $a =~ "-" ]]; then echo "a contains '-'" fi
5. get file name
path=/a/b/c/d.txt nameWithSuffix="${path##*/}" echo "with suffix:\t$nameWithSuffix" nameWithoutSuffix="${nameWithSuffix%.*}" echo "without suffix:\t$nameWithoutSuffix"
6. file not exist
if [ ! -f "/a/b/c/a" ]; then echo "File doesn't exist." fi