You are on page 1of 299

Introduction to Unix

Gopan Nair

Systems Manager Financial Services

LogicaCMG 2006. All rights reserved

Schedule
Overview of Unix Login Process Learning the basic commands Getting Help Customizing the shell environment

Understanding file permissions


Job Control

<Presentation title>

Schedule
Using the vi editor Regular expressions and pattern matching Shell programming (Bourne shell)

<Presentation title>

What Is an Operating System?

Disks Users Operating System Programs Consumers Memory CPU Network Printers Resources

<Presentation title>

History of the UNIX Operating System


UNIX is originally developed at AT&T Bell Laboratories in 1969 by Ken Thompson In 1973, Unix was re-written in C language. This led to its popularity In 1983, Bell Lab announced their support for a new version, known as System V. By this time, University of California at Berkeley had also developed a new variant of Unix, known as 4.3 BSD. The 4.3 BSD provided new features such as reliable signals and TCP/IP. Neither AT&T nor BSD is now active in Unix development so most new developments are now defined by a specific vendor or a body such as IEEE (POSIX standard), FreeBSD organization or the Linux community.

<Presentation title>

Different Flavours of Unix?

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>

What Defines a User Account?

<Presentation title>

Creating a User Account


#useradd <your name>

10

<Presentation title>

Setting Your Password


Syntax:

passwd
Example:

Assigns a login password

$ 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>

Your Login Process


When you log in successfully, the first program started is the shell, or command line interpreter. Now that you have logged in you should see a $ prompt awaiting commands. This program gives you a method of communicating with the operating system to run and manage other programs. For the login shell process, Unix makes your home directory as the current directory. Therefore, after login, by default, you will be working with your home directory.

12

<Presentation title>

Your Login Process

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>

Basic Unix Commands

15

<Presentation title>

Using Unix Commands

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.

Note: Unix is case sensitive

16

<Presentation title>

Getting Help

The UNIX reference manual is divided into eight numbered


sections: 1. General User Commands 2. System Calls 3. User-level Library Functions

4. Device Drivers, Protocols


5. File Formats 6. Games (rarely available) 7. Document Preparation

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>

The Online Manual


Syntax: man [-k| X ] keyword | command

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

stty date ps env id who echo clear

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>

The stty Command


Syntax:

stty
Example:

reports or sets terminal control options

$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>

The date Command


Syntax: Date [options] [+format] Displays or sets the date or time Common Options
-u use Universal Time (or Greenwich Mean Time) +format specify the output format %a weekday abbreviation, Sun to Sat %j day of year, 001 to 366 %t <TAB> %y last 2 digits of year, 00 to 99 %D MM/DD/YY date

Example: $ date mmddHHMM[yy] $date +%a%t%D


Mon 06/10/96

sets the system date

$date '+%y:%j'
96:162

21

<Presentation title>

The id Command

Syntax: id

Displays user and group identification for session

Example: $id uid =303 (user3) gid=300 (class)

22

<Presentation title>

The who Command

Syntax: who [am i] whoami

Reports information about users who are currently logged on to a system

Examples: $ who root tty1p5 user1 tty1p4 user2 tty0p3 $ who am i user2 tty0p3

Jul 01 08:01 Jul 01 09:59 Jul 01 10:01 Jul 01 10:01

$ whoami user2 $ tty Prints the name of the tty line

23

<Presentation title>

The echo Command


Syntax:

echo [ String ... ]


Examples:

Writes character strings to standard output

$ echo Hello Class or echo "Hello Class Hello Class $ echo \n Hello Class $ echo $PATH

24

<Presentation title>

The clear Command

Syntax:
clear cls Clears terminal screen

25

<Presentation title>

Navigating the File System

26

<Presentation title>

The File System Hierarchy


/

sbin

usr

dev group

etc

opt

var

stand vmunix

tmp

home

passwd

profile user1 user2 user3

bin

contrib

lib

local

sbin

share man

vue

mail cp

news

tmp

vue

ls

man

sh

tar

vi = directory = file

27

<Presentation title>

Monitoring Disk Usage


Monitor available file system space with df: # df Filesystem

/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

%used Mounted on 89% /opt 80% /home 8% /myfs1 98% /myfs2

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>

Name of current working directory

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>

Understanding Absolute pathnames

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>

Understanding Relative Pathnames


The file system also lets you use relative pathnames. Relative pathnames do not begin with the / that represents the root directory because they are relative to the current directory. You can specify a relative pathname in one of several ways:
As the name of a file in the current directory or with the name of a directory one level below your current directory.

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>

Basic File System Commands

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>

ls List Contents of a Directory

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>

The find Command

Syntax:

find path_list expression

Performs an ordered search through the file system. path_list is a list of directories to search. expression specifies search criteria and actions.

Examples: $ find . -name .profile ./.profile $

38

<Presentation title>

The find Command More options


Option Description
-amin n -atime n -empty -mtime n -size n[ck] file was last accessed n minutes ago file was last accessed 24*n hours ago file is empty and is either a ordinary file or a directory file was last modified n*24 hours ago size of file is n 512 byte blocks (c bytes, k kilobytes)

-exec execute commands

