Linux Exercise: Shell Scripting

Working with local/global variables

  1. In your home directory, write a script, "modifytz", that modifies the TZ variable and then exports and prints this variable.
    • $ cd
    • $ vi modifytz
      #!/bin/bash
      export TZ="Europe/Amsterdam"
      echo "The timezone is now $TZ"
  2. Execute this script under a subshell. See if the TZ variable has changed in your current shell.
    • # bash modifytz
    • # echo $TZ
  3. Make the script executable and run it again. Did the TZ variable change this time?
    • # chmod +x modifytz
    • # ./modifytz
    • # echo $TZ
  4. Move the script to a directory that is included in your $PATH. Execute it again. Did the TZ variable change this time?
    • # mkdir ~/bin
    • # mv modifytz ~/bin
    • # modifytz
    • # echo $TZ
  5. "Source" the script in your current shell. Did the TZ variable change this time?
    • # . ~/bin/modifytz
    • # echo $TZ
  6. Make a list of all hidden files in your home directory. Look at the scripts that are "sourced" in your environment. If you want to, you can set your own TZ variable in one of these scripts.
    • # cd
    • # ls -la
    • # cat .bash_profile
    • # cat .bashrc

Shell Script Exercises

  1. Write a shell script "myadd" that adds the two numbers that were provided as arguments, and prints the result.
    • # vi ~/bin/myadd
      #!/bin/bash
      echo $(( $1 + $2 ))
    • # myadd 2 3
  2. Write a shell script "sigma" that adds up all numbers that were provided as arguments, and prints the result. Use a while loop combined with a shift statement.
    • # vi ~/bin/sigma
      #!/bin/bash
      total=0
      while [ $# -ge 1 ]
      do
      	total=$(( $total + $1 ))
      	shift
      done
      echo $total
    • # sigma 2 3 4 5
  1. Write a shell script "savedel" that deletes files, but only if these files exist and are regular files. Use a "for" statement to deal with multiple arguments. Make sure you print a decent error message if something goes wrong, and return an appropriate exit code.
    • # vi ~/bin/savedel
      #!/bin/bash
      retval=0
      for file in $@
      do
      	if [ -f $file ]
       	then
      		if rm -f $file >/dev/null 2>&1
      		then
      			echo "$file verwijderd"
      		else
      			echo "Kon reguliere file $file niet verwijderen."
      			retval=1
      		fi
      	else
      		echo "$file is geen reguliere file."
      		retval=1
      	fi
      done
      exit $retval

Extra shell script exercises (optional)

These exercises are provided without hints.

  1. Modify your "sigma" script so that it also prints the average value. And if you feel really challenged, modify it further so that it also prints the standard deviation.
  1. Write a shell script that prints how many unique users, beside yourself, are active on the system. (So you should not count yourself, and if somebody is logged in twice, that person only has to be counted once.)
    For an added challenge, modify your script so that a user is only counted once if his sessions have the same origin. But if a user is logged on from multiple locations (IP addresses), he or she should be counted multiple times.
  1. Write a script "kill_all", that accepts a list of process names. It should kill all processes (with the kill command), but only if that process belongs to you. So you may not kill processes owned by others, even if root would accidentally run this script. If any processes were found and killed, the script needs to wait for 10 seconds and see if these processes have not terminated yet. Then, kill those processes with the kill -9 command. Obviously, if any new processes were started in that 10-second window, they should be ignored.
  1. Download the connections file. This file contains a list of "connection start" and "connection end" lines. As the connections overlap, there may be several simultaneous connections. Write a shell script that analyses the file, and displays the following information:
    • The total number of connections
    • (Challenge) The number of connections per modem
    • The maximum number of simultaneous connections
    • (Challenge) The minimum, average and maximum time of a connection
  1. You have just started as a system administrator at a new workplace. Users are complaining to you about certain homegrown SUID programs not working properly, and - strangely enough - you also hear that users have access to all sorts of private information in a certain home directory. You investigate this and find that someone, at some point in time in the past, has executed the command chmod -R 777 * on the home directory where this data and these SUID programs are stored. You have looked at a few backup tapes but all these tapes have the same permissions set, so the command must have happened quite a while ago, and restoring backups is not an option. Fortunately, you managed to find an old file containing the output of the command find . -exec ls -l {}\;. This file does list the proper permissions. So it should be possible to restore the proper permissions on the files in this directory, based on the output from the find command.
    Download the file messed_up.tar.gz and extract it in a directory somewhere. Then download the file reference.txt and use this reference file to apply the correct permissions to the files in the tar archive.
    Note that some files contain SUID, SGID and Sticky Bits as well. Make sure these are restored properly as well.

You can download a tar file with the solutions to these challenge exercises here.

End of exercise