Lecture 7 – Sep 18th, 2019

Setup

  1. Log in to clyde.
  2. Create a directory and cd into it.

Task

  1. Create a Bash script named params that prints out each of its parameters. The output should look like this.
    $ ./params 'foo bar' '$HOME' "$HOME" "~" ~
    1: foo bar
    2: $HOME
    3: /usr/users/noquota/faculty/steve
    4: ~
    5: /usr/users/noquota/faculty/steve
    
  2. Copy params to a new file params2. Modify params so that your code is inside a function named main. After the function definition, call main as
    main $*
    

    and examine what happens when you run the script. In particular, compare to params. Replace $* with each of $@, "$*", and "$@" and run the script after each modification. Make sure you understand the differences between the four. (Unquoted $* and $@ should behave the same, but the others are different.)

  3. Create a new script architecture. This script should iterate over each of its parameters and print out a friendly name for each computer architecture. If the architecture is not known, print it out verbatim.
    $ ./architecture i386 i486 i686 x86_64 m68k ppc armv6l armv6b \
      armv7l armv7b armv8l armv8b microblaze
    Intel x86
    Intel x86
    Intel x86
    AMD x86-64
    Motorolla 68000
    PowerPC
    32-bit, Little-endian ARM
    32-bit, Big-endian ARM
    32-bit, Little-endian ARM
    32-bit, Big-endian ARM
    64-bit, Little-endian ARM
    64-bit, Big-endian ARM
    microblaze
    

    (Here, microblaze isn’t known to the script so it printed it verbatim.) Use a case statement rather than a series of if/else statements. Use patterns with wild cards to minimize the number of cases. In particular, the 32-bit Intel x86 and the 32-bit ARMs can be simplified.

  4. The uname command will print a bunch of information about the current machine when given the appropriate options. In particular, uname -m will print out the current machine’s architecture. Try running
    $ ./architecture "$(uname -m)"
    
  5. Run shellcheck on each of the scripts you created. Fix any warnings or errors.
  6. Bash’s parameter expansion can actually do a lot more than expand variables to their values. Peruse the manual and try out several of these in the shell.

    In particular, ${var:-word} and ${var-word} will expand to either the value of the var variable or the expansion of word depending on whether var has been set (and is not empty, depending on using the colon or not). Substrings can be returned by using ${var:offset} or ${var:offset:length}.

    Other useful expansions include ${var#word}, ${var##word}, ${var%word}, ${var%%word}, and ${var/pattern/string}.