-user uname true if file belongs to uname

39

<Presentation title>

mkdir and rmdir Create and Remove Directories

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>

Disk usage summary: du


du - summarizes disk space usage

$du sk *
538873 WL8.1 50150 2 jre130 logs

registry.xml

346190 tuxedo8.0 282596 wl61

$ du sk .

41

<Presentation title>

The File SystemSummary

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>

Manipulating files and directories

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>

Listing files and directories

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>

Listing files and directories

Following are some of the file types:


- (hyphen) b c d for ordinary files for block-special files for character-special files for directories

l
p s

for symbolic links


for pipe-special files (first in, first out) for local sockets

49

<Presentation title>

How do I create files?


vi touch I/O redirection
Examples: $ ls -Rl / > allfiles $ cat test.c > copytest.c

echo
Examples: $ echo testing > file

ln

50

<Presentation title>

touch Create a file


Syntax touch [options] [date_time] file touch [options] [-t time] file Common Options -a -c change the access time of the file (SVR4 only) dont create the file if it doesnt already exist

-f
-m

force the touch, regardless of read/write permissions


change the modification time of the file (SVR4 only)

-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>

What Can We Do with Files?

ls cat more less head tail wc uniq lp cp mv mv ln rm file strings


52

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>

Listing files and directories


Examples: $ $ ls -l total 4 [1] -rw-r--r--rw-r--r--rw-r--r--rw-r--r-drwxr-xr-x 1 1 1 1 2

[2] larry larry larry larry larry

[3] system system system system system

