AUnix Shell
AUnix Shell
Introductory Note 2
Robert Evans
Abstract
The Shell is the command interpreter on UNIX systems. This Note intoduces
some of the basic features of the Shell and lists many of the commands or pro-
grams available on the UNIX systems in the Department.
Contents
1 The Shell 1
2 Command Summary 5
2.1 Logging out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2.1 Commands for accessing floppy disks . . . . . . . . . . . . . . . 5
2.3 File Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4 Manipulating data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.5 Manipulating data (cont’d) . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.6 Compressed files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.7 Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.8 Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.9 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.10 Messages between Users . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.11 Network News . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.12 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.13 Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.13.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.13.2 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.13.3 C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1
2 Computing Support Group: Introductory Note 2 (2001/02)
2.13.4 JAVA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.13.5 FORTRAN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.13.6 Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.13.7 Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.14 Text Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.14.1 General Commands . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.14.2 Troff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.14.3 TeX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.15 Word Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.16 Database Management . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1 The Shell
The UNIX command interpreter or shell is the program you interact with when you log
into a Sun workstation and start a terminal window, or when you log in to a multi-
access Sun UNIX system over the Internet via a terminal emulator such as telnet or
putty,
The default login shell for users in the Computer Science Department is the T-Cshell
(tcsh). It prompts you with a percent symbol (%) preceded by an identification string.
There are other shells available. They all have similar characteristics but each has
its own particular features. The T-Cshell is and extended version of the (csh) C-Shell
interpreter. This Note assumes you are using the T-Cshell.
The T-Cshell has the following features:
commands are invoked by naming them. Most UNIX commands are simply pro-
grams which are run by the shell. For example,
scmabc-% ls
runs the program ls which reads your directory and lists the name of your files.
When you type a command name, the shell will search a set of directories until
it finds the program. This set is known as the search path.
The search path includes your current directory and one or two directories in
your home directory. You can write your own programs and invoke them auto-
matically (by naming them) from your current directory, or from subdirectories
“bin” or “solarisbin” in your home directory. The T-CShell keeps a hash table of
its path to speed-up access. You need to type the command rehash to update
the table if you write a new command and put it in “bin” or “solarisbin”.
commands often have argument strings which may, for instance, represent file-
names. E.g.
is the copy command cp with two filename arguments; “fileA” is copied to a new
file, “fileB”.
Some commands have flag argument strings usually beginning with a “-”. The
flags modify the behaviour of the program being invoked:
scmabc-% ls -lt
the shell will expand wildcards to match filenames in the current directory. For
example,
scmabc-% ls -l *.c
will give a directory listing of the files with names “something.c” (conventionally
C program source files).
most UNIX commands and programs adhere to a concept of standard input and
standard output. The standard input is a stream of data which the program reads
and the standard output is a stream of output written by the program. Often these
are both attached to the terminal so that input comes from your keyboard and
output goes to your screen. The shell lets you redirect the standard input and
output.
The symbol “ ” is used to redirect standard input from a file and the symbol “ ”
makes cat read from file “fileA”. It sends its standard output to the terminal or
screen.
the Shell has the facility to pipe the output of one program to the input of another.
The pipe symbol is “ ”. For example:
scmabc-% ls wc -w
pipes the output of ls (viz., your filenames) into the word count program wc.
The “-w” flag tells wc to count the number of words in its input. So the above
command counts the number of files in your directory.
prints “hello world” on the screen. Some variables are pre-set, e.g. $home is
your home directory. Type set to see a list of assigned variables. The symbol
“˜” can also be used to refer to your home directory.
scripts of shell commands can be written. These can be invoked in the same
way as compiled programs (i.e. just by naming them). For example:
#!/bin/csh
ls wc -w
ˆD
scmabc-% chmod +x ˜/bin/countfiles
4 Computing Support Group: Introductory Note 2 (2001/02)
creates a C-Shell script file in your “bin” directory. The chmod command changes
its protection mode to make it executable.
The first line of the script tells UNIX that the script is written in the C-Shell lan-
guage (UNIX scripts can be written in any language), while the second line tells
the system to run the directory listing command, ls, and pipe its output to the
word count program, wc.
scmabc-% rehash
tells the shell to make a new table of the files on its search path and now
scmabc-% countfiles
will execute the script and output the number of files in your directory,
the shell has “job control”. Programs which don’t require any terminal interaction
can be run in the background.
scmabc-% sort bigfile sortedfile &
scmabc-%
The “&” puts the sort program into the background and the Shell is available
immediately for other commands.
The special character “ˆZ” can be used to suspend a program which is running
in the foreground:
scmabc-% sort bigfile sortedfile
scmabc-% ˆZ
Stopped
scmabc-%
You may now use bg to put the program into the background or fg to continue it
in the foreground. The command
scmabc-% jobs
lists the status of all stopped or background jobs along with a reference number
(1,2,3...). Use this number preceded by a “%” to make bg or fg act on a partic-
ular job. If a backgound job needs terminal input, it will stop until you bring it into
the foreground.
the shell has a history mechanism - it remembers the last few commands.
scmabc-% history
lists the remembered commands along with a reference number. On a worksta-
tion, you can cut and paste from the history to rerun a command. You can also
use the symbol “!” to rerun any command from the history:
scmabc-% !23
reruns command number “23” from the history.
scmabc-% !so
reruns the last command starting “so...”.
scmabc-% !!
reruns the last command.
See the manual page on the C-shell for more details (type man csh). The T-Cshell
has an additional mechanism which allows you to recall and edit previous commands
using the keyboard cursor keys. See the manual page on the T-Cshell (man tcsh) for
instructions.
CSG IN 2 (2001/02) 5
Files can be printed using shell commands, The University host netnews.cf.ac.uk re-
using the CDE print manager, or direct ceives the “Network News” - a bulletin board
from some applications. of information from users in the US, Eu-
rope and elsewhere. These commands
You must specify a printer by name. Print- enable you to read and subscribe to the
ers are called news. The command tin is the recom-
tl3 Teaching Lab 3 (C/2.08) mended news handling program. It works
dot matrix printer on vdu-based systems or in a shell win-
tl3 lw Teaching Lab 3 laser dow on a workstation. It is a threaded
printer newsreader which presents you with arti-
tl2 lw Teaching Lab 2 (C/2.05) cles related to a particular topic one after
laser printer another.
tl1 lw Teaching Lab 1 (C/2.04) tin - threaded news reader
laser printer and poster
Most commands which can be used to print netscape - web browser and news
files, expect the printer name to be given reader and poster
following a -P argument.
Files may be sent to the printers as sim- 2.12 Networking
ple text files or they may be processed in
various ways for the laser printers. The Computer Science Department is con-
lp -d printer - send a file to a nected to the JANET Internet Protocol Ser-
printer vice (JIPS), the UK Universities’ network.
lpr -Pprinter - send a file to a These commands are used to send and
printer receive files from Campus UNIX hosts and
a2ps -Pprinter - format text file infrom other hosts on JIPS and the Internet
PostScript and print around the world.
on laser printer
ftp - file transfer program
dvips -Pprinter postprocess TeX
rcp - remote file copy
file into Postscript
rlogin - remote login to a UNIX
and print on laser
host
printer
rsh - remote shell
tftp - trivial file transfer program
telnet - make terminal connection
to another host
2.10 Messages between Users ssh - secure shell terminal or
command connection
The UNIX systems support on-screen mes- scp - secure shell remote file
sages to other users and world-wide elec- copy
tronic mail. sftp - secure shell file transfer
program
pine - vdu-based mail utility
netscape - web browser
elm - alternative vdu-based
mail utility (Some of these commands may be restricted
frm, - identifies sender of mail for security reasons).
from
mail - simple send or read mail
program
dtmail - CDE mail handling tool on
SPARCStations
mesg - permit or deny messages
parcel - send files to another user
talk - talk to another user
write - write message to another
user
8 Computing Support Group: Introductory Note 2 (2001/02)
2.14.2 Troff
2.14.3 TeX