Linux Basic — Interactive Scripts Practice

less than 1 minute read

Practice Exercises — Interactive Scripts

Practice writing small interactive helpers using read, select, getopts, and the confirmation pattern.


Warm-up

  1. Prompt for a username and echo it back.
  2. Prompt for a password silently (read -s) and confirm it by asking twice.
  3. Prompt with a 10-second timeout that defaults to no on timeout.

Validation

  1. Ask for a port number and re-prompt up to 3 times if invalid.
  2. Read a colon-separated record into user:uid:home with IFS=: and read.
  1. Write a select menu to choose a service (start, stop, status) and run the associated command.
  2. Use select with an array populated from ls /opt/tools.

Options & Flags

  1. Make a small script that accepts -f (force) and -n COUNT using getopts.
  2. Mix --help as a long option handled before getopts.

Confirmation

  1. Implement a confirm wrapper and use it to protect a rm -rf simulation.

Verification Checklist

  • All secrets use read -s
  • Timeouts have defaults
  • getopts uses shift $((OPTIND-1)) to expose remaining args
  • select handles invalid numeric choices gracefully

Next chapter → Menu-Driven Scripts

Updated: