Using Unix: Intermediate

Platform: Unix
Level of Difficulty: Intermediate
<hr>
Rutgers Main | OIT Main | NBCS Main | NBCS Documentation Main


Documentation Note

All examples used in this document will be from the rci login machines.The notation <CTRL>d means that you should hold down the control key while typing the indicated character (in this case d).

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 the Unix environment; all understand the same basic commands, but each has different additional features. The default shell used at Rutgers is the T shell (tcsh). You can view the shells available by typing the command: more /etc/shells

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

/bin/sh Bourne 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:
A variation of the Bourne shell.
/usr/local/bin/tcsh T shell:
See above.
/usr/local/bin/bash Bourne Again shell:
See above.

To change your login shell, issue the command chsh. The computer will then display your current shell and prompt you for your new login shell. Issuing a blank response to this prompt will result in no change to the login shell.

Shell Variables

Shells have settings that are called shell variables. Those of interest to most users are TERM, PRINTER, BIN, HOME, EDITOR, and PATH. TERM will hold the terminal type that the computer thinks you are using. PRINTER will hold the name of the printer that you want output to go to by default (see the output of the printers command for printer names). BIN will hold the printing bin number (if any) that you have set. HOME holds the absolute path to your home directory. EDITOR holds the name of your default editor (this will affect command line editing in the t shell). PATH holds a list of paths to be searched for commands that you issue. To review your shell variable settings issue the printenv command. The setenv command is used to alter the shell variables (see man setenv for details on how to use this command).

File Access/Protection

Unix allows you to control access to your files. If you or your start up files do nothing to change the default settings, no one else on the system will be able to read or execute your files. You may protect (or grant access) your files by the individual file or by entire directories. For someone to have access to one of your files, they first must have access to your directory. If you give them access to your directory, you can still restrict access to certain files.

Each file has protections for the three different types of users: owner. group, and others. The owner is the user who created the file. Someone can own a file in your directory, but you can always delete any file that is in your directory, even if it is owned by another user. Groups are set up by the system administrator. They represent users who have some common link, such as users in one class or one department. All existing groups can be found in the file /etc/group. Others are those users who do not fall into the owner or group category.

When the ls command is used with the options -l and -g the accesses are displayed, as in: drwxr-xr-x 2 jqsmith users 512 Mar 12 12:02 Project1

The first ten characters is the access code (explained below), next comes the number of links to this file/directory (2), then the username of the owner (jqsmith), then the name of the group whose members are affected by the group protections (users), this is followed by the size of the file in kilobytes (512), its last modification date/time (Mar 12 12:02), and finally the name of the file/directory (Project1). The common access code positions represent:
Position  character  meaning 
a plain file 
a directory 
2, 5, 8  read access allowed 
3, 6, 9  write access allowed 
4, 7, 10  execute access allowed 
2 to 10  a particular access is not given 
The letters are arranged in a certain way. The first is only used to give specific information about the file (see the above table). Then there are three groups of three characters. The first trio correspond to the owner's rights, the next three are the group rights, and the last three are for all others. When a letter is present it means that category of user has that particular access to the file. A file that has the following protection:

-rwxr-x

is a plain file; the owner can read, write and execute it; users in the same group as the owner can read and execute it; and everyone else can only see that the file exists (if given access to the containing directory).

The default protection given to new files is set by the umask command. If you want all your files to have the same default protection a umask command should be in your .login file. Protections for existing files and directories can be set or changed using the chmod command. The use of the commands chmod and umask is described on page 4.

Directories File Access/Protection

As previously mentioned, in order to be able to access a file, a user must have the appropriate permission for the file. In addition she/he must also have read and execute access to the directory that contains the file. Typically access to directories are set up as follows drwxr-xr-x, which translates to: the owner can do anything with the files in this directory, all other users can access files according to the access that they have been granted for each file. For instance, in the r-x above the r allows other users to look at the files in the directory, the x allows them to look into the directory, and since w is missing other users can not create new files in the directory). If you want to have a totally private directory, set the protections to drwx. In this case no one else could look at any files in the directory. If you want members of your group to be able to create files in your directory, you might use drwxrwxr-x.

Processes

Programs that are run by a shell are known as processes. Processes are affected by control characters: <CTRL>c will interrupt a process, <CTRL>d will exit a process or shell, and <CTRL>z will suspend a process.

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 executes. Background jobs can not 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', from the current directory, in the background; issue the command

./demo &.

