Department of Engineering

IT Services


Suppose in a directory that you wanted to change all the filenames ending in .f77 so that they instead ended in .f90. How would you go about it?

If you're new to Unix you may need to find out how to change filenames. Typing apropos filename will list the programs and routines whose summaries mention filename. Alas, the most useful command isn't mentioned there. Typing apropos rename lists mv which is what you need.

Maybe next you might try "mv *.f77 *.f90". Alas, this won't work - the shell will replace *.f77 by a list of filenames and the resulting command will fail. You need to use a loop of some sort. You also need a way to remove a suffix (look up basename) and how to add a new suffix. It's worth experimenting with a single name first. Do filename=test.f77 and see if you can produce a test.f90 string from $filename . One solution is

for filename in *.f77
  b=$(basename $filename .f77)
  mv $filename $b.f90

Here are some more exercises with useful man pages suggested. None of the solutions to these should be much more than a dozen lines long. Answers to some of these are in the next section.

  1. Change the args script supplied earlier so that if no argument is provided, "They are'' isn't printed, and if exactly 1 argument is provided, "... 1 argument'' rather than "... 1 arguments'' is printed (use if)

  2. Read in two numbers from the keyboard and print their sum (see the read, echo and let commands in the shell manual page).

  3. Write a shell script that given a person's uid, tells you how many times that person is logged on. (who, grep, wc)

  4. Write a shell script called lsdirs which lists just the directories in the current directory (test).

  5. Write a shell script called see taking a filename name as argument which uses ls if the file's a directory and more if the file's otherwise (test)

  6. Write a shell script that asks the user to type a word in, then tells the user how long that word is. (read, wc)

  7. In many versions of unix there is a -i argument for cp so that you will be prompted for confirmation if you are about to overwrite a file. Write a script called cpi which will prompt if necessary without using the -i argument. (test)

  8. Write a shell script that takes a uid as an argument and prints out that person's name, home directory, shell and group number. Print out the name of the group corresponding to the group number, and other groups that person may belong to. (groups, awk, cut. Also look at /etc/passwd and /etc/groups).

  9. Sort /etc/passwd using the uid (first field) as the key. (sort)

  10. Suppose that you want to write the same letter to many people except that you want each letter addressed to the senders personally. This mailmerge facility can be created using a shell script. Put the names of the recipients (one per line) in a file called names, create a texfile called template which has NAME wherever you want the person's name to appear and write a script (using sed) to produce a temporary file called letter from the template file.