[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.

3. User name of the file's owner.


4. Group to which the file belongs. 5. Number of bytes in the file. 6. Date and time the file was created or last modified.

7. Name of the file or directory.

53

<Presentation title>

cat Display the Contents of a File


Syntax: cat [file...] Examples:
$ cat remind Your mother's birthday is November 29. $ cat note remind TO: Mike Smith The meeting is scheduled for July 29. Your mother's birthday is November 29. $ cat abc 1234
abc 1234

Concatenate and display the contents of file(s)

CTRL

54

<Presentation title>

more Display the Contents of a File

Syntax: more [filename]... Example: $ more funfile . . . --funfile (20%)-Q or q


Return Space

Display files one screen at a time

Quit more One more line One more page

55

<Presentation title>

less - opposite of more


Syntax: less [filename]... less is a more functional clone of more provided by the GNU project .One added feature of less is that it allows you to scroll back and forth in a file one line at a time with the arrow keys. less reads files incrementally, rather than loading each file into memory like more, so less uses less memory. Example: $ less funfile . . . : Q or q

Quit less

56

<Presentation title>

head Display the End of a File

Syntax: head [-n] [filename]... display first few lines of file(s)

Example: $ head /var/log/messages $ head -12 /var/log/messages

57

<Presentation title>

tail Display the End of a File

Syntax: tail [-n] [filename]... Display the end of file(s)

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>

wc Print the number of bytes, words, and lines in files

Syntax: wc [OPTION]. . . Common Options -c count -m count -l count -w count [FILE]. . .

bytes characters (SVR4) lines words

If no options are specified it defaults to "-lwc".

Example:
$wc l /etc/passwd
59

<Presentation title>

uniq - remove duplicate lines


Syntax: uniq [options] [+|-n] file [file.new] Common Options -d one copy of only the repeated lines -u select only the lines not repeated +n ignore the first n characters -s n same as above (SVR4 only) -n skip the first n fields, including any blanks (space &tab) -f fields same as above (SVR4 only)

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>

mv Move or Rename Files

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>

cmp compare file contents


Syntax cmp [options] file1 file2 [skip1] [skip2] Common Options -l report on each difference -s report exit status only, not byte differences Examples

Given the files file1 and file2


apple grape red
blue

orange mango red


blue

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>

diff reports all differences between files


Syntax diff [options] file1 file2 Common Options
-b ignore trailing blanks -i ignore the case of letters -w ignore <space> and <tab> characters -e produce an output formatted for use with the editor, ed -r apply diff recursively through common sub-directories

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

Remove files Remove directories

<Presentation title>

ln Link Files

Syntax:

ln file new_file ln file [file ...] dest_dir

Link to a file Link files to a directory

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>

Hard Links and Soft Links

There are two kinds of links available for your use:


hard links soft, or symbolic, links When you create a hard link, you are providing another name for the same file. Hard links let you link only files in the same file system. All the link names are on equal footing. It is incorrect to think of one file name as the real name, and another as only a link.

69

<Presentation title>

Hard Links and Soft Links

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

To link a hard link, use the following command format:

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

The rm (remove file) command does not always remove a file.


Assume that a file has several hard links (and therefore more than one file name). In this case, the rm command removes the link between the file I-node number and that file name, but leaves the physical file intact. The rm command physically removes a file only after it has removed the last link between that file and its names.

74

<Presentation title>

file determine file type


Syntax file [options] [-m magic_file] [-f file_list] file Common Options -c -f -h check the magic file for errors in format file_list file_list contains a list of files to examine dont follow symbolic links (SVR4 only)

-L
-m

follow symbolic links (BSD only)


magic_file use magic_file as the magic file instead of /etc/magic

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>

File/Directory Manipulation Commands Summary

ls -l cat more tail cp mv ln rm

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>

File Permissions and Access

77

<Presentation title>

File Permissions and Access

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>

Who Has Access to a File?

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:

contents can be examined. contents can be examined.

Write

contents can be changed. contents can be changed.


file can be used as a command. can become current working directory.

Execute files: directories:

80

<Presentation title>

Permissions

Permissions are displayed with ls -l:


$ ls -l - rw- r-- r-- 1 user3 class 37 Jul 24 11:06 f1 - rwx r-x r-x 1 user3 class 37 Jul 24 11:08 f2 d rwx r-x r-x 2 user3 class 1024 Jul 24 12:03 memo

user (owner)access group access other access file owner file group

81

<Presentation title>

Permissions

symbolic r w \ binary 110 /

r - \ 100 /

r - \ /

100

\ /
Octal 6

\ /
4

\ /
4

82

<Presentation title>

Default File Permissions

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>

Default File Permissions

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>

Default File Permissions

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>

Changing File Permissions

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

Change Permissions of a File

Syntax: chmod mode_list file...


mode_list

Change permissions of file(s)

[who[operator]permission] [ ,... ] user, group, other or all + (add), - (subtract), = (set equal to) read, write, execute
group r-other r--

who operator permission Example:

Original permissions: mode user rw-r--r-- rw-

$ 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>

Changing Permissions Using Letters

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>

Changing Permissions Using Letters

Use one or more of the following to define a user class:


u g o a User (owner) Group All others (besides owner and group) All (user, group, and all others)

89

<Presentation title>

Changing Permissions Using Letters

Use one or more of the following to define an operation:


+ = Add permission Remove permission Assign permission regardless of previous setting

90

<Presentation title>

Changing Permissions Using Letters

Use one or more of the following to define a permission:


r s w x Read Set user or group ID Write Execute

91

<Presentation title>

Changing Permissions Using Letters

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>

Setting Default Permissions

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>

Setting Default Permissions

The umask command has following format:


umask octal-number

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

Syntax: umask [-S] [mode]


Example: default permissions:

User file-creation mode mask user rwgroup rwr-other rw---

set default permissions: rw$ umask g=r,o=

95

<Presentation title>

SUID and SGID Files


It is often the case in Unix that an ordinary user must perform a task which only the root has the power to. Since it is not practical to give every user the root password, setuid and setgid bits are used. A setuid program (or script) assumes the UID of the program owner when it is run (a setgid program assumes the GID of the program group when run). So a setuid root program (or script) has a special bit set such that it can be run by any user to perform rootly tasks. Setting setuid/gid bits: chmod u+s file -rwsr--r-- 1 smith user 1178 Feb 29 15:17 file

chmod g+s file


-rwxr-sr-- 1 smith user 1178 Feb 29 15:17 file

96

<Presentation title>

What's the sticky bit?


The Sticky Bit In the old days when machines did not have very much memory, the sticky bit was used to force a program or file to remain in memory. Today, the sticky bit is largely used on publicly writable directories to ensure that users do not overwrite each other's files. When the sticky bit (t) is turned on for a directory users can have read and/or write permissions for that directory, but they can only remove or rename files that they own. Only root is permitted to turn the sticky bit on or off. chmod +t /test drwxrwxrwt 5 sys sys 543 May 29 09:41 tmp

97

<Presentation title>

touch

Update Timestamp on File

Syntax: touch [-amc] file...

update access and/or modification times of file

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 10245 Aug 24 09:53 secondfile

users users users

25936 Aug 24 09:53 firstfile 0 Aug 25 10:02 newfile 10245 Aug 24 09:53 secondfile

users users users

25936 Aug 24 09:53 firstfile 0 Aug 25 10:02 newfile 10245 Aug 25 10:05 secondfile

98

<Presentation title>

chown

Change File Ownership

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>

The chgrp Command


Syntax: chgrp newgroup filename ...
Changes group access to a file Example: Only the owner of a file (or root) can change the group $ id of the file. uid=303 (user3), gid=300 (class) $ ls -l f3 -rw-r----- 1 user3 class 3967 Jan 24 13:13 f3 $ chgrp class2 f3 $ ls -l f3 -rw-r----- 1 user3 class2 3967 Jan 24 13:13 f3 $ chown user2 f3 $ ls -l f3 -rw-r----- 1 user2 class2 3967 Jan 24 13:13 f3 $

10

<Presentation title>

su

Switch User Id

Syntax: su [user_name]

Change your user ID and group ID designation

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>

The newgrp Command


Syntax: newgrp [group_name] Changes the group ID 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) $ newgrp teacher $ id uid=303 (user3) gid=33 (teacher) $ /usr/local/bin/class_setup $ newgrp return to login group status $ newgrp other Sorry $

10

<Presentation title>

Access Control Lists

Syntax: lsacl filename chacl ACL filename Examples:

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>

File Permissions and Access

Summary

Permissions chmod umask chown chgrp su newgrp

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>

Introduction to the vi Editor

10

<Presentation title>

Cursor Control Commands