The './' instructs the shell to find the executable file in the current directory; 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 tries to read from the terminal. Input can not be given to a background job so make sure that all input necessary is available to it. Background jobs are allowed to produce output.

You can suspend a currently running foreground job 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, where it will continue execution, type fg. You can have more than one background job at a time; the fg command will, by default, turn the latest background job into the foreground job.

Controlling Jobs

The shell keeps a table of current jobs, printed 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 was started as background job number 1 and has a process id of 1234. There are several ways to refer to jobs in the shell. 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; therefore, either %1 or fg %1 will bring job 1 into the foreground. Similarly saying %1 & returns job 1 to the background; this also may be done with bg %1.

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 '-'. %+ starts the current job and %- starts a previous job.

When you try to leave the shell while jobs are stopped, you will be warned that you have stopped 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.

You can terminate a background job using the kill command. kill %1 will terminate job number 1 and kill 1234 will kill the job with a process id of 1234.

If you try to logout and there are suspended jobs, you will be warned of this and the logout will be aborted. If you immediately try to logout again these jobs will be killed and you will be logged out. If you must start a process that is to continue after you logout, precede the command for that process with the nohup command and be sure you 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.

Metacharacters

Metacharacters are a group of characters that have special meanings, different shells may have different metacharacters. The following list is some of the metacharacters for the Rutgers default shell: tcsh.

|
Separates commands to form a pipe (see Using Unix: An Introduction (UNX02201)).
;
Separates sequences of commands (or pipes).
()
Encloses a sequence of commands or pipes to be executed as a single command.
||
Separates two sequences of commands or pipes the second of which is executed only if the first fails.
&&
Separates two sequences of commands or pipes the second of which is executed only if the first succeeds.
!
Precedes a history substitution (see man history).
*
A wild card character that matches any group of characters of any length, allowing a user to specify a large group of items with a short string. For example, to specify all the files that start with 'abc', you use 'abc*'.
?
A wild card character that matches any single character.Thus ls ??? lists files in the current directory whose names are only three characters long, while ls ???.* lists those files with a three letter main name and any extension.
[..]
A set of characters that can be matched. Thus ls [a-c]*.??? lists all files that begin with a, b, or c and have a three letter extension.
<
Redirect the standard input (see Using Unix: Introduction (UNX02201)).
>, >>
Redirect the standard output (see Using Unix: Introduction (UNX02201)).
%
Introduces a job name (see multitasking).
&
Place a process into the background (see multitasking).
@
Display or change the current shell variables (see man csh).

Intermediate Commands: Online Help

whereis
locate the executable, source, and man page files for a command

Dealing with the File System

chmod
set the protections on a file.
Usage: chmod [ugoa] [+-=] [rwx] filename. Of the options first indicate whose access is to be changed u (user, you), g (group), o (other), or a (all); then indicate the type of action + (add), - (delete), or = (set); then specify the access to be set r (read), w (write), or x (execute); and finally the file whose access is to be changed. chmod o-rwx file.name denies others from having any access to the file file.name.
file
determine the type of a file grep search for a character string in a file.
Usage:
grep string file
mkdir
create a subdirectory of the current directory rmdir remove an empty subdirectory
umask
set the accesses to be denied on newly created files
Usage: umask ### Each # indicates which accesses (if any) are to be denied to a class of user. The first affects the user (you), the second affects the group, and the third affects all others. If a # is 0 then no access is denied that class of user, 1 denies execute access, 2 denies write access, 4 denies read access, 3 is the sum of 1 and 2 (no execute or write access), 5 is the sum of 4 and 1 (no execute or read access), etc. umask 077 would allow full access by the user and deny all others any access. Usually this command is placed in the file .login to be executed every time you log in.
wc
count and display the number of lines, words and characters in a file

Work Environment

date show the current date and time printenv show your current environment variable settings setenv set environment variables unsetenv unset environment variables

Controlling Your Jobs

bg
run the indicated job in the background
fg
move the indicated job from the background to the foreground
jobs
list current jobs and their status
kill
terminate a process ps list current processes

For More Information

Questions should be directed to the OIT/NBCS Help Desk (732-445-HELP). You can also send electronic mail to the address help on any OIT system (e.g., help@eden.rutgers.edu or help@rci.rutgers.edu) or post a message to the newsgroup ru.comp.qa.news .


<hr>
Copyright © 2008 Rutgers, The State University of New Jersey, NBCS Help Desk. All rights reserved.

Rutgers Logo

webmaster@nbcs.rutgers.edu
06/22/05