--long options and help, logging, a cleanup trap, a lock so only one copy runs, retries, a dry-run switch, a yes/no prompt, a safe file loop, a config file reader and parallel jobs. Every piece is explained, and the scripts it builds pass ShellCheck.Then: chmod +x myscript.sh && ./myscript.sh --help
((n++)) can stop a set -e script. When n is 0 the expression is “false” and returns 1. Use n=$((n + 1)).local x=$(cmd) hides a failure. The exit code is the one from local, which always succeeds. Write local x on one line and x=$(cmd) on the next.cmd | while read … loses its variables. The loop runs in a subshell. Use while …; done < <(cmd) instead.for f in $(ls) breaks on spaces. Use a glob, for f in /path/*, or find … -print0 with read -d ''.rm $file with a file called “a b” removes a and b. Always write "$file" and "$@".read without -r eats backslashes. And without IFS= it trims spaces at both ends of the line.cd can fail. Under set -e the script stops; without it, the next commands run in the wrong folder. Write cd /path || exit in scripts without set -e.if, while, &&/|| lists and functions called from them, and in command substitutions. It’s a safety net, not error handling.wait -n or ${x,,} there, and no flock. The pieces here work in bash 3.2 except flock.Tick the pieces a script needs, such as option parsing, a lock so only one copy runs, retries or a dry-run switch, and get one complete script with them wired together in the right order. Each part is explained below the script, and the scripts it builds pass ShellCheck.
-e stops the script when a command fails, -u stops it when it uses a variable that was never set, and -o pipefail makes a pipeline fail when any command in it fails, not just the last. It’s a safety net with gaps: failures inside if tests and &&/|| lists don’t stop the script.
The built-in getopts only handles short options like -v. For --long options, loop over the arguments with while (( $# )); do case $1 in … esac; shift; done, which also lets you accept --name=value. The external getopt command can do it too, but the one on macOS can’t.
Use flock: exec 9>>/run/lock/myscript.lock then flock -n 9 || exit 1. The kernel releases the lock as soon as the script ends, even after a crash, so there’s no stale PID file to clean up. In a crontab, flock -n /tmp/job.lock /path/to/script does the same.
Make a private folder with tmp=$(mktemp -d) and add trap 'rm -rf -- "$tmp"' EXIT. The EXIT trap runs when the script finishes, fails, or is stopped with Ctrl+C or kill. Nothing runs after kill -9.
Wrap it in a function with an until loop that sleeps and doubles the wait after each failure, and gives up after a set number of tries. Count with n=$((n + 1)) rather than ((n++)): when n is 0, ((n++)) returns 1 and set -e ends the script.
Related tools: Cron Orchestrator · PowerShell ↔ Bash Translator · All tools A–Z
Most tools run entirely in your browser. Tools marked 📡 contact outside services, as listed in the Privacy Policy. Review commands before you run them; see the Terms.