Intermediate Use Of The UNIX Operating System

Documentation Notes

All examples used in this document will be from the RCI or Eden systems. User input will appear in this typeface. System output will appear in this typeface. Text that is in italics represents place holders for specifics that you would supply. The notation <CTRL>d means you hold down the control key while typing the indicated character (in this case d). This document is intended to be viewed on the web and used as a self-paced tutorial, and presumes that you will be using either an Eden or RCI account. All commands listed in this tutorial are linked to a description of the command, simply click on the command to go to that description.

Class setup

Shells

The commands that you type to a UNIX system are processed by a program called a shell. A shell reads your command and then calls a program to execute it; that is, shells are used to coordinate other programs. There are several different shells available in UNIX, all of which understand the same basic commands, but with different additional features. The default shell here is the T shell (tcsh). You can see the available shells by typing:

more /etc/shells

The shells currently listed in /etc/shells on RCI are:

/bin/sh

Bourne shell: The original shell. Good programming environment.

/sbin/sh

Bourne shell: See above.

/bin/csh

C shell: A shell with C-like syntax.

/bin/ksh

Korn shell: A shell with some features from the Bourne and C shells.

/bin/tcsh

T shell: The Rutgers default, a C shell with file name and command completion, command line editing, and a friendlier environment.

/bin/bash

Bourne Again shell: An updated variation of the Bourne shell.

/usr/local/bin/tcsh

T shell: see above

/usr/local/bin/bash

Bourne Again shell: See above.

To view your current shell type:

echo $0

and the computer will respond with the current shell, except the C shell where the response will be:

no file for $0

To change your login shell on Eden, go to http://www.eden.rutgers.edu/tools.php. To change your login shell on Rci, go to http://www.rci.rutgers.edu/tools.php. In both cases follow the web form to change your login shell.

To start a new shell (after you have logged in) simply issue the full pathname of that shell (see above). To exit a shell, other than your login shell, type either of the below:

exit
<CTRL>d

Within the standard shell (the T Shell) on RCI and Eden you may create your own shorthand commands to do tasks that you often perform. The command used for this is the "alias" command and the syntax for it is "alias newname 'command to be performed'" as in:

alias rm 'rm -i'

so each time you use "rm" to remove files it will always ask before deleting the files. To make an alias part of your normal startup, place an alias command into your ".cshrc" file. To unset an alias use the "unalias" command.

Shell Variables

Shells have settings that are called shell variables. Those of interest to most users are TERM, PRINTER, HOME, EDITOR, and PATH.

Shell Variable

Description

EDITOR

The name of your default editor (this will affect command line editing).

HOME

The absolute path to your home directory.

PATH

A list of paths to be searched for commands that you issue.

PRINTER

If set, the name of the printer you want to use by default.

TERM

The terminal type the computer thinks you are using.

If your PATH variable is set incorrectly you will have to issue the full path of manny commands you wish to use. To review all of your shell variable settings issue the "printenv" command. To see the value of a particular variable use the command "echo $varname" as in:

echo $TERM

The "$" indicates that what follows is a variable whose value you wish to display. You may also use the command "printenv varname", as in:

printenv TERM

In the T and C shells, the setenv command is used to alter or set shell variables with the command "setenv varname value", as in:

setenv PRINTER arcgalp3

In the T and C shells, the unsetenv command is used to delete a shell variable with the command "unsetenv varname, as in:

unsetenv PRINTER

See man setenv for details on how to use the setenv and unsetenv commands.

Shell Scripts

Shells scripts are files that have shell commands in them such that when they are executed they perform a specific set of tasks. In their simplest form a shell script is simply a file containing a series of commands to be executed one after the other. More complex scripts can be written but are beyond the scope of this document. One feature common to most shell scripts is that the first line is used to set which shell is to be used when executing this script. The line would look like:

#!/bin/sh

if the script is to execute in the Bourne Shell. If no specification is used then the script will execute in the shell currently in use by the person executing the script. Once a shell script file has been created it must be made executable for you to easily run it. This is done via the following command:

chmod u+x script.sh

where script.sh would be the name of the file the shell script is in. Like all commands a script is run when it is named such that the system knows where it is. If the script "script.sh" is placed in a location searched by your PATH variable and if you have execute privileges to it, you may run (execute) it by typing:

script.sh

If the script is not in a directory searched by your PATH variable you must give the path to the script to execute it. Use the command:

