Skill 3 in course 2.1: Users and Automation
Shell Scripting
Write short Bash scripts that take arguments, make decisions, repeat work and process command output.
You will be able to
- Create an executable script with a shebang, arguments and a meaningful exit status.
- Make decisions with if, test, [ ] and case, and validate input before acting.
- Repeat work with for and while loops over lists, files and command output.
- Capture and process command output with command substitution, pipes and read.
Lessons
- 01Your first script: shebang, arguments and exit statusTurn commands you type into a file you can run, give it inputs with $1 and $2, and make it report success or failure.
bashchmodbash -nbash -x$1$#0/5 exercises - 02Making decisions: if, test and caseTest files, strings and numbers, combine tests, and choose between several actions.
iftest[ ][[ ]]case&&0/5 exercises - 03Repeating work: for, while and untilDo the same thing to a list of names, files or lines, and stop or skip when you need to.
forwhileuntilreadbreakcontinue0/5 exercises - 04Processing command outputCapture what a command prints, parse it with the tools you already know, and build small useful scripts from the pieces.
$( )| while readawkcutgrep -csort | uniq -c0/5 exercises