- 01Setting up (what you'll need)
- 02Your first shell script (echo)
- 03Using variables
- 04Command-line arguments ($1, $2 ...)
- 05Branching (the if statement)
- 06Repeating (the for loop)
- 07Repeating (the while loop)
- 08Writing your own function
- 09Working with arrays
- 10Command substitution ($())
- 11Working with strings (parameter expansion)
- 12Reading from standard input (read)
- 13Exit status and && / ||
- 14Pipes and redirection
- 15The case statement
- 16Arithmetic ($(( )))
- 17The basics of grep, sed and awk
- 18Argument checks and error handling
- 19Function return values and exit codes
- 20[Project] Build a small log-tallying script
Argument checks and error handling
This lesson covers the basics of argument checking and error handling — what makes a script safe to run.
The foundation of a safe script is to check the argument count and, if it is not what you expected, print a message and stop: if [ $# -eq 0 ]; then ... exit 1; fi. The number given to exit reaches the caller as the exit status.
The sample code shows the classic shape for the case where a required argument was not supplied: print an error message and stop abnormally with exit 1.
A common early stumble is writing no error checking at all and diving straight into the real work. A script that behaves mysteriously, with no error message, when given unexpected input is a reliable source of trouble in production.
In professional work, making sure a failure is noticed immediately is treated as central to running automation scripts safely.
๐งช This site can't compile or run Shell Script directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
