Below you will find pages that utilize the taxonomy term “Shell”
Blogs
read more
Shell - Switch Statements
Recently I had to work a bit more than usual on shell scripting, and had to do a bunch of switches. I used this also in the scripts for this project. A simplified impression shows how we can match user input with supported commands:
#!/bin/sh
CMD=$1
case "$CMD" in
"run") echo "cmdRun" ;;
"publish") echo "cmdPublish" ;;
*) echo "Unknown command $CMD"; exit 1 ;;
esac
This way we can match the provided input with supported cases ("run"
and "publish"
), and error otherwise (*
). The double semicolon (;;
) marks the end of a case.