Decisions
Make a script choose: test values and files, and pick from a menu.
Updated
Choosing
- If This, Then That: if and [[ ]] (Apprentice): if lets a script choose: the commands after then run only when a test passes, [[ ]] compares text with == and whole numbers with -lt and its friends, and if can also ask whether any command worked.
- Checking Before You Act: File Tests (Apprentice): File tests let a script look before it acts: -e asks whether a path exists, -f and -d whether it's a file or a folder, -s whether a file has anything in it, and -nt whether it's newer than another.
- Menus and Choices: case and select (Apprentice): case matches one value against a list of patterns and runs the lines for the first match, which suits a script's options and yes-or-no answers, and select turns a list of words into a numbered menu.
Exercise: The Smart Backup
- Exercise: The Smart Backup (Apprentice): Finish a backup script that copies a file only when it's needed, with a usage line, --help, --list and a --pick menu, checks for missing, empty and folder names, and a clear message when a copy fails, while a check script runs it job by job.