Professional Documents
Culture Documents
Gopan Nair
Schedule
Overview of Unix Login Process Learning the basic commands Getting Help Customizing the shell environment
<Presentation title>
Schedule
Using the vi editor Regular expressions and pattern matching Shell programming (Bourne shell)
<Presentation title>
Disks Users Operating System Programs Consumers Memory CPU Network Printers Resources
<Presentation title>
<Presentation title>
Product
From
Processor
AIX
HP UX Solaris Tru64Unix
IBM
HP Sun Compaq
PowerPC
PA-RISC/ITANIUM SPARC Alpha
IRIX
LINUX SCO Unix NCR MP-RAS
SGI
SCO NCR
MIPS
Intel Motorola/Intel
Freeware Intel
<Presentation title>
Features of UNIX
Hierarchical file system Multi-tasking Multi-user Provides more than one type of shells (command interpreters) Provides more than one mechanism for inter-process communication (IPC) Excellent networking support through TCP/IP Portability
<Presentation title>
Unix Architecture
<Presentation title>
<Presentation title>
10
<Presentation title>
passwd
Example:
$ passwd Changing password for user3 Old password: New password: Re-enter new password: Password Restrictions: minimum of six characters at least two alpha characters at least one non-alpha character
11
<Presentation title>
12
<Presentation title>
At login time, each shell reads one or more initialization files from your home directory. These files give you the opportunity to set environment variables
The name of this script depends upon the login shell. For Bourne shell, this file is .profile file. For C-shell, this file is .cshrc file.
To customize your login environment, you can modify these startup scripts.
13
<Presentation title>
The Shell Command Interpreter Functions of a Shell: After login you are presented with a shell prompt which waits for you to type commands Validates the command entered by the user. Once the return key is pressed, the shell processes the command When the command is finished, the shell re-displays the prompt. This process continues until the user exits the shell, by typing exit or by pessing ctrl-d, at which time the user is logged out of the UNIX host
14
<Presentation title>
15
<Presentation title>
Most Unix commands are individual programs that perform various tasks. Unix has a large set of commands that are described related reference (man) pages. When you enter a command, the shell interprets the command, and then gives an appropriate response -- that is, the system either runs the program or displays an error message. If the command is valid, the shell directs the operating system to do what is requested.
16
<Presentation title>
Getting Help
8. System Administration
You can see the command summary for each section by typing: man # intro where # is one of the eight section numbers
17
<Presentation title>
in which X is the number of one of the manual sections Examples: $ $ $ $ $ Use Use Use man date man -k copy apropos copy man passwd man 4 passw
Space Return
Display the "date" man page. Display entries with keyword "copy". Display entries with keyword "copy". Display the "passwd" man page-Section 1. Display the "passwd" man page-Section 4.
to view next page. to view next line. to quit the man command.
18
<Presentation title>
General commands
set terminal options display or set the date display information about processes display or change current environment Display you user and group identifications dentify other users logged on to the system Display simple messages to your screen. Clears terminal screen.
19
<Presentation title>
stty
Example:
$stty -a
speed 9600 baud; 45 rows; 107 columns; eucw 1:1:0:0, scrw 1:1:0:0: intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ^@ eol2 = ^@; start = ^Q; stop = ^S; susp = ^Z; dsusp = ^Y; reprint = ^R
$ stty erase ^H to change the erase character from ^? (the delete key) to ^H $ stty echo To stop the display of the characters typed $ stty echo
20
<Presentation title>
$date '+%y:%j'
96:162
21
<Presentation title>
The id Command
Syntax: id
22
<Presentation title>
Examples: $ who root tty1p5 user1 tty1p4 user2 tty0p3 $ who am i user2 tty0p3
23
<Presentation title>
$ echo Hello Class or echo "Hello Class Hello Class $ echo \n Hello Class $ echo $PATH
24
<Presentation title>
Syntax:
clear cls Clears terminal screen
25
<Presentation title>
26
<Presentation title>
sbin
usr
dev group
etc
opt
var
stand vmunix
tmp
home
passwd
bin
contrib
lib
local
sbin
share man
vue
mail cp
news
tmp
vue
ls
man
sh
tar
vi = directory = file
27
<Presentation title>
/dev/vg00/lvol5
/dev/vg00/lvol4 /dev/vg01/myfs1 /dev/vg01/myfs2
kbytes used avail 294912 261523 31352 24576 19333 4978 16384 1174 14331 15893 14006 297
Determine space used by directory subtrees with du: # du -sk /myfs2/* 844 1327 1073 10757 4 /myfs2/data1 /myfs2/data2 /myfs2/data3 /myfs2/data4 /myfs2/lost+found
28
<Presentation title>
The directory in which you are working at any given time is your current, or working directory. If you are uncertain about the directory in which you are working, enter the pwd (print working directory) command. You can use the cd command to move to your home directory from any location.
29
<Presentation title>
Understanding pathnames
A pathname specifies the location of a directory or a file within the file system.
A pathname consists of a series of directory names separated by slashes ( / ) that ends with a directory name or a file name. A pathname that starts with a slash ( / ) is called a full pathname or an absolute pathname. A pathname that starts without a slash ( / ) is called a relative pathname
30
<Presentation title>
You can also think of a full pathname as the complete name of a file or a directory in relation to the root (/) directory. Regardless of where you are working in the file system, you can always find a file or a directory by specifying its full pathname.
31
<Presentation title>
As a pathname that begins with .. (dot dot, the relative pathname for the parent directory).
As a pathname that begins with . (dot, which refers to the current directory). This relative pathname notation is useful when you want to run your own version of an operating system command in the current directory (for example ./ls).
32
<Presentation title>
Understanding directories
In Unix, a directory is a file that has a number of entries. Each entry contains: A filename The I-node number of the file
Every directory contains at least two entries: Filename of .. (dot dot) Filename of. (dot)
33
<Presentation title>
Understanding directories
In the C shell and the Korn or POSIX shell, you may also use a tilde ( ~) at the beginning of relative pathnames.
The tilde character used alone specifies your home directory. The tilde character followed by a user name specifies the home directory of that user. For example,
$ cd ~/mydir
$ cd ~micky/mouse
34
<Presentation title>
pwd
Displays the directory name of your current location in the hierarchy. Sees what files and directories are under the current directory. Changes your location in the hierarchy to another directory.
ls
cd
Finds files.
Creates a directory. Removes a directory.
find mkdir
rmdir
35
<Presentation title>
Syntax: ls [-adlFR] [pathname(s)] Example: $ ls f1 f2 memo $ ls -F f1 f2* memo/ $ ls -aF profile f1 f2* memo/ $ ls memo f1 f2 $ ls -F /home user1/ user2/ user3/ $ ls -F ../user2 f1
36
<Presentation title>
cd Change Directory
Syntax:
cd [ dir_pathname]
Example:
$ pwd /home/user3 $ cd memo; pwd /home/user3/memo $ cd ../..; pwd /home $ cd /tmp; pwd /tmp /home/user3 $ cd; pwd $ cd -
37
<Presentation title>
Syntax:
Performs an ordered search through the file system. path_list is a list of directories to search. expression specifies search criteria and actions.
38
<Presentation title>
39
<Presentation title>
Syntax:
mkdir [-p] dir_pathname(s) rmdir dir_pathname(s)
Example:
$ pwd /home/user3 $ mkdir fruit $ mkdir fruit/apple $ cd fruit $ mkdir grape orange $ rmdir orange $ cd .. $ rmdir fruit rmdir: fruit not empty $ rmdir fruit/apple fruit/grape fruit
40
<Presentation title>
$du sk *
538873 WL8.1 50150 2 jre130 logs
registry.xml
$ du sk .
41
<Presentation title>
File Directory Tree Path name HOME pwd cd ls find mkdir rmdir
A container for data A container for files and other directories Hierarchical structure of a UNIX system Identifies a file's or directory's location in the hierarchy Represents the path name of your login directory Displays your current location in the hierarchy Changes your location in the hierarchy to another directory Lists the contents of a directory Finds files specified by options Creates directories Removes directories
42
<Presentation title>
Managing Files
43
<Presentation title>
What Is a File?
A container for data or a link to a device. Every file has a name and may hold data that resides on a disk. There are several different types of files: Regular files text, data, drawings executable programs Directories Device files
44
<Presentation title>
In Unix, each file is a series of bytes without any structure (format). The directory is the only file that has a structure imposed on it. Each file has one or more names. Each file is also stored in a directory. The directories are stored in the file system. A file system is the useful arrangement of files into directories.
45
<Presentation title>
In Unix, each file has a number of attributes associated with it. Some of these are:
File type File permissions File owner user ID and group ID File size Date/time of creation, last update and last access Number of hard links
46
<Presentation title>
File names
A file name can contain any character except the following because these have special meaning to the shell: Slash ( / )
Backslash ( \ )
Ampersand ( & ) Left- and right-angle brackets (< and >) Question mark ( ? )
Dollar sign ( $ )
Left bracket ( [ ) Asterisk ( * ) Tilde ( ~ ) Vertical bar or pipe symbol ( | )
47
<Presentation title>
File names
You may use a period or dot ( . ) in the middle of a file name. If you use a dot at the beginning of the file name it will be hidden when doing a simple listing of files thru the ls command. The maximum length of a file name depends upon the file system used. Most new file system allow a maximum length of 255 characters (the default). Older file systems allow a maximum file name length of only 14 characters.
48
<Presentation title>
l
p s
49
<Presentation title>
echo
Examples: $ echo testing > file
ln
50
<Presentation title>
-f
-m
-t time use the time specified, not the current time (SVR4 only) When setting the "-t time" option it should be in the form: [[CC]YY]MMDDhhmm[.SS] Examples To create a file: $ touch filename
51
<Presentation title>
Look at the characteristics of a file Look at the contents of a file Look at the contents of a file, one screenful at a time more versatile paginate than more display the first few lines of a file displays the last part of a file display a count of lines, words and characters in a file remove duplicate lines Print a file Make a copy of a file Change the name of a file or directory Move a file to another directory Create another name for a file Remove a file determine the type of file to search a binary file for printable, ASCII characters
<Presentation title>
[4]
[5]
[6]
[7]
101 Jun 5 10:03 file1 75 Jun 5 10:03 file2 75 Jun 5 10:03 file2 65 Jun 5 10:06 file3 32 Jun 5 10:07 project
1. Number of 512-byte blocks used by files in this directory. 2. Number hard of links to each file.
53
<Presentation title>
CTRL
54
<Presentation title>
55
<Presentation title>
Quit less
56
<Presentation title>
57
<Presentation title>
Example: $ tail -1 note soon as it is available. $ tail f [filename]... tracks the file, printing out every new line as
it is added.
58
<Presentation title>
Example:
$wc l /etc/passwd
59
<Presentation title>
Example:
$uniq 3 /etc/passwd The -n option of the uniq command is used to skip the first 3 fields in file, and filter out lines which are duplicates from the 4th field onward.
60
<Presentation title>
cp Copy Files
Syntax: cp [-i] file1 new_file Copy a file cp [-i] file [file...] dest_dir Copy files to a directory cp -r [-i] dir [dir...] dest_dir Copy directories Example: $ ls -F f1 f2* memo/ note remind $ cp f1 f1.copy $ ls -F f1 f1.copy f2* memo/ note remind $ cp note remind memo $ ls -F memo note remind
61
<Presentation title>
Syntax: mv [-i] file new_file Rename a file mv [-i] file [file...] dest_dir Move files to a directory mv [-i] dir [dir...] dest_dir Rename or move directories Example: $ ls -F f1 f2* memo/ note remind $ mv f1 file1 $ ls -F file1 f2* memo/ note remind $ mv f2 memo/file2 $ ls -F file1 memo/ note remind $ ls -F memo file2* $ mv note remind memo $ ls -F file1 memo/ $ls -F memo file2* note remind $ mv memo letters $ ls -F file1 letters/
62
<Presentation title>
green
yellow
The comparison of the two files yields: $ cmp file1 file2 The default it to report only the first difference found.
63
<Presentation title>
Examples For the mon.logins and tues.logins files above, the difference between them is given by:
$ diff file1 file2
The output lists the differences as well as in which file the difference exists. Lines in the first file are preceded by "< ", and those in the second file are preceded by "> ".
64
<Presentation title>
rm Remove Files
Syntax: rm [-if] filename [filename...] rm -r[if] dirname [filename...] Examples: $ ls -F f1 f2 fruit/ memo/ $ rm f1 $ ls -F f2 fruit/ memo/ $ rm -i f2 f2? <user|y| $ rm fruit rm: fruit directory $ rm -r fruit
65
<Presentation title>
ln Link Files
Syntax:
Example:
$ ls -l f1 -rw-rw-r-- 1 user3 class 37 Jul 24 11:06 f1 $ ln f1 /home/user2/f1.link $ ls -l f1 -rw-rw-r-- 2 user3 class 37 Jul 24 11:06 f1 $ ls -l /home/user2 -rw-rw-r-- 2 user3 class 37 Jul 24 11:06 f1.link $ ls -i f1 /home/user2/f1.link 1789 f1 1789 /home/user2/f1.link
/
home user2 f1.link This is a sample file to be copied. user3 f1 You are here.
66
<Presentation title>
Hard Links
Directory /mydir
Inode Table
Data Blocks
f1 file1
101 101
101 -rwxr-xr-x
ln /mydir/f1 /mydir/file1
67
<Presentation title>
Symbolic Links
Directory /mydir
102 103
Inode Table
102 rwxr-xr-x 103 link to /mydir/f2
Data Blocks
f2 file2
ln -s /mydir/f2 /mydir/file2
68
<Presentation title>
69
<Presentation title>
Soft links or symbolic links let you link both files and directories. In addition, you may link both files and directories across different file systems. A symbolic link is actually a distinct file that contains a pointer to another file or directory. This pointer is the pathname to the destination file or directory. Only the original file name is the real name of the file or directory. Unlike a hard link, a soft link is actually only a link.
70
<Presentation title>
Creating Links
ln <existing_pathname> <new_pathname>
If you want to link files and directories across file systems, you can create symbolic links.
To create a symbolic link, add an -s flag to the above ln command and specify full pathnames of both files.
71
<Presentation title>
Understanding Links
Each file has a unique identification, called an I-node number. The I-node number refers to files data stored at a particular location - rather than to the file name.
A directory entry is a link between an I-node number and a file name. This link enables you to link multiple file names to the same I-node number. To display I-node number of files, use ls with the -i flag.
72
<Presentation title>
Understanding Links
Hard links to a file carry the same I-node number. Since an I-node number represents a file within a particular file system, hard links cannot exist between file systems.
However, the soft link is a new file with its own, new I-node number. Since the soft link refers to the original file by name, rather than by I-node number, they work across file systems.
73
<Presentation title>
Deleting Links
74
<Presentation title>
-L
-m
Examples Below we list the output from the command "file filename" for some representative files. /etc/passwd: /usr/bin/ls: test.tar: test.tar.Z: ascii text ELF 32-bit MSB executable SPARC Version 1 USTAR tar archive compressed data block compressed 16 bits
75
<Presentation title>
Display file characteristics Concatenate and display contents of files to screen Format and display contents of files to screen Display the end of files to screen Copy files or directories Move or rename files or directories Link file names together Remove files or directories
76
<Presentation title>
77
<Presentation title>
Access to files is dependent on a user's identification and the permissions associated with a file. This module will show how to Permissions ls (11, ls -l) chmod umask chown chgrp su newgrp Understand the read, write, and execute access to a file Determine what access is granted on a file
Change the file access Change default file access Change the owner of a file Change the group of a file Switch your user identifier Switch your group identifier
78
<Presentation title>
The UNIX system incorporates a three-tier structure to define who has access to each file and directory: user group other The owner of the file A group that may have access to the file Everyone else
The ls -l command displays the owner and group who has access to the file. $ ls -l -rw-r--r-- 1 -rwxr-xr-x 1 drwxr-xr-x 2 user3 user3 user3 | owner class 37 class 37 class 1024 | group Jul 24 11:06 f1 Jul 24 11:08 f2 Jul 24 12:03 memo
79
<Presentation title>
Types of Access
There are three types of access for each file and directory: Read
files: directories:
files: directories:
Write
80
<Presentation title>
Permissions
user (owner)access group access other access file owner file group
81
<Presentation title>
Permissions
r - \ 100 /
r - \ /
100
\ /
Octal 6
\ /
4
\ /
4
82
<Presentation title>
When you create a file or directory, the system automatically supplies a default permission. The following is a typical file permission :
-rw-r--r--
This permission specifies that the owner has read and write permissions while the group and all others have only read permission.
83
<Presentation title>
When you create a file or directory, the system automatically supplies a default permission. The following is typical directory permission :
drwxr-xr-x
This permission specifies that the owner has read and write permissions while the group and all others have read and execute (search) permission.
84
<Presentation title>
The default permission codes that your system provides relieve you from the task of specifying them explicitly every time you create a file or directory. If you want to create your own default permission codes, you must change your user mask with the umask command.
85
<Presentation title>
Use the chmod (change mode) command to set or change the permissions for your files and directories. Please remember that whatever restrictions you impose, the super-user can always override them. There are two ways to specify the permissions for chmod :
You can specify permissions with letters and symbols. You can specify permissions with octal numbers.
86
<Presentation title>
chmod
[who[operator]permission] [ ,... ] user, group, other or all + (add), - (subtract), = (set equal to) read, write, execute
group r-other r--
$ chmod u+x,g+x,o+x file or $ chmod +x file Final permissions: mode user group other rwxr-xr-x rwx r-x r-x
87
<Presentation title>
The following is the format of the chmod command when using letters and symbols:
chmod userclass-operation-permission filename
The userclass-operation-permission entry represents three codes that specify the user class code, operation, and permission code that you want to activate. The filename entry is the name of the file or files whose permissions you want to change.
88
<Presentation title>
89
<Presentation title>
90
<Presentation title>
91
<Presentation title>
Assume a file file1 that has -rw-r--r set of permissions. Now, enter the chmod command with the flags go+w.
This command expands the permissions for both the group (g) and for others (o) by giving them write access (+w) to file1 in addition to the read access they already have.
92
<Presentation title>
If you want to further restrict the permissions established by a program when it creates a file or directory, you must specify a user mask with the umask command. The user mask is a value that determines the access permissions when a file or directory is created.
93
<Presentation title>
The octal-number is a 3-digit octal number that specifies the permissions to be subtracted from default permissions (777 or 666).
Please note that through umask you actually specify permissions that are not to be granted.
94
<Presentation title>
umask
Permission Mask
95
<Presentation title>
96
<Presentation title>
97
<Presentation title>
touch
Examples:
$ ll -rw-r--r-- 1 karenk -rw-r--r-- 1 karenk $ touch newfile $ ll -rw-r--r-- 1 karenk -rw-r--r-- 1 karenk -rw-r--r-- 1 karenk $ touch secondfile $ ll -rw-r--r-- 1 karenk -rw-r--r-- 1 karenk -rw-r--r-- 1 karenk $
users users
25936 Aug 24 09:53 firstfile 0 Aug 25 10:02 newfile 10245 Aug 24 09:53 secondfile
25936 Aug 24 09:53 firstfile 0 Aug 25 10:02 newfile 10245 Aug 25 10:05 secondfile
98
<Presentation title>
chown
Syntax: chown owner [:group] filename ... Changes owner of a file(s) and, optionally, the group ID
Example:
$ id uid=303 (user3), gid=300 (class) $ cp f1 /tmp/user2/f1 $ ls -l /tmp/user2/f1 -rw-r----- 1 user3 class 3967 Jan 24 13:13 f1 $ chown user2 /tmp/user2/f1 $ ls -l /tmp/user2/f1 -rw-r----- 1 user2 class 3967 Jan 24 13:13 f1
Only the owner of a file (or root) can change the ownership of the file.
99
<Presentation title>
10
<Presentation title>
su
Switch User Id
Syntax: su [user_name]
Example: $ ls -l /usr/local/bin/class_setup -rwxr-x--- 1 class_admin teacher 3967 Jan 24 13:13 class_setup $ id uid=303 (user3), gid=300 (class) $ su class_admin Password: $ id uid=400 (class_admin), gid=300 (class) $ /usr/local/bin/class_setup $ log out of su session $
CTRL + D
10
<Presentation title>
10
<Presentation title>
list the ACL for a file change the ACL for a file
$ lsacl funfile (user3.%,rw-)(%.class,r--)(%.%,r--) funfile $ chacl "user2.class=rw,%.%-r" funfile $ lsacl funfile (user2.class,rw-)(user3.%,rw-)(%.class,r--)(%.%,---) funfile $ chacl -d "user2.class" funfile $ ll funfile rw-r----- 1 user3 class 3081 May 28 16:12 funfile $ lsacl funfile (user3.%,rw-)(%.class,r--)(%.%,---) funfile
10
<Presentation title>
Summary
Define who has what access to a file user, group, others read, write, execute Change the permissions of a file Define the default permissions for new files Change the owner of a file Change the group of a file Switch user ID Switch group ID
10
<Presentation title>
10
<Presentation title>
0 (zero) or ^
end of line
beginning of line
beginning of current/previous sentence beginning of next sentence beginning of current/previous paragraph end of current paragraph top line on screen middle line on screen bottom line on screen
10
<Presentation title>
Input modes
Command a A i Mode Name append append insert Insertion Point just after the current character end of the current line just before the current character
---------------------------------------------------------------------------------------
I
o O
Insert
open open
10
<Presentation title>
Deletion commands
Command x D db de dd dw u Function Delete only the current character Delete to the end of the line Delete from the current character to the beginning of the current word Delete from the current character to the end of the current word Delete the current line Delete from the current character to the beginning of the next word undo the last change
10
<Presentation title>
Examples: ~ ~ ~ ~ ~ ~ "file1" [New file] This is a new file, so the system responds by putting your cursor at the top of a screen.
$ vi file1 [Return]
10
<Presentation title>
Type the lowercase letter i to specify that you want to insert text to the new file. The system does not display the i that you enter. Enter following text, pressing Return key after each line:
11
<Presentation title>
Press the Escape key to indicate that you have finished your current work.
Type a colon (:) to enter the Last Line mode. The cursor will be placed on the last line of the screen, where you can enter the commands recognized by the vi editor. Enter lowercase letter w next. This indicates to the system that you want to write, or save, a copy of the new file in your current directory.
11
<Presentation title>
Using vi editor
Use dd command to delete current line. Use <n>dd command to delete <n> lines beginning from the current line. Use dw command to delete current word. Use <n>dw command to delete <n> words beginning from the current word.
11
<Presentation title>
Using vi editor
11
<Presentation title>
Using vi editor
Use Ctrl-F command to move to next screen. Use Ctrl-D command to move forward by half a screen. Use Ctrl-B command to move to previous screen.
Position cursor to any opening bracket and press % key to see the matching closing bracket.
Press A command to add data to end of current line.
11
<Presentation title>
Using vi editor
Use yy command to copy current line. Use <n>yy command to copy <n> lines from the current line.
Use p to paste the lines cut using the yy or the dd command. The lines are pasted AFTER the current line.
Use P to paste the lines cut using the yy or the dd command. The lines are pasted BEFORE the current line.
11
<Presentation title>
Using vi editor
Use . command to repeat the last add, update, delete or paste command. Use x command to delete current character.
Use <n>x command to delete <n> characters beginning from the current character.
Use r<char> command to replace current character with <char> character.
11
<Presentation title>
Using vi editor
Use cw command to change current word with new word(s). Enter new words and press ESC when done. Use <n>cw command to change <n> words beginning with current word with new word(s). Enter new words and press ESC when done. Use C command to replace remainder of the line. Enter new text and press ESC when done.
11
<Presentation title>
Using vi editor
Use :/string command to search the string. Use n command to repeat the previous search.
Use :s/old_string/new_string command to substitute old_string with new_string in the current line.
Use :<m>,<n>s/old_string/new_string command to substitute old_string with new_string in line m thru n.For first line set m to 1. For last line, set n to $.
11
<Presentation title>
Using vi editor
Use :set nu command to see line numbers. Use :set nonu command to remove line numbers.
11
<Presentation title>
12
<Presentation title>
12
<Presentation title>
There is one here and one more here and yet one more but not this ONE nor this One
N previous
12
<Presentation title>
Alternatively, frequently used options can be set automatically when vi is invoked, by use of the .exrc file :set all display all option settings
The following is a sample .exrc file: set number set list set ignorecase
12
<Presentation title>
vi Quick Reference
Cursor Movement Commands: (n) (n)h (n)j (n)k (n)l ^F ^B ^D ^U H M L G 0 $ (n)w (n)b
12
Inserting Text: i insert text before the cursor a append text after the cursor (does not
indicates a number, and is optional left (n) space(s) down (n) space(s) up (n) space(s) right (n) space(s) forward one screen back one screen down half screen up half screen beginning of top line of screen beginning of middle line of screen beginning of last line of screen beginning of last line of file (zero) beginning of line end of line forward (n) word(s) back (n) word(s)
<Presentation title>
vi Quick Reference
Change Commands: (n)cc changes (n) characters on line(s) until end of the line (or until escape is pressed) cw changes characters of word until end of the word (or until escape is pressed) (n)cw changes characters of the next (n) words c$ changes text to the end of the line C changes remaining text on the current line (until stopped by escape key) ~ changes the case of the current character J joins the current line and the next line u undo the last command just done on this line . repeats last change s substitutes text for current character S substitutes text for current line :s substitutes new word(s) for old :<line nos effected> s/old/new/g & repeats last substitution (:s) command. (n)yy yanks (n) lines to buffer y(n)w yanks (n) words to buffer p puts yanked or deleted text after cursor P puts yanked or deleted text before cursor
12
<Presentation title>
vi Quick Reference
File Manipulation: :w (file) writes changes to file (default is current file) :wq writes changes to current file and quits edit session
12
<Presentation title>
Use :s/old_string/new_string command to substitute old_string with new_string in the current line.
Use :<m>,<n>s/old_string/new_string command to substitute old_string with new_string in line m thru n.For first line set m to 1. For last line, set n to $.
Use :g/ old_string/s//new_string command to replace old_string with new_string in the file.
12
<Presentation title>
( output of another
12
<Presentation title>
Matching filenames
If the file names have a common pattern, the shell can match that pattern, generate a list of those names, and automatically pass them to the command as arguments. The asterisk (*), sometimes referred to as a wildcard , matches any string of characters.
Examples: $ ls file *
12
<Presentation title>
Matching filenames
If the file names have a common pattern, the shell can match that pattern, generate a list of those names, and automatically pass them to the command as arguments. The asterisk (*), sometimes referred to as a wildcard , matches any string of characters. For example, file* will match any filename starting with word file.
13
<Presentation title>
Matching filenames
There is one exception to the general rules for pattern matching. When the first character of a file name is a period, you must match the period explicitly. For example, ls * displays the names of all files in the current directory except those that begin with a period. The command ls -a displays all file names, including those that begin with a period.
13
<Presentation title>
Matching filenames
This restriction prevents the shell from automatically matching the relative directory names. These are . (for the current directory) and .. (for the parent directory). In addition to the asterisk (*), shells provide other ways to match character patterns. These are summarized in the next slide.
13
<Presentation title>
Matching filenames
The filename pattern-matching characters are: * ? [] [.-.] [!] matches any string, including NULL matches any single character matches any of the set characters matches any character within the specified range matches any character except those in the set
13
<Presentation title>
New Topic
13
<Presentation title>
Regular Expressions
A regular expression specifies a set of strings to be matched. It contains ordinary text characters and operator characters. Ordinary characters match the corresponding characters in the strings being compared. Operator characters specify repetitions, choices, and other features.
13
<Presentation title>
Basic expressions are built by concatenating simpler basic regular expressions. Ordinary characters and operator characters together make up the set of simple basic regular expressions. You can concatenate any number or combination of simple expressions to create a compound expression that will match any sequence of characters that corresponds to the concatenated simple expressions.
13
<Presentation title>
An ordinary character matches itself. A period (dot) matches any single character except the newline character.
An * (asterisk) matches any number of occurrences of the preceding simple expression, including none.
13
<Presentation title>
The [chars] (Bracket expression) matches a single instance of any one of the characters within the brackets. Ranges of characters can be abbreviated by using a hyphen. For example, [0-9a-z] matches any single digit or lowercase letter.
Within brackets, all characters are ordinary characters except the hyphen (when used in a range abbreviation) and the circumflex (when used as the first character inside the brackets).
13
<Presentation title>
The ^ (Circumflex) - when used at the beginning of a regular expression (or a sub-expression), matches the beginning of a line (`anchors' the expression to the beginning of the line). When used as the first character inside brackets, excludes the bracketed characters from being matched. Otherwise, has no special properties. $Dollar sign - when used at the end of a regular expression, matches the end of a line (`anchors' the expression to the end of the line). Otherwise, has no special properties.
13
<Presentation title>
The \char (Backslash) - except within a bracket expression, escapes the next character to permit matching on explicit instances of characters that are usually basic regular expression operators. The expr expr ... (concatenation) - matches any string that matches all of the concatenated expressions in sequence.
14
<Presentation title>
14
<Presentation title>
Shell Basics
14
<Presentation title>
POSIX shell Korn shell Bourne shell C Shell A context-sensitive softkey shell Restricted Korn shell Restricted Bourne shell Bash shell (GNU Bourne-Again Shell)
14
<Presentation title>
Command aliasing File name completion Command history mechanism Command line recall and editing Job control Enhanced cd capabilities Advanced programming capabilities
14
<Presentation title>
Aliasing
Syntax: alias [name[=string]] Examples: $ alias dir=ls $ alias mroe=more $ alias mstat=/home/tricia/projects/micron/status $ alias laser="lp -dlaser" $ laser fileX request id is laser-234 (1 file) $ alias displays aliases currently defined $ alias mroe displays value of alias mroe mroe=more
14
<Presentation title>
$ more fra ESC ESC $ more frankenstein Return . . . $ more abc ESC ESC $ more abcdef ESC =
1)abcdefXlmnop 2)abcdefYlmnop $ more abcdef ESC ESC Then type X or Y, then . Associated file name will be completed
14
<Presentation title>
Recalling Commands
Uses the history mechanism. Must have the EDITOR environment variable set.
EDITOR=vi export EDITOR At $, press Escand use normal vi commands to scroll through previous commands. k scrolls backward through the command history. j scrolls forward through the command history. nG takes you to command number n. Press to execute the command.
Return
14
<Presentation title>
Esc
Pressing until it appears Typing the command number, then G K Use vi commands to edit the line
Return
14
<Presentation title>
Syntax: env Example: $ env HOME=/home/gerry PWD=/home/gerry/develop/basics EDITOR=vi TERM=70092 ... PATH=/usr/bin:/usr/contrib/bin:/usr/local/bin:\ /home/gerry/bin
14
<Presentation title>
A shell variable is a name that represents a value. The value associated with the name can be modified. Some shell variables are defined during the login process. A user can define new shell variables. Syntax: name=value Example: $ PATH=/usr/bin/X11:/usr/bin $ PS1=
15
<Presentation title>
A list of directories where the shell will search for the commands you type The TERM variable Describes your terminal type and screen size to the programs you run
15
<Presentation title>
$!
$$*
15
<Presentation title>
/etc/csh.login
/etc/profile
$HOME/.login
$HOME/.profile
$HOME/.cshrc
$HOME/.kshrc
15
<Presentation title>
.login .cshrc
<Presentation title>
15
15
<Presentation title>
$ whereis if if : $ $ whereis ls ls : /sbin/ls /usr/bin/ls /usr/share/man/man1.Z/ls.1 $ $ whereis cd cd : /usr/bin/cd /usr/share/man/man1.Z/cd.1 $ $ whereis holdyourhorses holdyourhorses : $
15
<Presentation title>
POSIX shell Korn shell Bourne shell C Shell A context-sensitive softkey shell Restricted Korn shell Restricted Bourne shell
15
<Presentation title>
15
<Presentation title>
There are three types of substitution in the shell: Variable substitution Command substitution Tilde substitution
15
<Presentation title>
Variable Substitution
Syntax: $name
Example: $ echo $PATH /usr/bin:/usr/contrib/bin:/usr/local/bin $ PATH=$PATH:$HOME:. $ echo $PATH /usr/bin:/usr/contrib/bin:/usr/local/bin:/home/user3:. $ echo $HOME /home/user3 $ file_name=$HOME/file1 $ more $file_name <contents of /home/user3/file1>
16
<Presentation title>
Command Substitution
Syntax: $(command)
Example: $ pwd /home/user2 $ curdir=banner $(ls) $ echo $curdir /home/user2 $ cd /tmp $ pwd /tmp $ cd $curdir $ pwd /home/user2
16
<Presentation title>
Tilde Substitution
$ echo $HOME HOME=/home/user3 $ echo ~ /home/user3 $ echo $PWD PWD=/home/user3/tree $ ls ~+/poodle /home/user3/tree/dog.breeds
<Presentation title>
$ echo $HOME /home/user3 $ env HOME=/home/user3 PATH=/usr/bin:/usr/contrib/bin:/usr/local/bin SHELL=/usr/bin/sh $ set HOME=/home/user3 PATH=/usr/bin:/usr/contrib/bin:/usr/local/bin SHELL=/usr/bin/sh color=lavender count=3 dir_name=/home/user3/tree $ unset dir_name
16
<Presentation title>
color=lavender count=3
$env $export
16
$ vi
local variables color=lavender /usr/bin/sh env var TERM=98550
parent
sleeps
local variables local variables color=lavender /usr/bin/sh fork: program and data spaces are duplicated env var TERM=98550 STEP 2: /usr/bin/vi env var TERM=98550 exec: program and local data space are replaced with program and data of requested program (/usr/bin/vi) and program is executed. When program completes, control returns to the parent (/usr/bin/sh) and the prompt is displayed.
STEP 1:
16
<Presentation title>
Monitoring Processes
$ ps -f UID PID user3 4702 user3 4895 $ ksh $ ps -f UID PID user3 4702 user3 4896 user3 4898
PPID 1 4702
C 1 18
C 0 1 18
C 0
16
<Presentation title>
$ export color=lavender $ ksh (create child shell process) $ ps -f UID PID PPID C STIME user3 4702 1 0 08:46:40 user3 4896 4702 1 09:57:20 user3 4898 4896 18 09:57:26 $ echo $color lavender $ color=red $ echo $color red $ exit (exit child shell) $ ps -f (back in parent shell) UID PID PPID C STIME user3 4702 1 0 08:46:40 user3 4895 4702 1 09:58:20 $ echo $color lavender
16
<Presentation title>
Quoting
16
<Presentation title>
Introduction to Quoting
Many characters have "special" meaning to the shell: white space carriage return $ # * <> Quoting removes (escapes) the special meaning of the special characters.
16
<Presentation title>
Quoting Characters
\ ' "
17
<Presentation title>
Quoting \
Syntax: \ Example:
$ echo the \\ escapes the next character the \ escapes the next character $ color=red\ white\ and\ blue $ echo the value of \$color is $color the value of $color is red white and blue $ echo one two \ > three four one two three four
17
<Presentation title>
Quoting '
Removes the special meaning of all characters surrounded by the single quotes
$ color='red white and blue' $ echo 'the value of \$color is $color' the value of \$color is $color $ echo 'the value of $color is' $color the value of $color is red white and blue $ echo 'this doesn't work' > Ctrl + d $ echo '**************' ************
17
<Presentation title>
Quoting "
"
Removes the special meaning of all characters surrounded by the double quotes except \ , $, {variable name}, $(command), and "
Examples:
$ color="red white and blue" $ echo "the value of \$color is $color" the value of $color is red white and blue $ cur_dir="$LOGNAME - your current directory is $(pwd)" $ echo $cur_dir user3 - your current directory is /home/user3/tree $ echo "they're all here, \\, ', \" " they're all here, \, ', "
17
<Presentation title>
Quoting Summary
Purpose Escapes next character Escapes all characters inside ' ' Escapes all characters inside " ", except \, $, {variable name}, and $(command)
17
<Presentation title>
17
<Presentation title>
Command execution
Shell
I/O redirection
*?[]
Command substitution
17
<Presentation title>
File
Reset Break
Device
Menu System Back Space Insert line Delete line Insert char Delete char
File Descriptor
Stop f1 f2 f3 f4 User f5 f6 f7 f8 ~ ` ! 1 @ 2 Q Tab W
stdin
Caps
CTRL
Return
Select
DEL ESC
Shift
0
# 3 $ 4 E R D C
% 5
^ 6
& 7
* 8
( 9
) 0
_ -
= +
.
} ] | \
/ + 8 5 9 6
{ [
7
Prev
: ;
" '
4 1 0
,
Tab
< ,
> .
? /
Shift
Next
2 3 .
Extend char
Extend char
stderr
17
<Presentation title>
Any command that reads its input from stdin can have its input redirected to come from another file.
Example: $ cat remind Your mother's birthday is November 29 $ mail user3 < remind $ mail From user3 Mon July 15 11:30 EDT 1993
17
<Presentation title>
Any command that produces output to stdout can have its output redirected to another file.
Examples: Create/Overwrite $ date > date.out $ date > who.log $ cat > cat.out Create/Append $ ls >> ls.out $ who >> who.log $ ls >> who.log
Ctrl
17
<Presentation title>
Any command that produces error messages to stderr can have those messages redirected to another file. Examples:
$ cp 2> cp.err $ cp 2>> cp.err $ $ more cp.err Usage: cp [-f|-i] cp [-f|-i] cp [-f|-i] Usage: cp [-f|-i] cp [-f|-i] cp [-f|-i]
Create/Overwrite Create/Append
source_file target_file source_file ...target_directory -R|-rsource_directory...target_directory source_file target_file source_file ... target_directory -R|-r source_directory...target_directory
18
<Presentation title>
What Is a Filter?
Reads standard input and produces standard output. Filters the contents of the input stream or a file. Sends results to screen, never modifies the input stream or file. Processes the output of other commands when they are used in conjunction with output redirection.
18
<Presentation title>
wc Word Count
Syntax:
wc [-lwc] [file...] Examples: Counts lines, words, and characters in a file
$ wc funfile funfile provided as a command line argument 116 529 3134 funfile $ $ wc -l funfile 116 funfile $ $ ls > ls.out $ $ wc -w ls.out count the number of entries in your directory 72 ls.out
18
<Presentation title>
18
<Presentation title>
18
<Presentation title>
Summary
cmd < file cmd > file cmd >> file cmd 2 > file.err
A filter
wc sort grep
Redirects input to cmd from file Redirects standard output from cmd to file Redirects standard output from cmd and append to file Redirects errors from cmd to file.err A command that accepts stdin and generates stdout Line, word, and character count Sorts lines alphabetically or numerically Searches for lines that contain a pattern
18
<Presentation title>
Pipes
18
<Presentation title>
Pipelines Introduction
Command execution
export varname Environment settings env Variable assignment Variable substitution variable=value set
cmdA | cmdB
Pipelines
Shell
$varname $(cmd)
I/O redirection
*?[]
Command substitution
18
<Presentation title>
who
wc -l
18
<Presentation title>
The | Symbol
accept stdin
cmd_A
accept stdin
cmd_B*
cmd_C
generates stderr
generates stdout
generates stderr
generates stdout
generates stderr
generates stdout
Example:
$ ps -ef | more $ ls | more $ ls | sort -r | more
*cmd_B must be a filter.
18
<Presentation title>
Input and Output Redirection Syntax: cmd_out > file or cmd_in < file
who | sort
19
<Presentation title>
Redirection in a Pipeline
3 streams for each command: -stdin -stdout -stderr You can redirect streams that are not dedicated to the pipeline: stdout stdin
cmd_B stdout stderr stdin stdout cmd_B stderr stdin cmd_C stdout stderr
<Presentation title>
Some Filters
cut tr Tee
Cuts out specified columns or fields and display to stdout Translates characters Passes output to a file and to stdout Prints and format output to stdout
pr
19
<Presentation title>
-clist [file...] Cuts columns or fields -flist [-dchar][-s][file...] from files or stdin
19
<Presentation title>
The tr Command
Syntax: tr [-s] [string1 [string2]] Examples: $ who | tr -s " " $ $ date | cut -c1-3 | tr "[:lower:]" "[:upper:]" Translates characters
19
<Presentation title>
Syntax: tee [-a] file [file. . .] Example: $ who $ who $ who $ who $ who
| | | | |
sort tee unsorted | sort tee unsorted | sort | tee sorted wc -l tee whoson | wc -l
stdin file
stdout
19
<Presentation title>
The pr Command
Syntax:
pr [-option] [file...] Examples: $ $ $ $ pr -n3 funfile pr -n3 funfile | more ls | pr -3 grep home /etc/passwd | pr -h "User Accounts" Formats stdin and produces stdout
19
<Presentation title>
... | lp
Examples:
$ pr -l58 funfile | lp Request id is laser-226 $ $ ls -F $HOME | pr -3 | Request id is laser-227 $ $ grep home /etc/passwd Request id is laser-228
(standard input). tee homedir | lp (standard input). | pr -h "user accounts" | lp (standard input).
19
<Presentation title>
Pipelines Summary
Pipeline
cmd_out | cmd_in cmd_out | cmd_in_out | cmd_in Cuts out columns or fields to standard output Sends input to standard output and a specified file Prints formatter to the screen, commonly used with lp Translates characters
cut tee pr tr
19
<Presentation title>
Process Control
19
<Presentation title>
The ps Command
$ ps -ef UID PID PPID C root 0 0 0 root 1 0 0 root 2 0 0 root 3 0 0 user3 1324 1 3 user3 1390 1324 22
TTY TIME COMMAND ? 0:20 swapper ? 0:00 init ? 0:16 vhand ? 12:14 statdaemon ttyp2 0:00 -sh ttyp2 0:00 ps -ef
20
<Presentation title>
Background Processing
COMMAND sh grep ps
20
<Presentation title>
jobs
fg fg bg bg
Displays jobs currently running Suspends a job running in the foreground stty susp ^Z Ctrl + z Brings job number to the foreground or any job whose command line begins with string. Transfers job number to the background or any job whose command line begins with string.
20
<Presentation title>
$ nohup cat * > bigfile & [1] 972 $ Ctrl + d Return login: user3 Password: $ ps -ef | grep cat UID PID PPID user3 972 1 ....
20
<Presentation title>
Syntax:
nice [-N] command_line Runs a process at a lower priority N is a number between 1 and 19.
Example: $ nice -10 cc myprog.c -o myprog $ nice -5 sort * > sort.out & $
20
<Presentation title>
Example:
$ cat /usr/share/man/cat1/* > bigfile1 & [1] 995 $ cat /usr/share/man/cat2/* > bigfile2 & [2] 996 $ kill 995 [1] - Terminated cat /usr/share/man/cat1/* > bigfile1 & $ kill -s INT %2 [2] + Interrupt cat /usr/share/man/cat2/* > bigfile2 & $ kill -s KILL 0
20
<Presentation title>
20
<Presentation title>
A shell program is a regular file containing UNIX system commands. The file's permissions must be at least "read" and "execute." To execute, type the name of the file at the shell prompt. Data can be passed into a shell program through environment variables command line arguments user input
20
<Presentation title>
Execution of myprog:
$
$ myprog
/usr/bin/sh /usr/bin/sh
PID = 1324
/usr/bin/date
/usr/bin/ls
myprog
# date ls -F
20
PID = 1362
$ color=lavender $ cat color1 echo You are now running program: color1 echo the value of the variable color is: $color $ chmod +x color1 $ color1 You are now running program: color1 the value of the variable color is: $ export color $ color1 You are now running program: color1 the value of the variable color is: lavender
20
<Presentation title>
$ cat color3 echo You are now running program: $0 echo The value of command line argument \#1 is: $1 echo The value of command line argument \#2 is: $2 $ chmod +x color3 $ color3 red green You are now running program: color3 The value of command line argument #1 is: red The value of command line argument #2 is: green
21
<Presentation title>
This shell program will install a program, specified as a command-line argument to your bin directory:
$ cat my_install echo $0 will install $1 to your bin directory chmod +x $1 mv $1 $HOME/bin echo Installation of $1 is complete $ chmod +x my_install $ my_install color3 my_install will install color3 to your bin directory Installation of color3 is complete $
21
<Presentation title>
# *
Example:
21
<Presentation title>
This enhanced example of the install program accepts multiple command-line arguments:
$ cat > my_install2 echo $0 will install $# files to your bin directory echo The files to be installed are: $* chmod +x $* mv $* $HOME/bin echo Installation is complete $ chmod +x my_install2 $ my_install2 color1 color2 my_install2 will install 2 files to your bin directory The files to be installed are: color1 color2 Installation is complete
21
<Presentation title>
$ cat color6 echo This program prompts for user input echo "Please enter your favorite two colors -> \c" read color_a color_b echo The colors you entered are: $color_b $color_a $ chmod +x color6 $ color6 This program prompts for user input blue The colors you entered are: blue red $ color6 This program prompts for user input Please enter your favorite two colors ->red blue tan The colors you entered are: blue tan red
21
<Presentation title>
This enhanced example of the install program prompts the user to input the file names to be installed:
$ cat my_install3 echo $0 will install files into your bin directory echo "Enter the names of the files -> \c" read filenames chmod +x $filenames mv $filenames $HOME/bin echo Installation is complete $ chmod +x my_install3 $ my_install3 my_install3 will install files into your bin directory Enter the names of the files -> f1 f2 Installation is complete
21
<Presentation title>
or
$ function install > { > echo Install file: $1 > chmod +x $1 > mv $1 $HOME/bin > echo Install complete > } $ install myfile Install file: myfile Install complete
21
<Presentation title>
Additional Techniques
sign (#). sh shell_program arguments shell_program does not have to be executable. shell_program does have to be readable. sh -x shell_program arguments Each line of shell_program is printed before being executed. Useful for debugging your program.
21
<Presentation title>
21
<Presentation title>
Return Codes
The shell variable ? holds the return code of the last command executed:
0: non-zero: Example:
$ $ 0 $ $ 0 $ 0
21
<Presentation title>
Syntax: test expression or [ expression ] The test command evaluates the expression, and sets the return code. Expression Value true false Return Code 0 non-zero (commonly 1)
The test command can evaluate the condition of Integers Strings Files
22
<Presentation title>
$ [ $X -lt 7 ] $ echo $? 0
$ [ $X -gt 7 ] $ echo $? 1
22
<Presentation title>
Syntax: [ string1 = string2 ] [ string1 != string2 ] Example: $ X=abc $ [ "$X" = "abc" ] $ echo $? Determines string equivalence Determines string nonequivalence $ X=abc $ [ "$X" != "abc" ] $ echo $?
22
<Presentation title>
-f funfile $? -d funfile $?
22
<Presentation title>
Syntax: -o -a ! \( \)
Example: $ [ "$ANS" = y -o "$ANS" = Y ] $ [ "$NUM" -gt 10 -a "$NUM" -lt 20 ] $ test -s file -a -r file
22
<Presentation title>
String checking
string1 = string2 True if strings are same String1 != string2 True if strings are different -n string -z string String True if string has non-zero length True if string has zero length True if string is NOT NULL
22
<Presentation title>
return [arg]
Example:
$ cat exit_test echo exiting program now exit 99 $ cat rtn_test function rtn { echo return from function return 99 }
<Presentation title>
Logical operations
! Binary NOT operator -a Binary AND operator -o Binary OR operator Examples :
22
<Presentation title>
The if Construct
Example:
if test -s funfile then echo funfile exists fi echo hello
22
<Presentation title>
fi
Example:
if [ $X -lt 10 ] then echo X is less than 10 else echo X is not less than 10 fi
22
<Presentation title>
Example:
case $OPT in 1) 2) 3) *) esac echo echo echo echo option 1 option 2 option 3 no option ;; ;; ;; ;;
23
<Presentation title>
The case construct patterns use the same special characters that are used to generate file names.
$ cat menu_with_case echo COMMAND MENU echo d to display time and date echo w to display logged-in users echo l to list contents of current directory echo Please enter your choice : read choice case $choice in [dD]*) date ;; [wW]*) who ;; l*|L*) ls ;; *) echo Invalid selection ;; esac $
23
<Presentation title>
Return value from each program echo $? [ $num1 -lt $num2 ] [ $string1 = $string2 ] test -f filename Terminates program and sets the return code
case word pattern1) ;; pattern2) ;; *) ;; esac in command list command list
else
fi command listC
command list
23
<Presentation title>
23
<Presentation title>
Loops an Introduction
Purpose:
Control:
Three forms:
23
<Presentation title>
or
(( expression
))
Example:
$ x=10 $ y=2 $ let x=x+2 $ echo $x 12 $ let "x = x / (y + 1)" $ echo $x 4 $ (( x = x + 1 )) $ echo $x 5
x=12 let "x < 10" echo $? (( x > 10 )) echo $? if (( x > 10 )) then echo x greater else echo x not greater fi greater
23
<Presentation title>
Syntax:
while list A do list B done
Example: $ cat test_while X=1 while (( X <= 10 )) do echo hello X is $X let X=X+1 done
$ test_while hello X is 1 hello X is 2 . . . hello X is 10
23
<Presentation title>
Example A:
Example B:
23
<Presentation title>
Example:
$ cat test_until X=1 until (( X > 10 )) do echo hello X is $X let X=X+1 done $ test_until hello X is 1 hello X is 2 . . . hello X is 10
23
<Presentation title>
Example A:
Example B:
23
<Presentation title>
24
Example A:
$ cat example_A for NAME in $(grep home /etc/passwd | cut -f1 -d:) do mail $NAME < mtg.minutes echo mailed mtg.minutes to $NAME done
Example B:
24
<Presentation title>
break [n]
Terminates the iteration of the loop and skips to the next command after [the nth] done. Stops the current iteration of the loop and skips to the beginning of the next iteration [of the nth] enclosing loop. Stops the execution of the shell program, and sets the return code to n.
continue [n]
exit [n]
24
<Presentation title>
while true do echo "Enter file to remove: \c" read FILE if test ! -f $FILE then echo $FILE is not a regular file continue fi echo removing $FILE rm $FILE break done
24
<Presentation title>
let expression ((expression)) while condition is true do ... done until condition is true do ... done for var in list do ... done break [n] continue[n] exit [n]
evaluate an arithmetic expression evaluate an arithmetic expression while until for break out of loop terminate current iteration of loop terminate the program
24
<Presentation title>
24
<Presentation title>
What Is a Signal?
A flag transmitted to a process when certain events occur:
Event Exit the shell Hangup sent to background processes when logging out
Ctrl + c Ctrl +
The kill command transmits an explicit signal to a process: kill [ -s signal ] PID
24
<Presentation title>
What Is a Trap?
Captures a signal Provides opportunity to perform some action or procedure when signal is sensed
24
<Presentation title>
$ cat > mytrap trap 'echo bye; exit' INT QUIT TERM while true do echo hello done $ mytrap hello hello hello Ctrl bye ----------Press $
24
<Presentation title>
Ignoring Signals
$ cat > mytrap2 trap '' INT while true do echo hello done $ mytrap2 hello hello hello ----------hello hello ----------$
Press Press
Ctrl
+
+
(ignored)
Ctrl
24
<Presentation title>
Place trap at beginning of program to manage the removal of temporary files if program is aborted: trap 'rm /tmp/tempfile;exit' INT QUIT TERM Place trap before critical sections of code to ignore signals: trap '' INT QUIT TERM
Reset trap to default action when sensitive code section is complete: trap INT QUIT TERM
25
<Presentation title>
Signal Logout
Ctrl Ctrl + + c \
Exit EXIT signal Interrupt INT signal Quit QUIT signal Transmit signal to PID Send TERM signal, software termination, to PID Send "sure kill" KILL signal to PID Capture signal and run cmd
25
<Presentation title>
25
<Presentation title>
/dev/rmt/0m 9-track tape or DDS tape(old name) /dev/rmt/c0t3d0BEST 9-track tape or DDS tape Ask your system administrator which device file accesses the tape drive. Commands to perform file backups include tar cpio
25
<Presentation title>
25
<Presentation title>
Generate an archive. Read list of files from stdin. Archive is written to stdout. Restore from an archive. Archive is read from stdin.
cpio -i[cdmtuvx]
<Presentation title>
At times you may need to debug a program to find and correct errors. Two options to the sh command can help you debug a program: sh -v shell_script_name The -v (verbose) option prints the shell input lines as they are read by the system.
25
<Presentation title>
sh -x shell_script_name The -x (execute) option prints the commands and their arguments as they are executed. To try these two options, create a shell program that has an error in it.
25
<Presentation title>
For example, create a file called bug that contains the following list of commands:
$ cat bug today=`date`
25
<Presentation title>
read person
mail $1 << _DATA_ Hello $person , The system will not be available between 3 and 5 PM today. $today. From System Administrator _DATA_ $
25
<Presentation title>
New Topic
26
<Presentation title>
26
<Presentation title>
C Compilers in UNIX
GNU C - Linux IBM Visualage C compiler - IBM AIX HP C/ANSI C Compiler Sun Forte C compiler - HP_UX - SUN solaris
26
<Presentation title>
C compilation model
26
<Presentation title>
Stages of c compilation
Pre-processor : removes comments and interprets special preprocessor directives denoted by #. Compiler : Translates source code received from pre-processor to assembly code Assembler : The assembler creates object code. On a UNIX system you may see files with a .o suffix (.OBJ on MSDOS) to indicate object code files.
Link editor : If a source file references library functions or functions defined in other source files the link editor combines these functions (with main()) to create an executable file. External Variable references are resolved here .
26
<Presentation title>
26
<Presentation title>
26
<Presentation title>
Lint is a UNIX utility that is particularly good at type checking of variable and function assignments, efficiency, unused variables and function identifiers, unreachable code and possibly memory leaks .
26
<Presentation title>
The make utility builds up-to-date versions of programs. It is useful for large projects in which multiple source files are combined to form a single program or for building a set of programs that are part of a single product or application.
The make command accepts options to control or modify how the building process is performed. Please note that the make utility does not address the problem of maintaining more than one version of the same source file.
26
<Presentation title>
By using the make utility to maintain programs, you can do the following:
Combine the instructions for creating a large program in a single file Define macros to use within the make description file Use shell commands Create or update libraries Include files from other programs
26
<Presentation title>
27
<Presentation title>
The make utility works by comparing the creation date of a program to be built, called the target or target file, with the dates of the files that make it up, called dependency files or dependents. If any of a given target's dependents are newer than the target, make considers that the target is out of date. In this case, make rebuilds the target by performing the necessary compiling, linking, or other steps.
27
<Presentation title>
Each dependent can also be a target; for example, an executable program is made from object modules, which are in turn made from source files. The make utility uses following sources of info:
A description file that you create File names Time stamps of the files from the file system A set of rules that tell make how to build files
27
<Presentation title>
The make utility depends on files' time stamps. For make to work properly on a distributed system, the date and time on all systems in the network must be synchronized. You start the make utility in the directory that contains the description file. The syntax of the make command is as follows:
make [ [-f ] makefile ] [ options ] [ targets ] [ macro definitions ]
27
<Presentation title>
The description file tells make how to build the target by defining what dependencies are involved and what their relationships are to the other files in the procedure. The description file contains following info:
Definitions of macros in the description file One or more target names Dependency file names that make up the target files Commands that create the target files from the dependents
27
<Presentation title>
The make utility usually looks for a description file named either makefile or Makefile. You can override the default file name by using the -f option to the make utility to specify the name of the desired description file, as in the following example: % make -f my_makefile
27
<Presentation title>
The make utility determines what files to create to get an up-to-date copy of the target by checking the dates of the dependency files. If any dependency file was changed more recently than the target, make creates all the files that are affected by the change, including the target. In most cases, the description file is easy to write and does not change often.
27
<Presentation title>
27
<Presentation title>
Determines if the target is current with respect to its dependents. Creates the target by one of the following methods if the target or one of the dependents is out of date:
Executes commands from the description file Uses internal rules to create the file (if they apply) Uses default rules from the description file
27
<Presentation title>
If all files described on the dependency line are up to date, make indicates that the target is up to date and then stops. If any dependents are newer than their targets, make recreates only those targets that are out of date. Any missing files are deemed to be out of date.
The make process works from the top down in determining what targets need to be rebuilt and from the bottom up in the actual rebuilding stage.
27
<Presentation title>
When the make utility runs commands to create a target, it replaces macros with their values, echoes each command line to the standard output, and then runs the command. The make utility runs commands that it can execute directly, such as rm or cc, without invoking a new shell. The utility invokes each command line that includes shell functions, such as pipes or redirection, in a new shell.
28
<Presentation title>
The items inside brackets are optional. Targets and dependents are file names (strings of letters, numbers, periods, and slashes).
28
<Presentation title>
The make command recognizes wildcard characters, such as asterisks ( * ) and question marks ( ? ). Each line in the description file that contains a target name is called a dependency line. The dependency line is followed by one or more command lines that specify the process steps to create the target.
28
<Presentation title>
To place comments in the description file, use a number sign ( # ) to begin the comment text. The make utility ignores this sign and all characters on the same line after this sign. The make utility also ignores blank lines. You can enter lines that are longer than the line width of the input device by putting a backslash ( \ ) at the end of the line. Do not extend comment lines in this way; begin each new comment line with its own number sign.
28
<Presentation title>
A command is any string of characters, except a number sign or a newline character. Commands can appear after a semicolon ( ; ) on a dependency line or on lines immediately following a dependency line. Each command line after the dependency line must begin with a single tab character.
28
<Presentation title>
When you define command sequences for the targets in the description file, either specify one command sequence for each target or specify separate command sequences for special sets of dependencies. To use one command sequence for every use of the target, use a single colon ( : ) following the target name on the dependency line.
28
<Presentation title>
For example, the following lines define a target, test, with a set of dependency files and a set of commands to create the target:
test: test: dependency list1 command list1 dependency list2... command list2
28
<Presentation title>
A target name can appear in several places in the description file with different dependency lists, but there can be only one command list associated with the target name. The make utility finds all the dependency lines for a given target and concatenates all their dependency lists into a single list. When any of the dependents have been changed, make can run the commands in the one command list to create the target.
28
<Presentation title>
To specify more than one set of commands to create a particular target file, enter more than one dependency definition. Each dependency line must have the target name followed by two colons ( :: ), a dependency list, and a command list that make uses if any of the files in the dependency list changes.
28
<Presentation title>
The following define two separate processes to create the target file test:
test: test: dependency list1 command list1 dependency list2 command list2
If any of the files in dependency list1 changes, make runs command list1; if any of the files in dependency list2 changes, make runs command list2.
28
<Presentation title>
To avoid conflicts, a given dependency file cannot appear in both dependency list1 and dependency list2. Make runs the commands on each command line independently of preceding or subsequent command lines. In the following example, the cd command has no effect on the cc command :
test: test.o cd /u/tom/newtest cc main.o subs.o -o test
29
<Presentation title>
To make the cd command affect the cc command, place both commands on the same line, separated by a semicolon. For example:
test: test.o cd /u/tom/newtest; cc main.o subs.o -o test
29
<Presentation title>
# Make prog from 3 object files prog: x.o y.o z.o # Use the cc program to make prog cc x.o y.o z.o -o prog # Make x.o from 2 other files x.o: x.c defs # Use the cc program to make x.o cc -c x.c # Make y.o from 2 other files
29
<Presentation title>
# Use the cc program to make y.o cc -c y.c # Make z.o from z.c z.o: z.c # Use the cc program to make z.o cc -c z.c
29
<Presentation title>
# Make prog from 3 object files prog: x.o y.o z.o # Use the cc program to make prog cc x.o y.o z.o -o prog # Use the file defs and the appropriate .c file # when making x.o and y.o x.o y.o: defs
29
<Presentation title>
29
29
<Presentation title>
export TERM=vt100 export PS1=$PWD $ export LPDEST=laser export PATH=$PATH:/usr/local/bin export EDITOR=vi export HISTSIZE=50 export HISTFILE=~/.sh_history
29
<Presentation title>
/etc/csh.login
/etc/profile
$HOME/.login
$HOME/.profile
$HOME/.cshrc
$HOME/.kshrc
29
<Presentation title>
$HOME/.dtprofile
29
<Presentation title>