./script.sh

to execute the script "script.sh" in the current directory "./". If you make an alias as in:

alias scripts "~/script.sh"

the script "script.sh" in your home directory would be executed whenever you enter the command "scripts". The above command if entered on the command line would create the alias "scripts" for the current shell. If the above line was placed in your .login file this alias would be defined every time you login. On most accounts on RUCS computers the files .login and .cshrc can be found. These files are shell scripts that, for the T and C Shells, are both executed at login time and the .cshrc file is executed when a new T or C shell is started.

Practice 1

Processes

Programs and commands that are run by a shell are known as processes. Processes are affected by control characters such as: <CTRL>c to interrupt a process, <CTRL>d to exit a process or shell, and <CTRL>z to suspend a process. To view what processes you are running in the current login session use this command:

ps

The "-f" option to ps causes a full report of the processes to be displayed, the "-e" causes every processes running on the current system to be displayed, not just those in the current login session. See "man ps" for more of the options that are available for the "ps" command.

Multitasking

Normally when the shell is given a command, it executes the command and then the user waits until the command finishes and the computer issues a prompt for the next command. UNIX is a multitasking operating system, which means that more than one job can be performed by a user at the same time. When more than one job is running, one runs in the foreground and the rest run in the background. The foreground is where the user interacts through the keyboard and the user must wait while the foreground process executes. Background jobs cannot accept input from the keyboard and do not make the user wait for their completion before allowing another job to be started. To run an executable file called "demo" (that is in the current directory) in the background, issue this command:

./demo &

The "&" instructs the shell to put the execution into the background, where it will run unattended until it is finished. The prompt will then appear and another command can be executed.

A job being run in the background will stop if it needs input. Input can not be given to a background job so make sure that all input necessary is available to it. Background jobs may produce output.

You can suspend most foreground jobs by typing a <CTRL>z. This will stop the job's execution and put it into the background. To return the job to the foreground to continue execution, type "fg". You can have more than one background job at a time; the "fg" command will, by default, place the most recently suspended background job in the foreground.

Controlling Jobs

The shell keeps a table of current jobs, which is displayed by the jobs command. When a job is started in the background with "&", the shell prints a line which looks like:

[1] 1234

indicating that the job is background job number 1 and has a process id of 1234.

There are several ways to refer to jobs. If you wish to refer to a job with job number "1", you can refer to it with "%1". Just naming a job brings it to the foreground; thus the following two commands:

%1
fg %1


are synonyms that bring job "1" into the foreground. Similarly the following commands:

%1 &
bg %1


resumes the execution of job 1 in the background.

The command jobs has a notation for current and previous background jobs. The current background job is marked with a "+" and the previous jobs with a "-". Thus:

%+

restarts the current job and:

%-

restarts a previous job.

When you try to leave a shell (or logout) while jobs are suspended, you will be warned that you have suspended jobs. If you immediately try to exit the shell again, the shell will not warn you a second time, and the suspended jobs will be terminated. You may use the jobs command to see what those jobs are and if you want to terminate them. If you must start a process that is to continue after you logout you need to precede the command for that process with the "nohup" command and have provided for all of its input and output to go to files. NOTE: abuse of the system via the "nohup" command will endanger your account privileges.

You can terminate a background job using the "kill" command. The command:

kill %1

will terminate job number 1 and:

kill 1234

will kill the job with a process id of 1234.

Practice 2

Input/Output Redirection and Pipes

UNIX works on the assumption that there is always a place for output, input, and error messages. Most of the time input is taken from the keyboard and output and errors are displayed on the screen. These are commonly referred to as the standard input, output, and error. One of the best features of UNIX is that you can change where each of these are. Sometimes it is desirable to have your input come from a file, and have your output go to a file; this is called redirection. Redirection is slightly different in each of the available shells. In the default Rutgers shell (the T shell) the redirection is achieved by adding one or more of the following to the end of a command:

< filename

Use the file filename as the standard input.

> filename

Use the file filename as the standard output. If it does not exist, then it is created; if it exists, its previous contents are lost.

>> filename

Use the file filename as the standard output like > but appends the output to the end of filename rather than replacing contents.

>& filename

Use the file filename as the standard output and standard error. Replace the contents of filename with new information.

>>& filename

Use the file filename as the standard output and standard error. Appends the new information to the current contents of filename

