Linux Basic — Interactive Scripts Practice
Practice Exercises — Interactive Scripts
Practice writing small interactive helpers using read, select, getopts, and the confirmation pattern.
Warm-up
- Prompt for a username and echo it back.
- Prompt for a password silently (
read -s) and confirm it by asking twice. - Prompt with a 10-second timeout that defaults to
noon timeout.
Validation
- Ask for a port number and re-prompt up to 3 times if invalid.
- Read a colon-separated record into
user:uid:homewithIFS=:andread.
Menus
- Write a
selectmenu to choose a service (start,stop,status) and run the associated command. - Use
selectwith an array populated fromls /opt/tools.
Options & Flags
- Make a small script that accepts
-f(force) and-n COUNTusinggetopts. - Mix
--helpas a long option handled beforegetopts.
Confirmation
- Implement a
confirmwrapper and use it to protect arm -rfsimulation.
Verification Checklist
- All secrets use
read -s - Timeouts have defaults
getoptsusesshift $((OPTIND-1))to expose remaining argsselecthandles invalid numeric choices gracefully
Next chapter → Menu-Driven Scripts