Run start for $1=start, stop for $1=stop, and usage for any other value.
Which case statement is appropriate?
case WORD in PATTERN) ... ;; esac compares one value with multiple patterns; put * last for the default branch.
Detailed explanation
case $1 { start: run_start; stop: run_stop; *: usage; }Incorrect. C-style braces and colons are not shell case syntax.
case "$1" in start) run_start ;; stop) run_stop ;; *) usage ;; esacCorrect. It uses in, pattern parentheses, ;;, and esac correctly.
if "$1" in start|stop; then run; fiIncorrect. if does not use in in this form and the branches are not separated.
case "$1" then start run_start else usage fiIncorrect. It mixes case and if keywords into invalid syntax.
Try it yourself
An example you can run in a temporary verification environment.
bash --noprofile --norc -c 'case "$1" in start) printf start ;; stop) printf stop ;; *) printf usage ;; esac' demo stopExpected result
stopKey points
- in starts patterns
- ) ends a pattern
- ;; ends a branch
- esac closes case
Notes
- Environment: GNU Bash 5.2 / 短命シェル
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.