Exercises

Lecture 5 Scripting Example

Download the script called lectfour. At a shell prompt you can type wget https://courses.cs.washington.edu/courses/cse374/21sp/assignments/lectfour. Use emacs to open this script by typing emacs at the prompt. Use ctrl-x,ctrl-f to open the file. You can choose the file by typing its path in the prompt at the bottom of the window. Use your arrow keys to navigate through the script and read its examples. Now exit emacs (use ctrl-x,ctrl-c), and then run the script ./lectfour try this. You will need to make sure the script is set to be executable before you do this using the chmod command.

Note: If you are on your virtual machine you can open emacs in a new window - emacs &. Alternatively, you could open a second frame within emacs (ctrl-x,2), and then run a shell in one of them (meta-x,shell), and run your script in that shell.

Lecture 5 Scripting Challenge

Create a script called sortpeople.sh that sorts a list of people by their last names. For example, when we run the script ./sortpeople.sh names.txt, where names.txt contains the following:

Ada Lovelace
Rachel Carson
Hypatia Alexandria
it prints
Alexandria
Carson
Lovelace

You should use three commands, piped togetheer using the | symbol. You should use cat and sort and well as either cut or awk. Use man pages and/or Google to look up how to use these commands and what options you might need.

You can find a list of names here - can you modify the wget command to download this? For background on these names, check this out (scroll down).

A fully completed script will include error checking for the input argument, and be used as an executable. However, it might be useful to test the commands at the command line first.