2026-07-15
shell cheat sheet
basename ("a/b/c" -> "c")

${0##*/}


dirname ("a/b/c" -> "a/b")

${0%/*}


default value

${1:-"default"}


interactive mode

if [ -t 0 ]
then
fi


random 0s ... 10s delay in batch / non-interactive mode

if [ ! -t 0 ]
then
	sleep "`jot -r 1 0 10`"
fi


automatic script locking to avoid double execution

if [ "${1:-}" = lockf ]
then
	shift
else
	prog="${0##*/}"
	
	
	lockf -t 0 "tmp/$prog.lock" "./$prog" lockf $*
	
	
	exit
fi
created [1784142923] 2026-07-15 22:15:23 +0300/EEST, modified [1784142990] 2026-07-15 22:16:30 +0300/EEST