Lab 3: Linux Processes and Scripting

Exercises:

This lab was an introduction to Bash scripting and linux processes.

  1. Learning about linux processes. All linux processes have a PID associated with them, which is a unique id for that process. This can be used then to kill the process from the console.
  2. Starting to write bash scripts which execute other programs. This first script runs a infinite loop, which is there to get us to learn how to use pskill to kill the process.
    #!/bin/bash
    javac Loop.java
    java Loop
  3. More advanced scripting, using if statements and switch cases. This includes the use of posix pattern matching, which is similar to regex. [1] We also learned about for loops.
    #!/bin/bash

    # if statement example
    if [ ... ] then
        echo "Hello world"
        exit 1
    fi

    # case statement example
    case $a in
        [0-9]*) ;;
        *) echo "Arguments must be numbers.">&; exit 1;;
    esac
    for a in $*
    do
        echo "$a"
    done
    This all in the end culminated in writing a bash script that would only take 2 single digit numbers, outputting "OK" and "Error" otherwise. That can be found here (should that work).

Commands and tidbits:

Reflections

Whilst still followable, this is where the content started increasing in difficulty. First, the processes section was not too hard to get, along with piping outputs from one command to another, however this was when I was first met with the horrible, convoluted, unintuive syntax and style of the bash script. Whitespace rules were incredibly strict, stricter even than a whitespace dependent language like Python, and in ways that were hard to grasp. Examples include things like do, then having to be on a new line to their respective for, if statements, as well as certain things throwing errors if there are no spaces surrounding them: if [ cond ] good, if [cond] bad.

Bash seems to be a language where "if it works it works", and I need to get used to its convoluted syntax, just enough to make a script work.

References

Back