k h l j b w e $ ( ) { } H M L move up one line line move one character to the left line move one character to the right move down one line beginning of previous word beginning of next word end of current/next word

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

beginning of the current line


new line below the current line new line above the current line

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>

Using the vi editor

Start the vi program by entering following command:

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>

Using the vi editor

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:

You start the vi program by entering [Return]


the vi command optionally followed by the name [Return] of a new or existing file. [Escape]

11

<Presentation title>

Using the vi editor

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

Use ^ command to move to beginning of current line.


Use $ command to move to END of current line.

Use w command to move forward by word.


Use b command to move back by word. Use e command to move to end of word.

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.

Use G to move to end of file.

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.

Use :set ai to enable auto-indent feature.


Use :set noai to remove auto-indent feature.

Use :set sm (show match) to enable bracket-match feature.

11

<Presentation title>

Quitting the vi editor

To save your work and quit:


Press the Escape key to indicate that you have finished your current work. Type a colon (:) to enter the Last Line mode. Enter command wq. This indicates to the system that you want to write, or save, a copy of the new file in your current directory and quit the vi editor.

12

<Presentation title>

Quitting the vi editor

To quit without saving your work :


Press the Escape key to indicate that you have finished your current work. Type a colon (:) to enter the Last Line mode. Enter command q!. This indicates to the system that you want quit the vi editor WITHOUT saving the changes you made to the file SINCE THE LAST w command.

12

<Presentation title>

Searching for Text: /, n, N

There is one here and one more here and yet one more but not this ONE nor this One

/one <cr> n next

N previous
12

<Presentation title>

Configuring the vi session


To configure the vi environment certain options can be set with the line editor command :set during a vi editing session.
:set ignorecase ignore the case of a character in a search
:set list display tabs and carriage returns :set nolist turn off list option :set nonumber turn off line numbers :set showmode display indication that insert mode is on

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)

overwrite other text)


I insert text at the beginning of the line A append text to the end of the line r replace the character under the cursor with the next character typed

(The arrow keys usually work also)

R Overwrite characters until the end of the


line (or until escape is pressed to change command) o (alpha o) open new line after the current line to type text O (alpha O) open new line before the current line to type text

<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

:w! (file) overwrites file (default is current file)


:q quits edit session w/no changes made :q! quits edit session and discards changes :n edits next file in argument list :f (name) changes name of current file to (name) :r (file) reads contents of file into current edit at the current cursor position (insert a file) :!(command) shell escape :r!(command) inserts result of shell command

12

<Presentation title>

vi editor - global replace

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>

The grep Command


The name of the grep command is an acronym for global regular expression printer.
Basic form : grep <options> <pattern> <filename> Grep can take input from file, standard input or a pipe command ) Options :
-c : Prints the COUNT of number of matching lines
-v : Prints line that DO NOT match -l : prints the names of files with matching pattern -n : Prints line number with each matching output

