Unix Operating System
Unix Operating System
Asad Khailany, D.Sc.
Eastern Michigan Unuversity
Unit 1
Definition of Computer operating system
Unix operating system
Kernel
utilities
major components of the Kernel
File subsystem
processor subsystem
What is Shell?
Commands accepted by Shell utility
Unit 2
Logging on
Logging off: logout, exit, CTRL-d, ignoreeof
Some basic commands:
date, ls, mkdir, cat, who, read, cwd, pwd, passwd,
finger, csh, stty, cal, uptime, ruptime, alias, wc,
!, !number, whoami, lock, leave, bc, ln
Standard input, output and error files
Input & output redirection
variables & variable substitution
Unit 3
grep, cut
On line manual
Text editors in Unix
vi
Unit 4
File organization
Some hidden files
.login .cshrc .profile .history
.logout .project .plan .newsrc
More commands related to files
ls, du, quota, chmod, compress, uncompress, rm
cd, cp, ln, mv, rmdir, more, less, head tail, pg
/etc/passwd file
pipes
more commands
paste, wait, wc, w, biff
Unit 5
Shell variables
Shell Programming
Postional Parameters
Special variables
Environmental variables
Unit 6
mail
elm
talk
write
mesg
Unit 7
Shell Programming
if, while, for , case
Four different quotes
Single quotes
double quotes
back quotes
back slash
Parameters
Keyword parameters
Positional parameters
Special parameters
Unit 8
Regular expression. See table 19-2 - page 313
more commands
tr, sort, sed
network - ftp, internet
more shell programming
Exam
Unit 1
1. What is a computer operating system?
Computer operating system is a complicated software package manages
And controls the hardware and application programs (software). Examples of tasks performed by computer operating systems are:
a. Memory Management
b. File Management
c. Scheduling - Process Management
d. Provides accounting and security services
2. Unix Operating System
The Unix Operating System is a multitasking and multi-user timesharing system.
Unix is a trademark of Novell Corporation. Note Novell bought Unix from AT&T.
All types of Unix are based on either :
BSD (Berkeley Software Distribution) Unix
or
System V Unix of AT&T which was produced at its Unix Systems Laboratory (USL)
The Unix operating system logically divided into two components:
a. The Kernel which is the heart of the Unix systems. The Kernel resides in the computer's memory all the time when the system is up.
b. The utilities reside on the secondary storage devices. Virtually every command you know under the Unix systems are parts of the utilities. Further, the shell itself is a utility program. Utility programs brought to the memory when needed.
The Kernel performs various primitive operations on the behalf of user processes. Among the services provided by the Kernel are:
1. Controlling the execution of processes.
2. Scheduling processes fairly for execution.
3. Allocating main memory
4. Allocating secondary storage.
5. Allocating processes controlled access to peripheral devices.
The two major components of the kernel are:
1. Files 2. Processes
File Subsystem: The file subsystem has the following responsibilities:
1. Manages files 2. Allocates file spaces 3. Administers free spaces
4. Control Access to files 5. Retrieve data for users.
The Unix file system is characterized by:
1. A hierarchical structure
2. The consistent treatment of file data
3. The ability to create and delete files.
4. Dynamic growth of files.
5. The protection of file data.
6. The treatment of peripheral devices as files.
The Process Subsystem: The processes subsystem is responsible for:
1. Process synchronization
2. Inter processes communication.
3. Memory management: The two policies for memory management are:
a. Swapping between main memory and secondary storage devices
b. Demand paging
4. Process scheduling: process scheduling allocates CPU to process.
Some example of system calls for controlling process are:
a. fork (create a new process). b. exec c. exit d. wait
The Process control subsystem interacts with the file subsystem via system commands. Examples of such system commands are: open, close, read, write, stat and chmod.
The most commonly used UNIX variants, including:
o AIX (IBM)
o Solaris (SunSoft)
o HP-UX (Hewlett Packard)
o SVR4 (AT&T)
Someother UNIX are:
o BSD (Berkeley Software)
o Digital Unix
o Linux
o SunOS (predecessor to Solaris)
o IRIX
.
What is a shell?
A Shell is a utility program. It is loaded into memory for execution whenever you log into the system. Shell is an interpreter programming language. It has its own syntax, semantic rules and commands.
Commands accepted by the Shell
Shell allows the following three types of commands:
1. A command can be an executable file that contains object codes produced by the compilation of source code. For example, a.out contains the object code produced by a C program.
2. A command can be an executable file that contains a sequence of shell commands lines.
For example .login (in C shell) , .profile (in Korn shell) contain shell scripts.
3. A command can be an internal shell command.
Different types of shells:
Following are different shells, names of their programs and their default prompts.
1. Bourn family shells:
o o Bourne shell - developed by Steven Bourne of AT&T - Late 1970s. The name of the program is sh. The default prompt is $
o o Korn shell - developed by David Korn of Bell Lab - mid 1980s. The name of the program is ksh. The default prompt is $ . This was created as the replacement of Bourne shell. The shell is an upward compatible with Bourne shell.
o o Bourne Again shell (bash) - developed by the Free Software Foundation - released 1989. The name of the program is bash. The default prompt is bash$ .This shell is also an upward compatible with Bourne shell.
o o Zee-shell (ZSH) - developed by Paul Falstad in 1990. This shell has all of the important features of other shells in addition to new capabilities. The name of the program is zash. The default prompt is the name of your computer followed by % .
2. The C-shell family contains:
• C-shell : The C-shell was written by Bill Joy when he was a student at university of California at Berkeley. The name of the program is csh. The default prompt is % . .
• Tcsh (Tee See Shell): The Tcsh shell was started in late 1970s by Ken Greer at Carnegie-Mellon University and carried on by Paul Placeway at Ohio State University in 1980s. The name of the program is tcsh. The default prompt is the greater than sign character >. Note that the tcsh prompt on our system is % soon you will learn how to change the prompt signs. .
An installation may support several shells. For example at EMU we have the following shells available:
1. The default shell is tcsh and its prompt is %.
2. C shell with prompt %.
2. Bourn shell and its prompt is $.
3. Korn shell and its prompt is $.
4. Bourn again shell ant its prompt is bash$
To change from a shell to a different shell:
To change from a shell to a different shell type the command of the shell program which you want to be your shell. To go back type either control d, or logout or exit.
Examples-1:
Suppose you are at tcsh shell and you want to transfer to Korn shell type: ksh
To go back to tcsh shell type control d
Thus you can transfer to any shell by typing the name of the shell program.
Suppose your current shell is tcsh (that is tee shell). to make korn shell to be working shell type ksh . Now you are at korn shell. From here make the bash shell to be your working shell by typing: bash. At this point make the tcsh to be working shell by typing tcsh
As you see we went from tcsh to ksh, from ksh to bash and from bash to tcsh. To logout from any shell type either control d or exit. Rcall that we went from tcsh to ksh and from ksh to bash and from bash to tcsh . At this point we go backward. First typing control d returns to bash. From bash typing control d return us to ksh and finally typing control d return us to original tcsh.
Unit 2.
NOTE: UNIX is case sensitive (i.e. "Cat" is not equivalent to "cat").
Logging on:
login: Type the user id. At EMU the users is made up of
6241
password: Type your password. Notice your password is : cis (stands for Computer Information Systems) followed by your student number.
Notice do not type the initial zeros which are in front of your student number. If your student number is 000123456 then your password will be cis123456.
After you get on the system change your password by executing command passwd in other words type passwd. Notice this is passwd not password.
Sequence of events that occurs when you login into a system:
1. When system is on, init program automatically starts up a getty program on each terminal port.
2. getty determine the baud rate, displays login: and waits for someone to type something in.
3. When something is typed, followed by RETURN, getty starts login program and gives login the characters which were typed.
4. login program displays password: and when the user types the password, login searches /etc/passwd file for the user's name and password. if the typed in information is found in /etc/passwd file, then login initiates the program which is the last entry in the line which corresponds to the user in the /etc/passwd file.
The passwd file is located within etc subdirectory, and the etc subdirectory is located within the root (/) directory. Thus in the /etc/passwd the first slash means the root directory, and the last entity is the file. Thus passwd file is within etc subdirectory which is within the root directory. Each authorized user has a record ( a line) within passwd file. For example the line correspond to the user khailany is:
khailany:##khailany:3088:466:Asad Khailany:/usr/users/cis/khailany:/usr/local/bin/tcsh
There are 7 fields in each record of the passwd file, the semi colon .:. is used the separate the fields. Following are the fields of the passwd file;
The first field contains the user id. khailany is my user id.
The second field contains encrypted password. ##khailany is my encrypted password.
The third field contains user number. 3088 is my user number.
The fourth field contains group number. 466 is my user number. Notice all students in your class have the same group number.
The fifth field contains user name or other information such as phone number. Asad Khailany is my name.
The sixth field contains the home directory of the user. /usr/users/cis/khailany is my home directory.
The seventh field contains the name of the default shell program. In other words user.s defaukt login shell. /usr/local/bin/tcsh is my default shell. Notice the last entry of /usr/local/bin/tcsh is tcsh (T shell) that is when I log in by default my shell will be T shell.
Home directory is the name of the disk area which is a number of disk blocks assigned to a user. Every user has a home directory. Thus my home directory is khailany within cis subdirectory within users subdirectory within usr subdirectory within root directory ./..
Root
Logging off: Two ways to log off:
exit
CTRL-d
Unix command format:
All Unix commands have the following format:
command name [options] [arguments]
Note that both options and arguments are optional. Each option is preceded by a minus sign.
Examples;
date displays the current date - no option no argument.
ls list all file names in the home directory- no option no argument
This command has the same effects as dir command in DOS.
ls -l displays the file names in a long format. Information about the type
of the file and the amount of storage occupies, different names .. etc. is provided. Notice that the option is -l but there is no argument.
mkdir database creates database directory within the current working directory. Note there is no option but the argument is database.
cat poem displays the content of the file poem on the screen. Note there is no option but the argument is poem.
ypcat command is used to display the content of the passwd file. Thus the command to display all the records of passwd file is:
ypcat passwd our Unix knows that passwd file is with /etc subdirectory.
grep pattern command is used to only get only the lines which contains the pattern.
For example to get only the records of students in which has 624 somewhere in the record type the command:
ypcat passwd | grep 624
Notice is called .|. pipe which makes the output of the program in the left side of the pipe .|. to be the input to the program in the right side of the pipe .|..
who displays information about all users who are currently logged
in the system.
cp file1 file2 this command copies the content of file1 to file2. Here there are two
arguments file1 and file2.
rm file1 deletes file1.
Standard input, output and error files
1. The standard input file for Unix is the key board.
2. The standard output file is the screen
3. The standard error file is the screen
echo command displays its arguments on the standard output.
echo I am trying to learn Unix
The phrase 'I am trying to learn Unix' is displayed on the standard output device (the screen).
Unix always uses its standard devices for the input, output and error files. The user must use redirection characters if they want input to be read from a file other than standard input file. The user must also use redirection characters if they want the output or errors to be written in a file other than the standard output or the standard error file.
The greater sign > is used to redirect the output and the < is to redirect the input file.
example:
echo I am trying to learn Unix > my-output
writes the phrase 'I am trying to learn Unix' in file my-output. Note that Unix created the file my-output for the user.
Now try to display the content of your file on the standard output by using the command
cat my-output
cat command displays the content of the file on standard output device (that is screen).
To create a file call it current-user to contain all users currently logged on the system type:
who > current-user
passwd This command changes your password
finger smith retrieves information about a user whose user-id is smith
Variable and file names
The only restrictions on variable and file names on most Unix systems are to have the first character to be an alphabetic or an under score '_' character and contain no space or tab characters. Some Unix systems restrict variable and file names to 14 characters.
Go to korn shell by typing ksh your prompt will bee $.
The following command reads the value for variables pay and employ-name from the standard input device.
read pay Employ-name .
After typing the above command followed by RETURN, type the following line followed by RETURN
980.50 Linda Brown
In the above read command we have two variables pay and Employ-name. Unix scans the input data from the left to the right until encounters a non delimiter character. A delimiter is either a space or a tab. All information from that point until the next delimiter is assigned to the first variable. In our case 980.50 is assigned to variable pay. The information from the next non blank or tab character until the next delimiter is assigned to the next variable. In the case where the number of variables (as in our case) is less than number of input fields, the remaining information from the current non delimiter character to the end of the line is assigned to the remaining variable. Thus the value of the variable Employ-name will be Linda Brown.
To retrieve the value of a variable (that is variable substitution) a dollar sign is placed in front of the variable ($variable).
Examples:
To display the value of variable pay type
echo $pay when this command is executed the following number is displayed.
980.50
echo pay displays pay that is no substitution
echo $Emply-nmae displays Linda Brown
echo The name of the employee is $Employ-name the payment is $pay
makes the following to be displayed
The name of the employee is Linda Brown the payment is 980.50
This is known as value substitution. shell interpret $ sign to substitute the value of the variable.
If you want $ to be displayed (i.e. not substituted) then the $ sign must be preceded by \ or enclosed into two single quotes to prevent shell from interpreting $. For example both of the following statements;
echo The name of the employee is $Employ-name the payment is \$$pay
echo The name of the employee is $Employ-name the payment is '$'$pay
produce the following output:
The name of the employee is Linda Brown the payment is $980.50
The following command
echo *
displays the name of all the files which are in your home directory.
ls ph*
displays the name of all files that start with ph
The * used for the remaining characters or the field. On the other hand ? (question mark) is used for one character only. The following command list the name of all files which starts with roll and the last character can be any thing.
ls roll?
ls ??? list the name all files which has only 3 characters.
Note both * and ? are regular expression. There are other regular expression characters that will be discussed later.
Assignment Statements
Assignment statement used to assign values to variables.
Bourn Shell family:
The format for assignment statements for Bourn shell family is :
variable=value
Note no spaces before or after = sign
Example
x=100 assigns 100 to the variable x
echo the value of x is $x displays
the value of x is 100
name='John Smith' makes value of variable name to be John Smith.
PS1='ksh$' makes the value of variable PS1 to be ksh$. Notice that PS1 contains the primary prompt in bourn shell family. Thus we changed the primary prompt from $ to ksh$.
To know what is the default primary prompt in your bourn family shell type
echo $PS1
The secondary prompt is the content of variable PS2. In our system the default secondary prompt for bourn shell family is >. to change it in bash from > to bash> type the command
PS2='bash>'
To know what is the default secondary prompt in your bourn family shell type
echo $PS2
C Shell family:
The format for assignment statements for C shell family is :
set variable=value
Note no spaces before or after = sign or make spaces on both side of =
Example
Following command changes the prompt sign to '>>'
set prompt='>>'
Following command changes the prompt sign to command number followed by word MY-C-Shell.
set prompt='! My-C-shell'
set x= 100 This statement sets the value of variable x to 100
set name=Jim Brown This statement sets the value of variable name to Jim
echo $name
Jim value of variable name is displayed
set name='Jim Brown'
echo $name
Jim Brown value of variable name is displayed
Quotes
Shell recognizes the following four different types of quotes:
1. The back slash character \. The back slash character informs the shell to take the next character literally. In other words, it prevents the shell from substitution or interpretation of the character following the /
Example:
The following command displays the name of all file in your directory:
echo *
Notice that shell interpreted * for the name of all files in the user's directory
echo \* displays *. Note that \ shell prevented shell from substituting the *.
2. The single quote character ' . Anything between two single quotes is considered literal. In other words shell is prevented from substitution or interpretation of any variables, files or characters within a pair of single quotes.
Example:
set x=15
echo 'the value of variable x is displayed by typing echo $x'
The above echo produces the following output:
the value of variable x is displayed by typing echo $x
Notice that shell was prevented from variable substitution.
3. The back quote character ` A pair of back quote characters is used to inform the shell to execute the command which is in between the pair of the back quote characters.
Example:
echo today date is date This produces today date is date
Note date is a command in Unix but shell was not informed to execute it.
echo today date is `date` This produces today date is Tue Sep 7 12:28:34 EDT 1999
Here shell executed the next date because it was enclosed in between two back quote characters.
4. The double quote character: A pair of double quotes has the same effect as a pair of single quotes with the exception that the $ sign and back quotes (and also back slashes in some systems) characters are not ignored by shell inside the pair of double quotes.
$
Example:
set x=15
echo 'the value of x is $x' The output of this command is
the value of x is $x Note two single quotes and the $ sign was ignored
echo "the value of x is $x" The output of this command is
the value of x is 15
Note the $ sign inside the two double quotes was not ignored
echo "today date `date`"
The output of this command is
today date Tue Sep 7 12:28:34 EDT 1999
How to perform arithmetic operations
In C Shell Family
Use set statement to assign values to variables
Following statements assign 20 to x and display its value
set x=20
echo $x
20
Suppose you want to add 1 to x, you may attempt to write the following command:
set x=x+1
When you type the following command to display the value of x you see x+1 is displayed.
echo $x
x+1
So how we add 1 to x ? To add 1 use expr command. Notice the back quotes and the dollar sign in front of x in the following statement.
set x=20
set x=`expr $x + 1`
echo $x
21
To use multiplication sign (*) you have to inform shell not to interpret * by having a back slash (\) in front of it. Note that shell considers * to be the list of all file name in the current working directory. Also to prevent shell from interpreting division sign (/) put a back slash (\) in front it.
The first statement below assigns 21 to x. The second statement multiplies value of x by 5 , divides 60 by 5 and adds the division reslut 12 to the result of multiplication that makes the value of x to be 117.
set x=21
set x=`expr $x \* 5 + 60 \/ 5`
echo $x
117
Another way to do arithmetic operation in C shell is to use @. This is easier than using set and expr. The format for this command is:
@ variableName = expression
In expression you do not have to use \ in front of * or / , but variable names must be preceded by $ sign (variable substation).
Examples:
Following command assigns 4 to variable hours. Note command number followed by % such 67 % is my prompt in C shell
67 % @ hours = 4
68 % echo $hours
4
69 % @ rate = 10
70 % echo $rate
10
71 % @ pay = $hours * $rate
72 % echo $pay
40
In Bourn Shell Family
First we go to Korn shell by executing
% ksh
Comments are wrriten by having # infront of it
PS1='ksh' # change the primary prompt to ksh # is usaed for comments
ksh x=20 # notice ksh is he prompt sign
ksh x=$x+1 # we try to add 1 to x, but it does not work
ksh echo $x # see value x became 20+1
20+1
ksh x=20 # reassign 20 to x
ksh ((x=x+1)) # use double parentheses to do arithmetic calculation
ksh echo $x
21 now we have correct result
ksh (( y = x/3 + 2*10 )) # it works no need for back slash in front * or /
# Notice previous statement divides 21(the value x) by 3 and adds 20 to it
ksh echo value of y is : $y
value of y is : 27
ksh b=`expr y \* 5` # This does not work because we need $ in front of y
ksh expr: non-numeric argument # this was the error message
ksh b=`expr $y \* 5`
ksh echo value of b is : $b
value of b is : 135
Another way to do arithmetic calculation in Bourn shell family is to use let statement.
Following are several example using let statement:
ksh let x=50 # assign 50 to x
ksh echo $x
50
ksh let y=$x*5 # multiply value of x by 5 and assign the result to y
ksh echo $y
250
ksh let b=$y/10+12*3
ksh echo $b
61
Some useful Commands
echo $TERM # displays the name of the terminal that you currently using it
echo $cwd # displays your current working directory
pwd # has the same effects echo $cwd which displays the current working directory
stty # displays the setup of your terminal. Note as showing in the following examples the set up of your terminal can be changed.
example executing stty:
Type the following command to see the set up of your terminal. Notice in the output below erase=^? means the back space is equivalent to type control ?
stty
#2 disc;speed 9600 baud; -parity hupcl
erase = ^?
brkint -inpck -istrip icrnl -ixany onlcr
echo echoe echok
Notice in the above erase is set to control ?.
stty all # display all setup. Note you get the same output by typing
stty -a
stty all
#2 disc;speed 9600 baud; 24 rows; 80 columns
erase = ^?; werase = ^W; kill = ^U; intr = ^C; quit = ^\; susp = ^Z
dsusp = ^Y; eof = ^D; eol = ; eol2 = ; stop = ^S
start = ^Q; lnext = ^V; discard = ^O; reprint = ^R; status =
time = 0; min = 1
-parenb -parodd cs8 -cstopb hupcl cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -iuclc
ixon -ixany -ixoff imaxbel
isig icanon -xcase echo echoe echok -echonl -noflsh -mdmbuf -nohang
-tostop echoctl -echoprt echoke -altwerase iexten -nokerninfo
opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel tabs -onoeot
cal # To display a calendar for a particular year
cal 1937 # displays the calendar for 1937
cal 4 1937 # displays the calendar for April 1937
cal 9 1752 # displays the calendar for September 1752. Try it see what you get.
% cal 9 1752
September 1752
Sun Mon Tue Wed Thu Fri Sat
1 2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Below uptime command displays uptime information about the computer you logged on
uptime
13:00 up 13 days, 19:49, 9 users, load average: 1.00, 1.00, 1.00
ruptime # displays uptime information about a computer on your local network
% ruptime # below the output generated by this command
fafnir up 7+07:19, 3 users, load 1.00, 1.00, 1.00
fenris up 7+07:19, 4 users, load 2.00, 2.00, 2.00
freya down 20+12:43
loki up 7+07:19, 6 users, load 0.40, 0.07, 0.03
hostname # displays the name of the system you are using
% hostname # See the output below
loki.emich.edu
whoami displays the name of your userid
% whoami # See the output below
khailany
lock # locks the system, but before doing that asks you to a password to unlock in later time.
% lock # after executing lock the system asks for the key twice
Key: # type the key
Again: # type it again. Then the system is locked for 15 minutes
lock: /dev/ttyp3 on loki.emich.edu. timeout in 15 minutes
time now is Mon Jan 25 14:42:49 EST 1999
Key: # To unlock type the key
leave # reminds you when it is time to leave
leave 559 # makes the system to remind you to leave at 5:59 PM if the current time is 4:00 PM . However if # the current time is 4:00 AM the system will remind you to leave at 5:59 AM
leave +47 # reminds to leave in 47 minutes
bc # activates a very powerful calculator. See pages 79-85 for details.
Unit 3 - Gettinh help
man command activates the manual program which is equivalent to help on many systems. To use the command type man command name.
Examples:
In each of examples below only the first page of the output is shown.
63 % man file
Reformatting page. Please wait... completed
file(1) file(1)
NAME
file - Determines file type
SYNOPSIS
file [-c] [-f file_list] [-m magic_file] file...
STANDARDS
Interfaces documented on this reference page conform to industry standards as follows:
file: XPG4, XPG4-UNIX
65 % man find
Reformatting page. Please wait... completed
find(1) find(1)
NAME
find - Finds files matching an expression
SYNOPSIS
find pathname... expression
STANDARDS
Interfaces documented on this reference page conform to industry standards as follows:
find: XPG4, XPG4-UNIX
Refer to the standards(5) reference page for more information about indus-try standards and associated tags.
66 % man cat
Reformatting page. Please wait... completed
cat(1) cat(1)
NAME
cat - Concatenates or displays files
SYNOPSIS
cat [-benrstuv] file...|-
The cat command reads each specified file in sequence and writes it to standard output.
STANDARDS
Interfaces documented on this reference page conform to industry standards as follows:
cat: XPG4, XPG4-UNIX
92 % whatis cat # give me the definition of command cat
cat (1) - Concatenates or displays files
93 % whereis cat # wher is command cat is stored?
cat: /sbin/cat /usr/bin/cat
C shell family, and Bourn shell family support the above commands. However Korn shell has whence command in addition to whereis and whatis commands. the effect of whence is the same as whereis command. Below are the execution of whence and whereis commands in Korn shell;
236 ksh$ whence cat
/usr/ucb/cat
237 ksh$ whereis cat
cat: /sbin/cat /usr/bin/cat
There is a help command in C shell family, and Bourn shell family. Below help command is executed in C shell.
96 % help
The commands:
man -k keyword lists commands relevant to a keyword
man command prints out the manual pages for a command are helpful; other basic commands are:
cat - concatenates files (and just prints them out)
vi - text editor
finger - user information lookup program
ls - lists contents of directory
mail - sends and receives mail
passwd - changes login password
sccshelp - views information on the Source Code Control System
tset - sets terminal modes
who - who is on the system
write - writes to another user
You could find programs about mail by the command: man -k mail
and print out the man command documentation via: man mail
You can log out by typing "exit".
The same output as the above was produced when help was executed in Bourn and Korn shells. However the output of Bourn Again shell (bash) was different as is shown below.
bash$ help
GNU bash, version 1.14.6(1)
Shell commands that are defined internally. Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
A star (*) next to a name means that the command is disabled.
%[DIGITS | WORD] [&] . filename
: [ arg... ]
alias [ name[=value] ... ] bg [job_spec]
bind [-lvd] [-m keymap] [-f filena break [n]
builtin [shell-builtin [arg ...]] case WORD in [PATTERN [| PATTERN].
cd [dir] command [-pVv] [command [arg ...]]
continue [n] declare [-[frxi]] name[=value] ...
dirs [-l] echo [-neE] [arg ...]
enable [-n] [name ...] eval [arg ...]
exec [ [-] file [redirection ...]] exit [n]
export [-n] [-f] [name ...] or exp fc [-e ename] [-nlr] [first] [last
fg [job_spec] for NAME [in WORDS ... ;] do COMMA
function NAME { COMMANDS ; } or NA getopts optstring name [arg]
hash [-r] [name ...] help [pattern ...]
history [n] [ [-awrn] [filename]] if COMMANDS; then COMMANDS; [ elif
jobs [-lnp] [jobspec ...] | jobs - kill [-s sigspec | -sigspec] [pid
let arg [arg ...] local name[=value] ...
logout popd [+n | -n]
pushd [dir | +n | -n] pwd
read [-r] [name ...] readonly [-n] [-f] [name ...] or r
return [n] select NAME [in WORDS ... ;] do CO
set [--abefhknotuvxldHCP] [-o opti shift [n]
source filename suspend [-f]
test [expr] times
trap [arg] [signal_spec] type [-all] [-type | -path] [name
typeset [-[frxi]] name[=value] ... ulimit [-SHacdfmstpnuv [limit]]
umask [-S] [mode] unalias [-a] [name ...]
unset [-f] [-v] [name ...] until COMMANDS; do COMMANDS; done
variables - Some variable names an wait [n]
while COMMANDS; do COMMANDS; done { COMMANDS }
text editors in Unix:
There are several text editors available in Unix. Some of these are:
ed line editor
edit
ex
vi which is ex in visual mode
pico
Following is a very short summary of vi text editor:
See chapter 5 pages 164 - 168
Entering text i, I, a, A, o, O, r, R
Moving the cursor j, k, l, h
Moving by larger chunks CTRL-d, CTRL-u, CTRL-b, CTRL-F, H, M, L
G, 17 G
Deleting x, 8x, dd, 7d, d
joining two files J
undo u
Unit 4
Unix File Organization
/ root
/bin
/usr/bin
/etc
/dev
/tmp
/users
.filenames
ls
du
quota
compress
uncompress
rm
cd
cp
mkdir
.login , .cshrc and .profilefes
Unix System administrator automatically make these files
C-shell family recognizes three special files .login, .cshrc and .logout. Commands in these files are executed automatically. Whenever a new shell starts the commands in .cshrc are executed. However the commands in .login file are executed only once : when the user logs in. The commands in .logout files are executed when the user logs out. The .profile is used in Bourn shell family. The purpose of .profile, its contents and the functions of the commands are similar to those in .login file in Cshell family
following are examples of .login, .cshrc , .logout and .profile files
% cat .login
if ($?prompt) then
set prompt="% "
set cdpath = ( $HOME/sys /usr/sys /usr/spool )
set path=(/usr/ucb /bin . $HOME/bin /usr/bin /usr/local/bin /usr/new /etc)
set notify
set history = 100
set inc = /usr/include
alias h history
alias pd pushd
alias pop popd
alias cd 'set old=$cwd; chdir \!*'
alias back 'set back=$old; set old=$cwd; cd $back; unset back; dirs'
endif
setenv NEWSPOSTER "/usr/local/unsupport/Pnews -h %h"
setenv NNTPSERVER eecs.umich.edu
alias type cat
alias dir ls -al
alias cd 'cd \!*; echo $cwd'
from
alias tin '/usr/users/lt/sharifi/bin/tin $*'
alias pico /unsupprt/bin/pico
alias lynx /unsupprt/lynx
alias tsh /unsupprt/tcsh
% cat .cshrc
if ($?prompt) then
set prompt="% "
set cdpath = ( $HOME/sys /usr/sys /usr/spool )
set path=(/usr/ucb /bin . $HOME/bin /usr/bin /usr/local /usr/new /etc)
set notify
set history = 100
set inc = /usr/include
alias clrs100o $cwd'
alias h history
alias pd pushd
alias pop popd
alias cd 'set old=$cwd; chdir \!*'
alias back 'set back=$old; set old=$cwd; cd $back; unset back; dirs'
endif
%
% cat .logout
date
time
echo Good bye $user have a good time
%
$ cat .profile
tty -s
if test $? = 0
then
stty dec crt
fi
PATH=$HOME/bin:/usr/ucb:/bin:/usr/bin:/usr/local:/usr/new:/usr/hosts:.
MAIL=/usr/spool/mail/$USER
tset -n -I
export TERM MAIL PATH
biff n
Unit 5
Shell Variables:
Shell recognizes three types of variables, also known as parameters. These are:
1. Keyword variables
2. Positional Parameters
3. Special variables
1. Keyword variables:
A keyword variable is just a programmer supplied variable. We already discussed how to create and assign values to these variables. Recall that in C-shell family the value assigned by the set statement is as follows:
set variable=value # for example
set x=200 # assigns 200 to x and
set name=Linda # Smith assigns Linda Smith to name and
set test= # assigns null value to test (nothing after = sign)
In Bourn shell family shell values are assigned by writing:
variable=value # Note: no set is needed, or use let statement
I=15 # assigns 15 to I and the statement below asigns 100 to y
let y=6*15+10
Both following statements assigns end to signal.
let signal=end
signal=end
2. Positional Parameters:
Whenever a shell program is executed, the name of the program being executed is assigned to $0, and the arguments typed on the command line are assigned to the parameters (variables) $1, $2, $3, $4, $5, $6, $7, $8 and $9. Parameters after $9 can be explicitly referenced as ${nn}. In addition Shell recognizes the following special Positional parameters:
1. $0 contains the name of program being executed.
2. $* contains the name of the postional parameters
3. $@ the same as $*, but when double quoted collectively reference all
positional variables treated as "$1", "$2" ,..etc.
4. $# contains the number of parameters passed to the program
5. $$ contains the process id number of the program being executed.
6. $! contains the process id number of the last program executed in the back ground. Note that programs are executed in the back whenever its name followed by &.
7. $- the current option in effect with set statements
Examples:
Use vi and create the following program. Name the program args1.
To put vi in insert mode type i and to close the file type Escape followed by : then type lower case x followed by return.
Bourn Shell programs: example-1: creating args1 program
vi args1
# Comments in shell programming is written by having # as the first character
# display the name of program being executed
echo $0
# the following statement displays the number of arguments
echo $# arguments passed
# display the value of parameters $1, $2 and S3
echo arg 1 = :$1: arg 2 = :$2: arg 3 = :$3:
let us execute the above program with 10 parameters.
args1 you may want to make 500 dollars per an hour
Below is the output generated by the above program.
args1
10 arguments passed
arg 1 = :you: arg 2 = :may: arg 3 = :want:
Bourn Shell programs:Example-2: creating args2 program
cat args2
# display the name of program being executed
echo $0
# display the process id number of program being executed
echo process id number of program being executed is $$
# display number of parameters
echo $# arguments passed
# display all parameters
echo they are :$*:
Example-3: creating args3 program
# display the name of program being executed
echo $0
# display number of parameters
echo Number of parameters passed is $#
# display all parameters, each on a line
for arg in $*
do
echo $arg
done
Example-4: creating args4 program
cat args4
# display the name of program being executed
echo $0
# display number of parameters
echo Number of arguments passed is $#
# display all parameters, each on a line. Note that $@ used instead of $*
for arg in "$@"
do
echo $arg
done
Example-5: creating shift-example program
cat shift-example
# display the name of program being executed
echo $0
# display number of parameters
echo Number of arguments passed is $#
# display all parameters. Note that we are using only $1 but because of the shift the
# value of parameters will be shifted at each iteration.
for i in $*
do
echo $1
shift
done
3. Special variables:
Shell also recognizes two kinds of these types of variables. The first kind of these variables acts as off/on switches. Some of the built-in (predefined) shell variables are:
ignoreeof if this switch set (it is set by typing set ignoreeof ) then the user must logout either with logout or exit rather than control-d
notify notify about job completion
verbose display full command after history substitution
The second kind of special variables are those variables that store values. Key variables (already discussed) are considered to be of this type. In addition to that, there are built-in shell variables, and environmental variables. Some of these built-in shell variables are:
argv list all arguments for the current program
term type of terminal you are using
path list of directory to be searched to find the program to be executed
cdpath directory to be searched to find sub directory
status to return the status of the last command
the value of these can be changed by a set statement. For example the following command
set path = (. /usr/bin ~/bin)
assigns the list directories to path. Note the items in the list are delimetered by spaces. The period "." means the current directory. If the above statement is executed, whenever a command is typed if it is not a built-in command, shell will search the current directory first. if it is not found then the directory /usr/bin is searched. If it is not found there then the /bin directory within the home directory is searched. Notice that ~ (tilde sign) stands for the home directory.
.
Environmental variables:
Environmental or Global variables are another set of variables which shell maintains for passing values between programs. By convention, environmental variables have uppercase names. setenv command is used to set their values and printenv or echo $varaiable-name is used to display the value of environmental variables.
Example:
setenv TERM vt100 this sets the value of the environmental variable TERM to vt100
printenv TERM or echo $TERM display vt100
PATH contains a list of directory separated by : which will be searched whenever shell needed to find the command to execute. The current directory is specified by :: in PATH. Notice that the value of the built-in variable path (in lowercase letters) also contains the same list as in PATH, but the separators in PATH are : and in path are spaces. Also notice that the value of path is assigned by set statement in the following manner:
set path = ( list of directories separated by spaces and enclosed in two parentheses)
The following six built-in variables have the same names as their corresponding environmental variables:
path, term, user, home, shell and mail
Whenever you change the value of path, term, or user shell updates the values of their corresponding global (environmental) variables.
The information in home, and shell are stored in .login or .profile. Whenever you log in, Unix automatically sets the value of home and shell to their values which are in .login (in C-shell) or .profile (in Bourn shell family). Following are some examples of environmental variables. CDPATH contains the directory to be searched whenever cd is executed
HOME contains the home directory
PS1 contains the primary prompts sign in Bourn shell family.
PS2 contains the secondary prompts sign (usually this sign is >) in Bourn shell family.
ERRNO the error number of the last failed command.
RANDOM a random number generator
SECOND the number of second sign in shell
PPID the process number of the parent processor
PWD the current working directory
LINENO the current line number
Unit-6
Mail :
The Unix mail programs are: mail, elm, MH, Mush and RMAIL.
Mail program starts by typing mail followed by return. The prompt for the mail program is &. when you get the prompt type ? which stands for help. A help menu will be displayed for you. Following is the help menu displayed after typing ?
& ?
Mail Commands
t type messages
n goto and type next message
e edit messages
f give head lines of messages
d delete messages
s file append messages to file
u undelete messages
r reply to messages(to sender and recipients)
R reply to messages(to sender only)
pre make messages go back to /usr/mail
m mail to specific users
q quit, saving unresolved messages in mbox
x quit, do not remove system mailbox
h print out active message headers
! shell escape
ch [directory] chdir to directory or home if none given
A consists of integers, ranges of same, or user names separated
by spaces. If omitted, Mail uses the last message typed.
A consists of user names or distribution names separated by spaces.
Distribution names are defined in .sendrc in your home directory.
&
Create the following file by using vi
vi test-mail
This message is send to you to test the mail system. Reply a conformation that you received it. Thanks.
:x type Escape, then : and lowercase x followed by return to close the file.
to send the above file to user1 type the following command
mail user1 < test-mail
to send the file to user1, user2 and user3 type the following command
mail user1 user2 user3 < test-mail
Another way to send the message is
cat test-mail | mail user1 user2 user3
Example: write shell commands to send the above message to all currently login users.
First we create a file of all such users (call it login-users) by following command
who | tr -s ' ' ' ' | cut -f1 -d' ' > login-users
mail test-mail to all user
|