Supplementary UNIX Information

Statistics 243 Fall, 2001
  1. There are nine PostScript files on my home page which are reprints of articles written for the Statistical Computing and Graphics newsletter, and which discuss some useful UNIX issues which we have not addressed in class. You can view these files through X windows with the ghostview command, or you can print the files with the lpr command.

  2. You may want to examine the manual page for csh to learn more about history substitution. It's worth mentioning a few of the simplest forms of history substitution here. First, you can repeat the last command you typed by typing two exclamation points (!!) and hitting return. Similarly, you can refer to the last token of a command (usually, but not always, the filename) with the symbols !$ . Another especially useful form of history substitution uses the caret (^, Shift-6 on most keyboards) to allow you to modify parts of the last command you typed.

    For example, suppose you are trying to edit a file called my.first.hw, and you accidentally type my.farst.hw. The following transcript shows how to use the caret to quickly make the change. The commands you type are shown in regular type; the computer's prompts and responses are shown in bold type.

    springer.s243% emacs my.farst.hw
    springer.s243% ^far^fir^
    emacs my.first.hw

    In other words, you surround the text to be replaced with carets, follow that with the text to be used in the replacement, and end it all with a final caret. The shell will echo the modified command.

  3. Shell variable modifiers can be used to extract parts of a filename from a shell variable or a history modifier like !$. To do so, follow the variable or modifier with a colon (:) and one of the letters h (to extract the head of the file, which is the leading part of the pathname), t (to extract the tail of the file), r (to extract the root of the file, which is the name minus any extension), or e (to extract the extension of the file). These modifiers are especially useful when you use loops in the shell (See part 4).

  4.   Each of the commonly used shells has its own programming language. Even though most problems which might be candidates for shell programs can be more effectively solved with other languages (notably perl), it is worth noting that most of the shell programming constructs can be typed into an interactive shell, making short work of some otherwise time consuming tasks.

    For example, while filename wildcards can be used to do lots of repetitive tasks (like copying or moving many files from one directory to another), there are some tasks (like creating links or renaming files) for which wildcards will rarely, if ever, do the right thing. While the syntax for the C shell's foreach loop is a bit odd, it nevertheless is very useful, especially combined with the modifiers described in part 3.

    Suppose we wish to rename a large number of files which have the extension .for to have the same root name, but the extension .f. There is no solution using wildcards which can achieve this goal. The following transcript shows how a C shell foreach loop can do the job

    springer.s243% foreach file (*.for)
    ? mv $file $file:r.f
    ? end

    The shell uses a question mark ? as a prompt for the statements of the foreach loop. The variable $file takes on, in turn, each of the elements resulting in the wildcard expansion of *.for (which must be parenthesized), for each statement between the foreach and the end. Note that you must refer to the variable without a dollar sign in the foreach statement, but with a dollar sign in the statements inside the loop.

  5. Aliases are a way of abbreviating or redefining commands to meet your personal preferences. The classic use of an alias is to change the rm command so that it always prompts you before removing a file, by using the -i option. The following command will tell the C shell to use rm -i whenever you type rm:
    alias rm rm -i
    You can see the aliases which are in effect for your account by typing alias without any other arguments. (In fact, you can just type a, since we have locally created an alias for alias!).

    To have an alias (or any other C shell command) execute everytime you start up a shell, you can place the command in a file called .cshrc in your home directory. If you want to know more about this file, type help dotlogin.

    Finally, if you want to override an alias, type the command name with a leading backslash (\). So to erase all your files without being asked, you could type \rm * instead of rm *.

  6. Many programs use environmental variables to find out about your preferences, or to learn about specific options which you may want to use. For example, some programs will require you to edit files, and you can communicate your preference for an editor by setting the environmental variable called EDITOR to the value of your choice. For example to set EDITOR to be emacs, you could use the following command
    setenv EDITOR emacs
    To display the value of all your environmental variables, use the command printenv; to display just one, use the echo command with a dollar sign before the name of the variable, as in
    echo $EDITOR
    Note that you use a dollar sign when refering to the variable, but not when defining it.