( output of another

e.g., grep -i error logfile

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

Using Regular Expressions

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.

Regular expressions fall into two groups:


Basic regular expressions Extended regular expressions

13

<Presentation title>

Basic Regular Expressions

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>

Basic Regular Expressions - Rules

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>

Basic Regular Expressions - Rules

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>

Basic Regular Expressions - Rules

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>

Basic Regular Expressions - Rules

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>

Commonly Used Shells

/usr/bin/sh /usr/bin/ksh /usr/old/bin/sh /usr/bin/csh /usr/bin/keysh /usr/bin/rksh /usr/bin/rsh /bin/bash

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>

POSIX Shell Features


A shell user interface with some advanced features:

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>

File Name Completion

$ 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>

Command Line Editing


Provides the ability to modify text entered on current or previous command lines. Press

Esc

to enter command mode.

Recall desired command by either

Pressing until it appears Typing the command number, then G K Use vi commands to edit the line

Do not use the arrow keys Enter

Return

to execute the modified command.

14

<Presentation title>

The User Environment

Your environment describes your session to the programs you run.

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>

Setting Shell Variables


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>

Two Important Variables


The PATH variable

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

$ env ... PATH=/usr/bin:/usr/contrib/bin:/usr/local/bin $ TERM=70092 $ $ tset Erase is Backspace Kill is Ctrl-U $

15

<Presentation title>

echo Displaying the shell Variable values


There are a number of variables automatically set by the shell when it starts. echo

$SHELL :Returns the name of the shell


$? $$ : the exit status of the last command executed : the process number (PID) of this shell - useful for including in filenames, to make them unique

$!
$$*

: the process id of the last command run in the background.


: the current options supplied to this invocation of the shell. : a string containing all the arguments to the shell, starting at $1.

15

<Presentation title>

What Happens When a User Logs In?


login: test12 password:

csh login scripts

POSIX login scripts

/etc/csh.login

system login script


local login script additional local login script
$

/etc/profile

$HOME/.login

$HOME/.profile

$HOME/.cshrc

$HOME/.kshrc

15

<Presentation title>

The Shell Startup Files

If the Shell is ...


Korn (/usr/bin/ksh) Bourne (/usr/old/bin/sh) POSIX (/usr/bin/sh) Restricted (/usr/bin/rsh, /usr/bin/rksh) C (/usr/bin/csh)

The Local Login Script is ...

.profile .kshrc .profile .profile .kshrc .profile

.login .cshrc
<Presentation title>

15

Shell Intrinsics versus UNIX Commands


Shell intrinsics are built into the shell.

set while if for cd ls


UNIX commands live in /usr/bin. more file The system locates UNIX commands by using the PATH variable.

15

<Presentation title>

Looking for Commands whereis

Syntax: $ whereis [-b|-m|-s] command Examples:

Searches a list of directories for a command

$ 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>

Commonly Used Shells

/usr/bin/sh /usr/bin/ksh /usr/old/bin/sh /usr/bin/csh /usr/bin/keysh /usr/bin/rksh /usr/bin/rsh

POSIX shell Korn shell Bourne shell C Shell A context-sensitive softkey shell Restricted Korn shell Restricted Bourne shell

15

<Presentation title>

Shell Advanced Features

15

<Presentation title>

Shell Substitution Capabilities

There are three types of substitution in the shell: Variable substitution Command substitution Tilde substitution

15

<Presentation title>

Variable Substitution

Syntax: $name

Directs the shell to perform variable substitution

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

$ echo $OLDPWD /home/user3/mail $ ls ~/home/user3/mail/from.mike /home/user3/mail/from.jim $ echo ~tricia/file1 /home/tricia/file1


16

<Presentation title>

Displaying Variable Values

$ 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>

Transferring Local Variables to the Environment

Syntax: export variable

Transfer variable to environment

color=lavender count=3

/usr/bin/sh PS1 PATH HOME SHELL count=3 color=lavender


<Presentation title>

$env $export

16

Passing Variables to an Application

$ vi
local variables color=lavender /usr/bin/sh env var TERM=98550

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

STIME TTY 08:46:40 ttyp4 09:55:10 ttyp4

TIME COMMAND 0:00 -sh 0:00 ps -f

PPID 1 4702 4896

C 0 1 18

STIME 08:46:40 09:57:20 09:57:26

TTY ttyp4 ttyp4 ttyp4

TIME 0:00 0:00 0:00

COMMAND -sh ksh ps -f

$ exec ps -f UID PID PPID user3 4702 1

C 0

STIME TTY 08:46:40 ttyp4

TIME COMMAND 0:00 -sh

user3 4896 4702 18 09:57:26 ttyp4 0:00 ps -f $

16

<Presentation title>

Child Processes and the Environment

$ 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

TTY ttyp4 ttyp4 ttyp4

TIME 0:00 0:00 0:00

COMMAND -sh ksh ps -f

TTY ttyp4 ttyp4

TIME COMMAND 0:00 -sh 0:00 ps -f

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

Backslash Single Quotes Double Quotes

\ ' "

17

<Presentation title>

Quoting \

Syntax: \ Example:

Removes the special meaning of the next character

$ 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 '

Syntax: ' Example:

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

Mechanism Backslash Single Quotes Double Quotes

Purpose Escapes next character Escapes all characters inside ' ' Escapes all characters inside " ", except \, $, {variable name}, and $(command)

17

<Presentation title>

Input and Output Redirection

17

<Presentation title>

Input and Output Redirection Introduction

Interpretive programming language Pipelines

Command execution

Shell

export varname Environment settings env variable=value Variable set assignment


Variable substitution $varname $(cmd)

I/O redirection

cmd >file cmd 2>f.err cmd< file

File name generation

*?[]

Command substitution

17

<Presentation title>

stdin, stdout, and stderr

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

keyboard stdout 1 2 terminal

stderr

17

<Presentation title>

Input Redirection <

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

Your mother's birthday is November 29 ?d $

17

<Presentation title>

Output Redirection and >>

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

input text here

Ctrl

17

<Presentation title>

Error Redirection 2> and 2>>

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

[-p] [-p] [-p] [-p] [-p] [-p]

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.

Examples: cat, grep, sort, wc

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>

sort Alphabetical or Numerical Sort

Syntax: sort [-ndutX] [-k field_no] [file...] Sorts lines


Examples:
$ sort funfile

funfile provided as a command line argument

$ tail -1 /etc/passwd user3:xyzbkd:303:30:studentuser3:/home/user3:/usr/bin/sh 1 2 3 4 5 6 7 $ sort -nt: -k 3 < /etc/passwd


$ who > whoson $ sort whoson $ sort -u -k 1,1 whoson

sort logged in users alphabetically sort and suppress duplicate lines

18

<Presentation title>

grep Pattern Matching


Syntax: grep [-cinv] [-e] pattern [-e pattern] [file...] grep [-cinv] -f patterns_list_file [file...] Examples: $ grep user /etc/passwd $ grep -v user /etc/passwd $ grep -in -e like -e love funfile $ who > whoson $ vi whoson :1,$s/ .*//g ZZ $ grep -f whoson /etc/passwd

18

<Presentation title>

Input and Output Redirection

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

Interpretive programming language

Command execution

export varname Environment settings env Variable assignment Variable substitution variable=value set

cmdA | cmdB

Pipelines

Shell

$varname $(cmd)

I/O redirection

cmd >file cmd 2>f.err cmd< file

File name generation

*?[]

Command substitution

18

<Presentation title>

Why Use Pipelines

$ who > temp_file $ wc -l < temp_file $ rm temp_file

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>

Pipelines versus Input and Output Redirection

Input and Output Redirection Syntax: cmd_out > file or cmd_in < file

Pipelines cmd_out | cmd_in

Example: who > who.out sort < who.out

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

Available for redirection:

cmd_A stdin stderr stdout cmd_A stdin stderr

cmd_B stdout stderr stdin stdout cmd_B stderr stdin cmd_C stdout stderr

Available for redirection:

Example: $ grep user /etc/passwd | sort > sort.out


19

<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>

The cut Command

Syntax: cut cut Examples:


$ date | cut -c1-3 $ tail -1 /etc/passwd user3:mdhbmkdj:303:30:student user3:/home/user3:/usr/bin/sh 1 2 3 4 5 6 7 $ cut -f1,6 -d: /etc/passwd $ cut -f1,6 -d: /etc/passwd | sort -r $ ps -ef | cut -c49- | sort -d

-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>

The tee Command

Syntax: tee [-a] file [file. . .] Example: $ who $ who $ who $ who $ who

Tap the pipeline

| | | | |

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>

Printing from a Pipeline

... | lp
Examples:

Located at end of pipe; sends output to printer

$ 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

Syntax: ps [-efl] Example:

Reports process status

$ ps PID TTY 1324 ttyp2 1387 ttyp2

TIME COMMAND 0:00 sh 0:00 ps

$ 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

STIME Jan 1 Jun 23 Jun 23 Jun 23 18:03:21 18:30:23

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

Syntax: command line > cmd.out &


Example: $ grep user * > grep.out & [1] 194 $ ps PID 164 194 195

TTY ttyp2 ttyp2 ttyp2

TIME 0:00 0:00 0:00

COMMAND sh grep ps

20

<Presentation title>

Putting Jobs in Background/Foreground

jobs

fg fg bg bg

[%number] [%string] [%number] [%string]

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>

The nohup Command


Syntax: nohup command line & Example:

Makes a command immune to hangup (logout) no hangup

$ nohup cat * > bigfile & [1] 972 $ Ctrl + d Return login: user3 Password: $ ps -ef | grep cat UID PID PPID user3 972 1 ....

COMMAND cat * > bigfile &

20

<Presentation title>

The nice Command

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>

The kill Command

Syntax: kill [-s signal_name] PID [PID...]

Sends a signal to specified processes.

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>

Introduction to Shell Programming

20

<Presentation title>

Shell Programming Overview

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>

Example Shell Program

$ cat myprog #this is the program myprog date ls -F

Execution of myprog:
$
$ myprog
/usr/bin/sh /usr/bin/sh

PID = 1324

/usr/bin/date

/usr/bin/ls

myprog
# date ls -F
20

PID = 1350 PID = 1361


<Presentation title>

PID = 1362

Passing Data to a Shell Program

$ 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>

Arguments to Shell Programs

Command line: $ sh_program arg1 arg2 . . . argX $0 $1 $2 . . . $X Example:

$ 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>

Arguments to Shell Programs (Continued)

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>

Some Special Shell Variables # and *

# *

The number of command line arguments The entire argument string


$ cat color4 echo There are $# command line arguments echo They are $* echo The first command line argument is $1 $ chmod +x color4 $ color4 red green yellow blue There are 4 command line arguments They are red green yellow blue The first command line argument is red $

Example:

21

<Presentation title>

Some Special Shell Variables # and * (Continued)

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>

The read Command


Syntax: read variable [ variable ... ] Example:

$ 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

Please enter your favorite two colors ->red

21

<Presentation title>

The read Command (Continued)

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>

Writing Shell Functions

Syntax: function funct_name {shell_script}


funct_name () {shell_script} Example:

or

$ function install > { > echo Install file: $1 > chmod +x $1 > mv $1 $HOME/bin > echo Install complete > } $ install myfile Install file: myfile Install complete

install () { echo Install file: $1 chmod +x $1 mv $1 $HOME/bin echo Install complete }

21

<Presentation title>

Additional Techniques

Document shell programs by preceding a comment with a number

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>

Shell Programming Branches

21

<Presentation title>

Return Codes

The shell variable ? holds the return code of the last command executed:
0: non-zero: Example:
$ $ 0 $ $ 0 $ 0

command completed without error (true) command terminated in error (false)


$ false $ echo $? 1 $ cp Usage: cp f1 f2 cp [-r] f1 ... fn d1 $ echo $? 1 $ echo $? 0

true echo $? ls echo $? echo $?

21

<Presentation title>

The test Command

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>

The test Command Numeric Tests

Syntax: [ number relation number ] Compares numbers according to relation Relations:


-lt -le -gt -ge -eq -ne Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to

Example: (Assume X=3)

$ [ $X -lt 7 ] $ echo $? 0

$ [ $X -gt 7 ] $ echo $? 1

22

<Presentation title>

The test Command String Tests

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>

The test Command File Tests

Syntax: test -option filename

Evaluates filename according to option

Example: $ test $ echo 0 $ test $ echo 1

-f funfile $? -d funfile $?

22

<Presentation title>

The test Command Other Operators

Syntax: -o -a ! \( \)

OR AND NOT GROUPING*

Example: $ [ "$ANS" = y -o "$ANS" = Y ] $ [ "$NUM" -gt 10 -a "$NUM" -lt 20 ] $ test -s file -a -r file

* NOTE: the ( ) must be escaped with a backslash.

22

<Presentation title>

File and String checking with test


File checking with test
-r file True if file exists and is Readable -w file True if file exists and is Writable -f file True if file exists and is NOT a directory -d file True if file exists and is Directory -s file True if file exists and is has size > 0 Example: test w /etc/passwd ; echo $? ( display 1 as file is Read-only )

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>

The exit and return Commands

Syntax: exit [arg]

return [arg]

Example:
$ cat exit_test echo exiting program now exit 99 $ cat rtn_test function rtn { echo return from function return 99 }

$ exit_test exiting program now


$ echo $? 99

$ rtn_test return from function


$ rtn

return code from function 99


22

<Presentation title>

Numerical and Logical operations with test


Numerical operations N1 eq N2 True if integers are Equal N1 ne N2 True if integers are Not equal N1 gt N2 True if N1 > N2 N1 lt N2 True if N1 < N2 N1 ge N2 True if N1 >= N2 N1 le N2 True if N1 <= N2

Logical operations
! Binary NOT operator -a Binary AND operator -o Binary OR operator Examples :

test ! r file1 True if file1 is NOT and existing Read-only file


test r file2 a w file2 True if file2 is Readable AND writable test r file3 o w file3 True if file3 is Readable OR writable

22

<Presentation title>

The if Construct

Syntax: (used for single decision branch)


if list A then fi list B

Example:
if test -s funfile then echo funfile exists fi echo hello

22

<Presentation title>

The if-else Construct

Syntax: (used for multi-decision branch)


if
list A then list B else list C

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>

The case Construct

Syntax: (multi-directional branching)


case word in pattern1) list A ;; pattern2) list B ;; patternN) list N ;; esac

