Practice
Tasks
A goal, a starting filesystem, and a par. Graded on the state of that filesystem, plus standard output, plus the exit code — and, where the task is about streaming, plus what the pipeline did not read.
Quoting and expansion
Read the awkward name
warm-upOne of these files has a space in its name. Print its contents, in a single command, with nothing on standard error.
par 1 command
Remove the file that looks like an option
routineThere is a file called
-rf.txt.rmwill read that name as options. Remove it anyway, and leave everything else alone.par 1 command
A pattern in a variable
routinePut
*.txtin a variable, then use it so that it expands to the matching filenames.par 2 commands
Exit codes and errors
Capture the status
warm-upTry to read a file that is not there, then print the status it left behind — and no other output.
par 2 commands
Create it only if it is not there
routineWrite
readyinto the named file, but only if that file does not already exist. One line.par 1 command
Make the failure visible
hardA pipeline whose first stage fails reports success. Turn on the option that changes that, run one, and print the status.
par 3 commands
Pipes and streams
Separate the streams
routineRead one file that exists and one that does not. The error goes into
errors.txt; the output stays on the terminal.par 1 command
Read one line, not the file
hardPrint the first line of
big.txt. The answer is graded on what it read as well as what it printed — a pipeline that reads the whole file does not pass.par 1 command
Keep a copy on the way past
routineSort the words, save the sorted list to
sorted.txt, and count the lines — all in one pipeline.par 1 command
Stop an infinite producer
warm-upTake three lines from something that never stops on its own. The pipeline has to terminate.
par 1 command
What did the background job do?
routineRun something that fails in the background, then report the status it exited with.
$?straight after an&will not tell you.par 2 commands
Text processing
Count the errors
warm-upPrint how many lines of
app.logmentionERROR. Just the number.par 1 command
Count each word
routinePrint each distinct word once, with how many times it appeared.
par 1 command
Take the second field
warm-upPrint only the second colon-separated field of every line.
par 1 command
Drop the debug lines
routinePrint
app.logwithout any of itsDEBUGlines. The file itself must not change.par 1 command
Rename a severity, in the file
hardEvery
ERRORinapp.logshould readFAILUREafterwards — in the file, not just on screen. Check it before you commit to it.par 1 command
The ragged column
warm-upPrint the second column of
table.txt. The columns are not aligned, and that is the point.par 1 command
Total a column
routinePrint the sum of the second column of
table.txt, and nothing else.par 1 command
Filter on a number
hardPrint the name from every row of
table.txtwhose second column is more than 40.grepcompares text, so it will not do.par 1 command
Files and finding
Two names, one file
routineGive the file a second name without making a second copy of it. Copying is not allowed here.
par 1 command
Find every text file
routineList every
.txtfile anywhere below the current directory, one per line.par 1 command
Traversable but not listable
hardSet the top-level directory so somebody may read a file inside it if they know the name, but may not see what is there.
par 1 command
Scripting
Read each line
routinePrint every line of
words.txtwrapped in square brackets, using awhile readloop.sedis not allowed: this task is about the loop.par 1 command
Keep it local
routineWrite a function that sets
countwithout disturbing the caller's value, then show that it did not.par 4 commands
Write it with a here-document
routineCreate a two-line file with a here-document rather than two separate
echocommands.par 1 command
Loop over the arguments
routineSet the positional parameters, then print each one on its own line — including the one with a space in it.
par 2 commands
Parse your own flags
hardWrite a script taking
-vand-o FILE, which then reports what it was given and what was left over. The operands must survive.par 2 commands
Reading a script before you run it
Show me what it would run
warm-upDIRis not set, and the line isrm -rf "$DIR/". Print what that line would actually hand torm— without handing it torm.par 1 command
Read it before you run it
routineThe documentation says
curl -fsSL https://example.test/install.sh | sh. Do it the other way round: save the script, then report how many of its lines containrm -rf.par 2 commands
Guard the script
hardclean.shrunsrm -rf "$BUILD"/*and nothing setsBUILD. Add the one line that makes it fail instead of emptying this directory, immediately after the shebang. Do not run it to find out.par 1 command