To redirect the contents of a file to be the input for a command you would type "command < filename". To mail the file foo.file to jqsmith@rci.rutgers.edu in one command:

pine jqsmith@rci.rutgers.edu < foo.file

Once in pine type a <CTRL>x to send the file. To redirect the output of a command to a file you would type command > file. For example, the cat command usually takes one or more files and displays them on the screen, as in: "cat file1 file2". By using redirection, it possible to append "file2" to the end of "file1" and place the results in "file3". This would be done with:

cat file1 file2 > file3

which will make the contents of "file3" be the contents of "file1" followed by the contents of "file2". Now suppose you want to append the contents of "file4" and "file5" to the end of "file3". This would be done with:

cat file4 file5 >> file3

In this case the original contents of "file3" are kept and the content of other files are added to the end of "file3".

When using ">" and ">>" any error messages that occur will still be sent to the terminal screen. To redirect error messages in addition to output use ">&" or ">>&". For instance you do an "ls -F" of a directory and part of what you see is:

     ABC       big.file           misc.prog      pp6
    Letters/   chili              newshell.1*    pp7
    Misc/      class.notes.txt    newshell.2*    pp8
but when you type:

ls ABC

you get the error message:

ls: ABC: No such file or directory

to report this you want to capture the error in a file to mail to help. To do this compose (with emacs) a message in a file named "msg.txt", then type:

ls -F >> msg.txt
ls ABC >>& msg.txt


to append the "ls" output and the error to the file "msg.txt", finally mail this to help with:

pine help < msg.txt

(within "pine" you will have to type a <CTRL>x to send the message).

In the above example, the file in question has a leading space in the name, it would have to be specified as:

\ ABC

As a final illustration of redirection, assume the executable program test takes its input from the keyboard (standard input) and sends output to the screen (standard output). To run a data file through the program, have the output go to an output file, and to have error messages go to a separate error file, you would use a command like:

(./test < datafile > outfile) >& errfile

The parentheses group what is inside them as a separate action then the output of that (only error messages in this case) will be redirected to "errfile".

Pipes are another way to redirect output. A sequence of commands separated by "|" characters forms a pipe. The output of each command within a pipeline is used as the input for the next. For example, the pipeline:

cat msg.txt | pine help

uses cat to display the file "msg.txt", this then is used as the input to "pine" for mailing to "help". This is another way to mail the file "msg.txt" to the address "help" (as before you will have to type a <CTRL>x within "pine" to send the message). The pipeline:

who | sort | lpr

will work as follows: the "who" program will produce as output the names of currently logged in users. This output will then be the input to "sort", which will put that list of users into alphabetical order. "lpr" will take this ordered list and send it to the default printer.

Practice 3

Programming Languages

There are a number of language compilers (programs that convert program files into executable files) on our UNIX systems. To ease the use of the compilers your source code files should be given an extension that matches the language used.

Language

File Extension

Compiler Command

C

.c

cc

C ++

.c

CC

Gnu C

.c

gcc

Gnu C ++

.c

g++

FORTRAN 77

.f

f77

FORTRAN 90

.f90

f90

Pascal

.p

pc

Assume, for example, that you have a C program in the file prog.c. Before executing it, you have to compile it using the C compiler. This is done with the command:

cc prog.c

This translates the program from source code into a binary executable file called a.out. To specify another executable file name use a command like this:

cc -o filename prog.c

Then to run this program you type in the path to that file, in this case either of the following depending upon which of the above was used:

./a.out
./filename


Should you wish to run a program that is in your home directory when you are elsewhere in the file system type in one of the following:

~/a.out
~/filename


Two program debuggers that are commonly available are dbx and adb. For more information see the man pages. For C there is a pre-compilation program checker called lint.

To link in math libraries (for functions like sine, cosine, square root) make the last item on your compile line "-lm" as in:

cc -o filename prog.c -lm

For More Information

Questions should be directed to the NBCS Help Desk, Room 013, Hill Center, Busch Campus, (732) 445-HELP (4357). You can also send electronic mail to the address help on any RUCS system (e.g., help@rci.rutgers.edu or help@eden.rutgers.edu) or you can visit the NBCS FAQ at http://faq.rutgers.edu.

Copyright © 2004 Rutgers, The State University of New Jersey, NBCS, Help Desk. All rights reserved.

07/26/04 Dan Koft, author

Rutgers University Computing Services

UNIX 3