Example:

case $ANS in yes) no) esac echo O.K. ;; echo no go ;;

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 Pattern Examples

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>

Shell Programming Branches Summary

Return Code Numeric test String test File test exit n if

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

command listA then command listB

else
fi command listC

command list

Decision based on return code of last command in listA

The string word is compared to each string pattern

23

<Presentation title>

Shell Programming Loops

23

<Presentation title>

Loops an Introduction

Purpose:

Repeat execution of a list of commands.

Control:
Three forms:

Based on the return code of a key command.


while ... do ... done until ... do ... done for ... do ... done

23

<Presentation title>

Arithmetic Evaluaton Using let

Syntax: let expression

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

$ $ $ 1 $ $ 0 $ > > > x

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>

The while Construct

Repeat the loop while the condition is true.

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>

The while Construct Examples

Example A:

Repeat while ans is yes.


ans=yes while [ "$ans" = yes ] do echo Enter a name read name echo $name >> file.names echo "Continue?" echo Enter yes or no read ans done

Example B:

Repeat while there are cmd line arg.


while (( $# != 0 )) do if test -d $1 then echo contents of $1: ls -F $1 fi shift echo There are $# items echo left on the cmd line. done

23

<Presentation title>

The until Construct

Repeat the loop until the condition is true. Syntax:


until
list A do list B done

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>

The until Construct Examples

Example A:

Repeat until ans is no.


ans=yes until [ "$ans" = no ] do echo Enter a name read name echo $name >> file.names echo "Continue?" echo Enter yes or no read ans done

Example B:

Repeat until there are no cmd line arg.


until (( $# == 0 )) do if test -d $1 then echo contents of $1: ls -F $1 fi shift echo There are $# items echo left on the cmd line. done

23

<Presentation title>

The for Construct


For each item in list, repeat the loop, assigning var to the next item in list until the list is exhausted. Syntax: for var in list do list A done Example: $ cat test_for for X in 1 2 3 4 5 do echo "2 * $X is \c" let X=X*2 echo $X done $ test_for 2 * 1 is 2 2 * 2 is 4 2 * 3 is 6 2 * 4 is 8 2 * 5 is 10
<Presentation title>

24

The for Construct Examples

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:

$ cat example_B for FILE in * do if test -d $FILE then ls -F $FILE fi done

24

<Presentation title>

The break, continue and exit Commands

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>

The break and continue Example

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>

Shell Programming Loops Summary

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>

Shell Programming Signals and Traps

24

<Presentation title>

What Is a Signal?
A flag transmitted to a process when certain events occur:

Signal EXIT HUP


INT QUIT KILL TERM

Event Exit the shell Hangup sent to background processes when logging out
Ctrl + c Ctrl +

interrupt quit (generates a core file)

Kill Software termination

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>

The trap Command

Syntax: trap 'cmds' signal [signal...] Example:

Performs cmds upon receipt of signal

$ 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

Syntax: trap '' signal [signal. . .] Example:

$ cat > mytrap2 trap '' INT while true do echo hello done $ mytrap2 hello hello hello ----------hello hello ----------$

Press Press

Ctrl

+
+

(ignored)

Ctrl

24

<Presentation title>

Placement of the trap Command

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>

Signals and Traps Summary

Signal Logout
Ctrl Ctrl + + c \

A flag sent to a shell program upon receipt of certain events

kill kill kill trap

-s signal PID PID -s KILL PID 'cmd' signal

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>

Offline File Storage

25

<Presentation title>

Storing Files to Tape


To store files to a tape you must know the device file name for your tape device. Typical names might be

/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>

The tar Command


Syntax: tar -key [f Examples: Create an archive: $ tar -cvf /dev/rmt/0m myfile Get a table of contents from the archive: $ tar -tvf /dev/rmt/0m Extract a file from the archive: $ tar -xvf /dev/rmt/0m myfile device_file][file. . .]

25

<Presentation title>

The cpio Command

Two modes: cpio -o[cvx]

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]

Examples: Create an archive of all files under current directory:

$ find . | cpio -ocv > /dev/rmt/0m


Restore all files from an archive: $ cpio -icdmv < /dev/rmt/0m
25

<Presentation title>

Debugging Shell Scripts

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>

Debugging Shell Scripts

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>

Debugging Shell Scripts

For example, create a file called bug that contains the following list of commands:
$ cat bug today=`date`

echo enter person


read person mail $1 Hello $person , The system will not be available between 3 and 5 PM today. $today. From System Administrator $

25

<Presentation title>

Debugging Shell Scripts

The corrected script is as follows:


$ cat bug today=`date` echo enter person

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

Building C Programs Using Make Utility

26

<Presentation title>

C programming under Unix


Important c files Stages of C compilation Compiling source code Compiling c++ code Make utility

Shared Library Path

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>

Generic C compiler options


-c : Suppress the linking process and produce a .o file for each source file listed. Several can be subsequently linked later -l<library> :Link with object libraries -L<directory> :Add directory to the list of directories containing object-library routines. -I<pathname> :Add pathname to the list of directories in which to search for #include files -g :invoke debugging option. This instructs the compiler to produce additional symbol table information that is used by a variety of debugging utilities. -D :define symbols either as identifiers (-D<identifier>) or as values (D<symbol>=<value>) in a similar fashion as the #define preprocessor command.

26

<Presentation title>

Compiling a c source code


The complier executable is cc. The Gnu version is gcc Cc source.c : By default the exe file created is a.out. Cc o Myexe src.c : produces exe file with name Myexe. cc file1.o file2.o ...... -o executable : creates an executable file by linking various object files cc calc.c -o calc -lm : compiles the source file and creates the exe file by linking with math library. cc prog.c -L/home/myname/mylibs mylib.a : compile with user created library files cc prog.c -I/home/myname/myheaders : compile with user created include(header) files. Cc DMAX=10 myprog.c : allocating value to macro MAX at compile time (The value can not be changes after compilation).

26

<Presentation title>

Compiling c++ source code


Same as compiling a c source code except:
Name the source file with *.cc extension. Use a c++ or g++ complier in place of cc.

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

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>

The make utility

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>

The make utility


Unix provides several versions of make command; this course describes the default version make(1). On some Unix systems, make(1u) and make(1p) offer some additional features. In addition to its extended feature set, the make(1p) version is POSIX compliant.

27

<Presentation title>

The make utility

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>

The make utility

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

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 make utility description file

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 description file

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 description file

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>

The make utility description file

The make creates a target file using following procedure:


Finds the name of the target file in the description file Finds a dependency line that describes the dependents of the target Ensures that all the target's dependency files exist and are up to date

27

<Presentation title>

The make utility

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>

The make utility

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>

The make utility

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>

Description File Entry Format

The general format of a description file entry is as follows:


[target1 [target2...] ] [:] [ [:] ] [ [dependent...] ] [ [; ] commands ] [ [# ] comment... ]

The items inside brackets are optional. Targets and dependents are file names (strings of letters, numbers, periods, and slashes).

28

<Presentation title>

Description File Entry Format

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>

Description File Entry Format

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>

Using Commands In Description File

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>

Using Commands In Description File

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>

Using Commands In Description File

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>

Using Commands In Description File

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>

Using Commands In Description File

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>

Using Commands In Description File

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>

Using Commands In Description File

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>

Using Commands In Description File

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>

Sample Description File

# 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

y.o: y.c defs

29

<Presentation title>

Sample Description File

# 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>

Sample Description File

# 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>

Customizing User Accounts

29

LogicaCMG 2006. All rights reserved

Why Customize a User Account?


Set the users terminal type. Customize the users prompt. Set the users default printer. Customize the users PATH variable. Define a command line editor.

29

<Presentation title>

Some Sample Customizations

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>

What Happens When a User Logs In?


login: karenk password:

csh login scripts

POSIX login scripts

/etc/csh.login

system login script


local login script additional local login script
$

/etc/profile

$HOME/.login

$HOME/.profile

$HOME/.cshrc

$HOME/.kshrc

29

<Presentation title>

What Happens at CDE Login? login: karenk password:

$HOME/.dtprofile

CDE home session

29

<Presentation title>

You might also like