Unix101 Lesson 4

Concepts we covered

  • Commenting out lines in scripts using “#”. This also works on the shell. Try it: put the hash in front of any command and press enter
  • Using the “tar” command to manipulate archives
    • Create an archive: tar cvzf archive_name.tar.gz dirname/
    • Extract an archive: tar xvf archive_name.tar
  • Using “gpg” to encrypt and decrypt
    • Encrypt: gpg -c file
    • Decrypt: gpg file
  • For Loops
    • From Wikipedia: “In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement. Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration.”
    • Example 1 (try it!):
      for name in matt caspar paul gerrit
      echo "Hello $name"
    • Example 2 (try it!)
      for temp in $(seq 1 10)
      echo "sending $temp"
      fortune | mail -s "Fortune Mail" [email protected]


Exercis 1

  • Create a directory homework104. Make sure it is readable only by you.
  • In this directory, create a script called fortune-maker.sh.
    • The script should use a “for” loop.
    • It should take one argument, telling the script how many fortunes to generate.
    • The output should look as follows (in this case the argument was “3”)
Fortune 1: In order to discover who you are, first learn who everybody else is;you're what's left.
Fortune 2: Use a pun, go to jail.
Fortune 3: I can give you my word, but I know what it's worth and you don't.

Exercise 2

  • Remember your backup-homework script from the first lesson? Of course you do! We are going to improve on it now:

  • Copy the original script to to your “scripts” directory. This directory should be in your path (see homework102). In other words, any script in this directory can be called without changing into the scripts directory. If this is not yet working… fix it! :-)
  • The backup-homework script should work as follows:

  • It should create an archive file in your backup directory (e.g. /home/wrede/backups) using the tar command.
  • The archive file should contain all directories in your home directory that begin with “homework” (i.e. /home/wrede/homework*)
  • The name of the archive file should incorporate the current date, for example: backup-2012-05-31.tar.gz. You did something similar for the 102 homework.