Week 5 workshop exercises

Basic exercises

  1. Use your favourite editor to create a file containing the lines:

    Make the file executable, using chmod u+x my_script.sh.

    Run the script: ./my_script.sh

  2. What environment variables are set on your computer? (Typing set will list them all.) What is the contents of the $PATH variable? Can you write a bash command to print its contents, but separated by spaces instead of colons?

  3. Write a script which displays the contents of the variable MYVAR. In the shell, set the value of MYVAR to any contents you like. Try running your script – what do you see? (Hint: export MYVAR=myconts should alter this behaviour.)

  4. Try each of the following, then displaying the value of x:

Control structures

  1. Create several files named file1, file2, file3 and so on, using touch.

    Use for afile in file*; do echo $afile; done to list them. (Also try for afile in `ls file*`; do echo $afile; done.)

    Give the files some simple content – what commands can you use to do this, without having to use an editor?

    Write a new bash command to print the name of each file, surrounded by equals signs, then a blank line, then the contents of the file.

  2. Create a script containing the following text:

    Try piping the contents of a text file into the script ( e.g. with cat SOMEFILE | ./myscript.sh) – what do you see?

  3. Type ls, then echo the exit code: $? – what do you see? Now try the same, but with a directory that doesn’t exist (e.g. ls XXXXYZ) – what happens?

    Try the same but with the commands false and true instead of ls.

    Now try if true; then echo "Yes"; fi. Try again using false instead of true. What happens if you replace true with a sequence of commands (that is, something like if cmd1; cmd2; cmd3; then echo "Yes"; fi, where cmd1, cmd2 etc are commands of your choosing) – when will the body of the if be executed?

    What about if we use a pipeline in the condition of the if statement? Try something like:

    and something like:

    Which of these prints the string yes? Can you work out why?

  4. Challenge: Using the ENROLMENTS-2017 file from the last workshop, write a script which reads in lines from stdin, and if the line contains the string CITS, increments a variable called total_cits_enrolments.

    After reading in the file, it should display a message including the contents of that variable.

    This problem will require all the techniques highlighted in exercises 1–3. If you have programmed before in another language, it may be straightforward. If not, it is suggested you work on this exercise in your own time, and we will examine one possible solution next week. Work carefully through the Shotts text, chapters 24–29. These chapters have many examples, and working through them should give you plenty of practice with for, while and if statements.

    Hint: we will probably need to use a pipeline as the condition for our if statement – something like if (cmd | filter); then ...; fi (where cmd and filter are replaced with other Linux commands).

    Here is one solution, based only on the Bash constructs we have seen so far:

    The combination of read and grep will be quite slow, however, since the grep command must be run for every line in the file (and there are 42630 lines).

    When testing your solution, the process will be much quicker if you create a small test file, or use head to test your script only on (say) the first 100 lines of ENROLMENTS-2017.

    Here is a faster solution, which uses Bash’s “improved test” feature (described here: http://mywiki.wooledge.org/BashFAQ/031). By putting an expression in double brackets, we can test for string equality, do integer comparisons, test for “glob” pattern matches, and test for regular expression matches.

    For instance, try the following.

    Test for string equality:

    Do an integer comparison:

    Do a “glob-style” match:

    That means we can write a script like the following, which doesn’t have to call grep:

    You will find the second version typically runs around 100 times faster than the first.

Advanced exercises

If you have programming experience and would like a challenge, take a look at the exercises here from the Advanced Bash-Scripting Guide by Mendel Cooper.