Week 6 workshop exercises

Case statements

  1. Write a script favourite_food.sh which assigns the value “dog” to the variable mypet.

    Then write a case statement which examines the value of mypet, and if the value is “dog”, prints “bones”, if it is “cat”, prints “fish”, and otherwise prints “don’t know”.

    Edit your script to give mypet different values, and run it – does it behave as you expect?

  2. Write a script food_line.sh that takes a single line of input (hint: use read), and then paste in your food case statement to print different messages, depending on the value.

    Try running it as follows:

    echo cat | ./test.sh 
  3. Consider the ENROLMENTS-2017 data.

    Write a script which processes each line of the enrolments file. (Hint: you’ll probably want a while loop, and read somevar to read lines from it.

    Split the line to determine what unit a student is taking, and use a case statement to analyse it. If the unit starts with CITS4407, print out "the best, obvs". If it starts with CITS1401, print out "ugh". (Check the textbook or search online to find out how to use case patterns to match the start of strings.)

    (More on data processing: how would we list students taking both CHEM1001-1 and CHEM1002-1?)