You are on page 1of 251

UNIX Power User Training

Unix Courseware Version 1.2

System Consultant

System Consultant

Introduction to UNIX

System Consultant

What is an Operating System?


Software that manages (allocates and de-allocates) system resources in an efficient and secure manner System resources consist of hardware (e.g. terminals, printers, storage device, etc) and software (e.g. application programs, language libraries, etc.)

System Consultant

System Resources
System Resources Hardware Components

Software

System Software

Application Software

System Consultant

Different Types of Operating Systems


Single-user, single-process operating systems: allow only one user at a time to use the computer system. The user can execute/run only one process at a time.
Examples: DOS, Windows 3.1

Single-user, multi-process operating systems: allow a single user to use the computer system; however, the user can run multiple processes at the same time.
Example: OS/2
System Consultant
5

Different Types of Operating Systems


Multi-user, multi-process operating systems: allow multiple users to use the computer system simultaneously. Each user can run multiple processes at the same time.
Examples: UNIX, Windows XP

System Consultant

UNIX Operating System


UNIX is a multi-user, multi-processing, portable(?) operating system. UNIX is designed to facilitate programming, text processing and communication

System Consultant

Components of UNIX: perspective 1

System Consultant

Components of UNIX: perspective 2

System Consultant

History of UNIX

Invented by Ken Thompson at AT&T in 1969 First version written in assembly language
single user system, no network capability

Thompson, Dennis Ritchie, Brian Kernighan


rewrote Unix in C: processor/architecture independent

Unix evolution:
I. II. III. IV. Bell Labs, USL, Novell, SCO AIX, Ultrix, Irix, Solaris, BSD, FreeBSD, Mach, OS X Linux, Redhat, Suse,

System Consultant

10

What Can UNIX Do for You?


Users Support Tools
Text processing (vi, sed, awk) Filing system E-mail and networking Electronic databases

Programmers Support Tools


Programming languages & compilers (C, C++) Shell scripts Programming Workbench
Source Code Control System (SCCS) Revision Control System (RCS)
System Consultant
11

Some Standard UNIX Shells

System Consultant

12

Command Source and Destination

System Consultant

13

Command Line Structure


mp% command [-options] [arguments]
Command prompt Command name Command modifier; usually one character preceded by + or - sign Arguments can be: 1. More information 2. Object identifiers 3. Names of files

Notes: UNIX is case sensitive!!!! Example: the command ls l is not the same as LS L Must be a space between the command, options and arguments No space between the plus or minus sign and the option letter Option letters must be typed exactly as they are indicated, uppercase or lowercase Fields enclosed in [ ] are optional Must press [Return] after you have completed entry of a command
System Consultant
14

Correcting Mistakes
Note: UNIX is case sensitive (use lowercase) Key Pressed
Backspace, Ctrl-h Ctrl-c Ctrl-r Ctrl-s Ctrl-w Ctrl-u

Result
Back up & erase last character Terminates the current command Redraws the current command line Stops scrolling of output on screen (Ctrl-q to resume/start scrolling) Erases a word on command line Erases/deletes entire command line
System Consultant
15

Command Line Structure


How do the results of the following commands differ? According to the synopsis of the sort command, what parts of the sort command are required?
sort list sort f list sort o sorted list
Command name Command option Option argument Command argument

System Consultant

16

The UNIX File System

System Consultant

17

A Directory Hierarchy

System Consultant

18

Sample UNIX File Structure (NIU, CS)


UNIX supports tree-like directory structure
Root Directory Root subdirectories

dev tty null

etc skel

home mp
z036473

usr ux bin local ucb

Home Directory

z036473 subdirectory

csci330

.cshrc
System Consultant

.logout

Files in the z036473 directory


19

Directory Types
Root Directory: /
The first directory in any UNIX file structure Always begin with the forward slash (/)

Home Directory: $HOME or ~


Created by system administrator This is where you are when you first log in! Under $HOME, you may create your own directory structure Type: cd [Return] takes you $HOME

Current Working Directory: .


The Directory you are currently working in Also called Current Working Directory (cwd)

Parent Directory: ..
The directory immediately above your current working directory.
System Consultant
20

File Types

- Text - Binary

- Root (/) - Home (~) - Working (.) - Parent (..)


21

System Consultant

Paths and Pathnames


Two ways of locating a file or a directory: By Using Absolute Pathname
Full pathname Traces a path from root to a file or a directory Always begins with the root (/) directory! Example: /home/ux/krush/unix/assignments/assign1.sp04 Traces a path from the cwd to a file or a directory No initial forward slash (/) Two dots (..) goes up one level on file structure Dot (.) points to current working directory (cwd) Example: unix/assignments/assign1.sp04
System Consultant
22

By Using Relative Pathname

Relative Pathnames for file3

Absolute Pathname: /usr/staff/joan/file3

System Consultant

23

Directory Operations

System Consultant

24

Display Current Directorys Full Pathname


To determine the full pathname of the current working directory, use the command named pwd pwd stands for print working directory
Example: To display the full pathname of the current working directory ux% pwd /home/ux/krush/unix

System Consultant

25

The ls Command

System Consultant

26

Long List Option

System Consultant

27

List ALL Contents of Current Directory


List contents of the current ux% ls -la directory in long format total 126 drwxr-xr-x 13 krush csci 1024 Apr 26 15:49 . drwxr-xr-x 15 root root 512 Apr 24 15:18 .. -rwx------ 1 krush csci 1120 Apr 12 13:11 .cshrc -rwxr--r-- 1 krush csci 885 Dec 2 13:07 .login -rw-r--r-- 1 krush csci 141 Mar 14 13:42 .logout --rwx------ 1 krush csci 436 Apr 12 11:59 .profile drwx------ 7 krush csci 512 May 17 14:11 330 drwx------ 3 krush csci 512 Mar 19 13:31 467 drwx------ 2 krush csci 512 Mar 31 10:16 Data -rw-r--r-- 1 krush csci 80 Feb 27 12:23 crontab.cron . Points to cwd, .. Points to parents dir. File names begins with a dot (.) are hidden files Directories Regular text file
28

System Consultant

List Contents of a Specific Directory


ux% ls -l unix/grades total 10 -rwxr-xr-x 3 krush csci -rwxr-xr-x 1 krush csci -rwxr-xr-x 2 krush csci -r-x------ 1 krush csci -r-x------ 1 krush csci

Listing contents of a subdirectory named unix/grades


72 Jan 19 19:12 330assign-graderun 70 Jan 19 19:13 330exam-graderun 70 Jan 19 19:12 330quiz-graderun 468 Feb 1 11:55 test-330grade 664 Feb 1 11:55 test-330grade,v

System Consultant

29

File Name Expansion & Wildcards


Allows you to select files that satisfy a particular name pattern (wildcards)
Character * ? [list] [lower-upper] str{str1,str2,} Description Match zero or more char. Match any single character Example ls *.c ls conf.?

Match any single character in list ls conf.[co] Match any character in range Expand str with contents of { } ls lib-id[3-7].o ls c*.{700,300}

System Consultant

30

The mkdir Command

System Consultant

31

Creating a New Directory


To create a directory, use the command named mkdir
Example: To create two new directories called csci330 and test-data ux% mkdir csci330 test-data ux% mkdir /home/ux/krush/unix/demo
must already exist

System Consultant

32

Directory Names
Use the following characters:
Uppercase letters (A-Z) Lowercase letters (a-z) Numbers (0-9) Underscore ( _ ) Period/dot ( . )

System Consultant

33

Directory Names
When naming a directory, avoid the following characters: & $ * <> \ () ; | # ^ [] ? ! {} / ~

Space Tab
System Consultant
34

Example: Create a Directory Creation

dev tty null

etc skel mp

home ux

usr bin local ucb


.logout

You are here


Data

z036473 csci330

.cshrc

Temp

Create a directory called Data under csci330 a) Using Absolute Pathname: mkdir /home/mp/z036473/csci330/Data b) Using Relative Pathname: System Consultant mkdir csci330/Data
35

The cd Command

System Consultant

36

Changing Directory

dev tty null

etc skel mp

home ux

usr bin local ucb


.logout

z036473 csci330

.cshrc

Temp

You are here

Data

In the Data directory, go to $HOME directory a) Using Absolute Pathname: cd /home/mp/z036473 b) Using Relative Pathname: System Consultant cd $home cd ../.. cd cd ~ cd ~z036473
37

Remove Directories
To remove an empty directory a directory that does not contain user-created files, use the command named rmdir
Example: To remove a directory called test, which does not contain user-created files. ux% rmdir test

To remove a non-empty directory, use the command named rm r


Example: To remove a non-empty directory called olddata ux% rm r old-data
System Consultant
38

Operations Common to Directories and Regular Files

System Consultant

39

Copying Files
To copy a file, use the command named cp Syntax: cp source-file new-file Commonly used options:
-i if new-file exists, the command cp prompts for confirmation before overwriting -p preserve permissions and modification times -r recursively copy files and subdirectories

System Consultant

40

Copying Files
source-file must have read permission. The directory that contains source-file must have execute permission. The directory that contains new-file must have write and execute permissions. Note that if new-file exists, you do not need the write permission to the directory that contains it, but you must have the write permission to newfile.

System Consultant

41

Moving Files
To move files from one directory to another directory, or to re-name a file, use the command named mv. The directory that contains the source file and the destination directory must have write and execute access permissions.

System Consultant

42

Moving Files
Syntax: mv source-file destination-file If the destination file exists, mv will not overwrite exiting file.
Example: Move assign1.txt a different directory and rename it to assign1.save ux% mv assign1.txt $HOME/archive/assign1.save ux% mv assign1.txt $HOME/archive

System Consultant

43

Moving a File

System Consultant

44

Rename Directories
To change the name of an existing directory, use the command named mv
Example: To rename the file called unix to csci330 ux% mv unix csci330

For the above example, what happens if csci330 already exists in the current directory and it is the name of a directory?

System Consultant

45

The mv Command

System Consultant

46

Removing/Deleting Files
You should remove un-needed files to free up disk space. To remove/delete files, use the command named rm. Syntax: rm file-list Commonly used options:
-f force remove regardless of permissions for file-list -i prompt for confirmation before removing -r removes everything under the indicated directory

System Consultant

47

Removing/Deleting Files
If file-list contains pathname, the directory components of the pathname must have execute permission. The last directory that contains the file to be deleted must have execute and write permissions.
Example: Remove the file named old-assign ux% rm unix/assign/old-assign

System Consultant

48

Recap: Common Operations on Files

System Consultant

49

Finding Files
The command named find can be used to locate a file or a directory. Syntax: find pathname-list expression find recursively descends through pathname-list and applies expression to every file. For syntax of expression, see Course Notes pp. 139

System Consultant

50

Finding Files
Example 1: Find all files, in your directory hierarchy, that have a name ending with .bak. ux% find $home name *.bak print Example 2: Find all files, in your directory hierarchy, that were modified yesterday. ux% find $home mtime 1 -print

System Consultant

51

The ln command
Allows file to listed in multiple directories 2 types:
Hard link Symbolic link

First: understand Unix file storage and organization

System Consultant

52

Unix file organization


Computer has one or more physical hard drives Hard drive is divided into partitions Partition holds file system
File system is set of data blocks Data blocks contain
general information actual file data directory information

System Consultant

53

Blocks in a file system

System Consultant

54

inode
Index (or information) node: one inode per file Each inode has unique number contents:
File type, access permissions, link count UID, GID Date and time of the files last
Data access (read and execute) Data modification (written) I-node modification (permission change)

Data blocks assigned to the file

55

Inodes in a filesystem

System Consultant

56

inode Contents: where is the file data ?


Inode may store:
10 addresses of data blocks that belong to file 1 address of a block that contains data block addresses 1 address of a block that contains addresses of blocks that contain data block addresses 1 address of a block that contains addresses of blocks that contain addresses of blocks that contain data block addresses

System Consultant

57

I-node Structure

I-node
Access, Links, and other information 1 2 . . 9 10 11 12 13

blocks

blocks

blocks indirect block double indirect block

blocks

blocks

blocks

blocks

triple indirect block

System Consultant

58

Directory representation
Directory is a file:
Has inode like regular file, but different file type Data blocks of directory contains simple table:

Name

Inode number

System Consultant

59

Example structure
I-node list

.
2763 2764 2765

Contents of dir1

. ..
myfile

1076 2083 2764 Data blocks on disk

. .

System Consultant

60

Example: user view vs. system view

System Consultant

61

Output: ls -li
ux% ls -li crontab.cron
118282 -rw-r--r-- 1 krush csci 80 Feb 27 12:23 crontab.cron

I-node

System Consultant

62

Howto share Files ?


Duplicate shared files. Create common login name for members of the team. Set appropriate access permissions on shared files. Create common group for members of the team. Share files via links.

System Consultant

63

Duplicate Shared Files


Make copies of shared files and give them to all members of the team. The simplest way to share files. Does not work well if members of the team work on the files at the same time.

System Consultant

64

Create Common Login Name


All team members use a common login that gives them access to shared files. This is a simple solution that works well if the size of the team is small and stable. A disadvantage is team members have to use a separate account, not their regular account.

System Consultant

65

Set Appropriate Access Permissions


Team members put all shared files in one place and set access permissions so all team members can access them. This scheme works well if all team members are in the same group (group permissions can be used). A disadvantage is if the group used has other users in it, they will also have access to the files.

System Consultant

66

Create Common Group


This solution is similar to setting appropriate permissions except a new group is created that contains just the members of the team. All team members have individual logins. This is an effective solution, especially if it is with version control.

System Consultant

67

Linking Files
To share a single file with multiple users, a link can be used. A link is:
A reference to a file stored elsewhere on the system. A way to establish a connection to a file to be shared.

Two types:
Hard link Symbolic link (a.k.a. soft link)

System Consultant

68

Hard Link
Advantages
Allow access to original file name via the file name or the I-node number The original file continues to exist as long as at least one directory contains its I-node Checks for the existence of the original file

Disadvantages
Cannot link to a file in a different file system

Prevents owner from truly deleting it, and it counts against his/her disk quota

System Consultant

69

Hard Link

home z036473

Syntax: ln shared-file link-name

dir1

dir2 dir3

From dir3, link to the file aa in dir1 name it bb: % ln /home/z036473/dir1/aa bb

aa

bb

System Consultant

70

The ln Command

System Consultant

71

A Hard Link

System Consultant

72

Hard Link
Contents of dir1
home z036473

. ..
aa
dir2 dir3

1076 2083 2407

2406 2407 2408

dir1

. .

aa

Contents of dir3

bb

. ..
bb

1070 2050 2407


73

System Consultant

Symbolic Link
Advantages
Allow access to original file name Can use either relative or absolute path to access the original file Can cross partition and drives Allows the creation of a link to a directory Can be circular linked to another symbolic linked file

Disadvantages
Created without checking the existence of the shared file Cannot access the shared file if its path has restricted permissions

System Consultant

74

Symbolic Link
A hard link may not be created for a file on a different file system Use symbolic link The linked files do not share the same I-node number

Syntax: ln s shared-file link-name


Also called source-file Also called target-file

System Consultant

75

Symbolic Links to Different File Systems

System Consultant

76

Users Disk Quota


A disk quota is set for each user account The command: quota v
displays the users disk usage and limits

2 kinds of limits:
Soft limit: ex. 3MB
Maybe exceeded for one week System will nag

Hard limit: ex. 4MB


Cannot be exceeded

System Consultant

77

Operations Common to Directories and Regular Files

System Consultant

78

Operations Unique to Regular Files

Create

Edit

Display Contents

Print

Others

System Consultant

79

Creating New Files


Create Regular Files

cat

vi

pico

Redirect Command Output See the C shell Section 06

See Text Editors Section 03

System Consultant

80

Creating A File With cat

Example: mp% cat > myfile This is line 1 of input Line 2 of input ^d mp%

System Consultant

81

Editing Text Files


Editing Text Files

vi

pico

sed

awk

See Text Editors Section 03

See course notes section 08

See course notes section 10

System Consultant

82

Displaying Contents of Text Files


Display Text File contents

cat

more

less

pg

head

tail

System Consultant

83

The cat Command

System Consultant

84

Viewing Contents of Text Files


The command named cat can be used to display/concatenate one or more files, displaying the output all at once.
Example: Display the contents of a file called assign1.txt. ux% cat assign1.txt

System Consultant

85

Viewing Contents of Text Files


The commands named more and less can be used to display the contents of one or more files one page at a time.
Space bar to advance to next page b to go back a page Enter Key to advance to next line Example: Display the contents of a file called assign1.txt one page at a time. ux% more assign1.txt

System Consultant

86

Viewing Contents of Text Files


The command named head can be used to display the beginning portion of indicated file(s); the default head size is 10 lines.
Example: Display the first 20 lines of a file called assign1.txt. ux% head 20 assign1.txt

System Consultant

87

Viewing Contents of Text Files


The command named tail can be used to display the ending portion of indicated file(s); the default tail size is 10 lines.
Example: Display the last 10 lines of a file called assign1.txt. ux% tail assign1.txt ux% tail 10 assign1.txt

System Consultant

88

Other Operations Unique to Text Files


Other File Operations

Combine contents

Extract contents

Compare contents

File size

Compress contents

sort

Unique lines

Encrypt/ decrypt

System Consultant

89

Combining Contents of Files


Method 1: To vertically concatenate the contents of two or more files, use the command named cat with output redirection (>). Syntax: cat file-1 file-2 file-3 > all-file all-file will contain the combined contents of file-1, file-2, and file-3 in top-down (vertical) fashion See demo

System Consultant

90

Combining Contents of Files


Method 2: To horizontally concatenate contents (columns/fields) of two or more files, use the command named paste. Syntax: paste file-1 file-2 See demo

System Consultant

91

Extracting Contents of Files


To extract one or more fields in a file, use the command named cut.
Example: Extract the month and year fields from the output of the date command. ux% date Mon Feb 2 20:37:38 CST 2004 ux% date | cut -d' ' -f2,6 Feb CST

System Consultant

92

The cut Command

System Consultant

93

Comparing Files: comm


The command named comm can be used to compare lines that are common in two sorted files. Syntax: comm [options] file-1 file-2 The output contains three columns:
Column1 contains lines unique to file-1 Column 2 contains lines unique to file-2 Column 3 contains lines common to both files

See Demo

System Consultant

94

Comparing Files: diff


The command diff can be used to compare two files line by line. Syntax: diff [options] file-1 file-2 If file-1 and file-2 are the same, no output is produced. If file-1 and file-2 are not the same, diff reports a series of commands that can be used to convert the first file to the second file. See Demo
95

System Consultant

Determining File Size


Recall: The ls command with the option l gives the file size in bytes. Use the command named wc to display the size of files as number of lines, words, and characters. Syntax: wc file-list Commonly used options:
-l display the number of lines -w display the number of words -c display the number of characters

See demo
System Consultant
96

The wc Command

System Consultant

97

Compress File Contents


The command named compress can be used to reduce the size of one or more files. Syntax: compress file-name To display compression percentage and the names of compressed files, use the option named v. The file extension .Z is automatically appended to file-name. See Demo
System Consultant
98

Viewing a Compressed File


The command named zcat can be used to display the contents of a compressed file in a readable format.
Example: Display the contents of a compressed file called customer-data.Z. ux% zcat customer-data.Z

System Consultant

99

Un-compress File Contents


To uncompress one or more compressed files, used the command named uncompress. Syntax: uncompress file-list
Example: uncompress a compressed file called customer-data.Z. ux% uncompress customer-data.Z

System Consultant

100

Sorting Files
To sort a text file in ascending or descending order, use the command named sort. Syntax: sort [options] file-name Commonly used options:
-r sort in reverse order -n numeric sort +x [-y] specify a field as the sort key; skipping x fields and start sorting with field y -f consider lowercase and uppercase to be equivalent

System Consultant

101

The sort Command

System Consultant

102

Fields Within A Line

System Consultant

103

Field Specifier Examples

System Consultant

104

Global and Local Options

System Consultant

105

Removing Repeated Lines


The command named uniq can be used to remove repetitious lines from a sorted input file, sending unique (unrepeated) lines to standard output. Syntax: uniq sorted-file-name Commonly used options:
-c place a count of repeated lines at beginning of each output line -d display the repeated lines -u display the lines that are not repeated
System Consultant
106

The unique Command

System Consultant

107

Encrypting Files
Encryption is a process that transforms a file to an unreadable form. The transformed file is called an encrypted file. You encrypt files to prevent other users from reading their contents. The command named crypt can be used to encrypt files. Syntax: crypt key < original-file > encrypted file See Demo

System Consultant

108

Decrypting Files
The process of transforming an encrypted file to its original format is called decryption. To decrypt an encrypted file, use the command named crypt. Syntax: crypt key < encrypted-file > original-file See Demo

System Consultant

109

UNIX Text Editors

System Consultant

110

Editor Concepts
Editing creating a new file and modifying an existing text file. An editor a utility that makes the editing task possible. A text editor differs from a word processor in that it does not perform text formatting, such as bold, center, underline, etc. A line editor a utility that applies changes to a line or group of lines; two common line editors: ex and sed. A screen editor shows a whole screen of text at a time; we can move cursor or select part of text, search for text, etc.
System Consultant
111

The Vi Editor
A screen editor available on most UNIX systems. When invoked, it copies the contents of a file to a memory space know as a work buffer. All editing are applied to the contents in the work buffer. If the file does not exist, an empty buffer is created. When we exit vi, the work buffer is erased. At exit time, we can do two things:
1. Quit without saving the original contents remain unchanged. 2. Save the file the original contents are replaced by the new version in the work buffer.
System Consultant
112

Vi Buffers
Work Buffer
This is where vi performs all editing Vi makes a copy of your file in the work buffer Makes all changes to the copied version Replaces original copy with edited copy when you save (:w)

General Purpose Buffer


Stores recent deleted, or copied text Used by the undo command to restore text

Name Buffers
26 named buffers, each named by a letter of the alphabet Used to store a different block of text to recall later
System Consultant
113

The UNIX vi Text Editor

Command Mode

Esc

Input Mode

Return

Insert (i, I) Append (a, A), Open (o, O) Change (c), Replace (r, R)

Last-Line Mode
System Consultant
114

Vi Mode
In vi, to find out what mode you are in, you need to create a vi start-up file (.exrc) In your home directory or in your cwd directory, create .exrc file with contents:
set showmode

System Consultant

115

File Names
When naming a file, avoid the following characters:
& $ Space * <> Tab \ () ; | # ^ [] ? ! {} / ~

System Consultant

116

Commonly Used File Extensions


Extension .a .c .cc .csh .f .sh .bsh .ksh File Type An archive or library C program source C++ program source C shell script FORTRAN program source Bourne shell script Bash shell script Korn Shell script
System Consultant
117

Commonly Used File Extensions


Extension .o .ps .shar .tar .txt .Z File Type Object file of compiled program Postscript source Shell archive Tar archive ASCII text file Compressed file

System Consultant

118

Recovering Text After a Crash


If the system crashes while you are editing a file using vi, you may be able to recover your text The system sends you an e-mail telling you how to recover your file To recover a file after a system crash:
% vi r filename

System Consultant

119

Vi Demo

Demo
120

Access Permissions

System Consultant

121

Terminology
A user any one who has Unix account on the system. Unix recognizes a user by a number called user id. A super user:
has the maximum set of privileges in the system also know as system administrator can change the system must have a lot of experience and training

Users can be organized into groups. One or more users can belong to multiple groups.
System Consultant
122

Users

System Consultant

123

Terminology
To find out group information, use the command named: groups user-id Example: To find out what groups the user z036473 belongs to.
ux% groups z036473 student csci467a csci330c

Information about groups is stored in the Network Information Service (NIS) file named group.org_dir.

System Consultant

124

Security Levels
There are three levels of security in UNIX: system, directory and file. System security controlled by the system administrator, a super user. Directory and file controlled by the user who owned them.

System Consultant

125

Security Levels

System Consultant

126

Access Permission Code


The protection on a file is referred to as its file modes File modes are set with the chmod command UNIX supports three types of access permissions:
r w x read write execute permission denied

System Consultant

127

Directory and File Permissions

System Consultant

128

Access Types
Access Type
r (read)

Meaning on File
View file contents (open, read) Change file contents

Meaning on Dir.
List directory contents

w (write)

- Change directory contents - Be careful !!! - Make it your cwd - Access files (by name) in it Permission denied

x (execute)

Run executable file

Permission denied

System Consultant

129

Checking Permissions
To check the permissions of an existing file or an existing directory, use the command: ls l Example:
ux% ls l unix total 387
drwxr--r--rw-r--r--rw-r--r--rw-r--r-1 z036473 student 1 z036473 student 1 z036473 student 1 z036473 student 862 Feb 7 19:22 unixgrades 0 Jun 24 2003 uv.nawk 0 Jun 24 2003 wx.nawk 0 Jun 24 2003 yz.nawk

System Consultant

130

Changing Permissions

System Consultant

131

The chmod Command

System Consultant

132

Changing Permissions: Symbolic Mode

System Consultant

133

Changing Permissions: Symbolic Mode


mp% chmod who operation permissions filename
u for user g for group o for others a for all + for add - for remove = for assign r for read w for write x for execute

System Consultant

134

Changing Permissions: Symbolic Mode


ux% ls -li sort.c 118283 -rw-r--r-- 1 krush csci

80 Feb 27 12:23 sort.c

Example 1: To change the permissions on the file sort.c using Symbolic mode, so that: a) Everyone may read and execute it b) Only the owner and group may write to it. We want this using symbolic mode:

rwx|rwx|r-x

Answer:
chmod ug=rwx,o=rx sort.c chmod ugo+rx,go+w sort.c
System Consultant
135

The chmod Command: Octal Mode

System Consultant

136

Changing Permissions: Octal Mode


ux% ls -li sort.c 118283 -rw-r--r-- 1 krush csci

80 Feb 27 12:23 sort.c

Example 2: Ignoring the original permission settings, change the permissions on the file sort.c using octal mode, so that: a) Everyone may read and execute it b) Only the owner and group may write to it. We want this using octal mode:

rwx|rwx|r-x

Answer: chmod 775 sort.c


System Consultant
137

Changing Permissions: Octal Mode


ux% ls -li sort.c 118283 -rw-r--r-- 1 krush csci 80 Feb 27 12:23 sort.c

Step
1 2

Perform
List the desired setting Assign binary: 1 for access; 0 for no access List octal values for the corresponding binary 1s Convert the octal values to a 3digit number Write the command
System Consultant

Settings
rwx|rwx|r-x 111|111|101 421|421|401 775 chmod 775 sort.c
138

3 4 5

Changing Permissions: exercise


Ignoring the original permission settings on a file called myfile, assign:
a) Read, write, and execute permissions to owner b) Read and execute permissions to group c) Execute permission to others We want: rwx|r-x|--x

1) Using Symbolic Mode: chmod u=rwx,g=rx,o=x myfile __________________________________ 2) Using Octal Mode: chmod 751 myfile __________________________________
System Consultant
139

User Masks (default permissions)


The default permissions are initially set for a file or directory using a three-digit octal system variable called user mask (mask). This user mask was defined initially by the system administrator when your account is created. Initially set by system administrator in a start-up file: $HOME/.cshrc The user mask contains the octal settings for the permissions to be removed from the default when a directory or file is created. The default permissions are:
777 for a directory 666 for a file
System Consultant
140

The umask Command

System Consultant

141

User Mask
User mask Value 000 111 222 333 444 555 666 777 Directory File (666) Default: 777 Default: 666 777 (rwx rwx rwx) 666 (rw- rw- rw-) 666 (rw- rw- rw-) 555 (r-x r-x r-x) 444 (r- - r- - r- -) 666 (rw- rw- rw-) 444 (r- - r- - r- -) 444 (r- - r- - r- -)

333 (-wx wx rx) 222 (-w- -w- -w-) 222 (-w- -w- -w-) 111 (- -x - -x - -x) 000 (--- --- --- )
System Consultant

222 (-w- -w- -w-) 000 (--- --- --- ) 000 (--- --- --- )
142

The command: umask


% umask 000 For regular files: For directories: rw-rw-rwrwxrwxrwx 666 777

In $HOME/.cshrc file: umask 022 For regular files: rw-r- - r- For directories: rwxr-xr-x

644 755

System Consultant

143

Default Access Permissions


To prevent cheating, do not grant read and write access permissions to group and others! Set default permissions for all files and directories created in the future, so that only you/owner can rw to it (rw- --- ---) In your $HOME/.cshrc file: umask 077

System Consultant

144

Default Access Permissions: exercise


Assume your $HOME/.cshrc file contains the command: umask 002. a) What permissions would be set for newly created text files? b) What permissions would be set for newly created directories?

System Consultant

145

Special Permissions
The regular file permissions (rwx) are used to assign security to files and directories. Three additional special permissions can be optionally used on files and directories.
Set User Id (SUID) Set Group ID (SGID) Sticky bit

System Consultant

146

Special Permissions: SUID


SUID enables regular users to execute a program file and become the owner of the file for the duration of execution. Example: The passwd command is used to change a users password. The file /usr/bin/passwd (owned by root) has this special permission set: r-sr-sr-x In theory, only the root user has the ability to change passwords. Since /usr/bin/passwd has the SUID permission on, when a regular user executes the passwd command, the user temporarily becomes the root user while the passwd command is executing.
System Consultant
147

Special Permissions: GUID


Like SUID, SGID enables regular users to execute a program file and become a member of the group that is attached to the file. If a file is owned by the system group and also has the SGID permission, then any user who executes that file will be a member of the system group during the execution.

System Consultant

148

Special Permissions: Sticky Bit


Sticky bit performs a useful function on directories Recall: Write permission applied to a directory enables you to add and remove any files to or from that directory. If you had write permission to a certain directory but no permissions for files within it, you could delete all of those files.

System Consultant

149

Special Permissions: Sticky Bit


Scenario: A company provides a common directory that gives all employees the ability to add and remove files in it. This directory must give user write permissions. Unfortunately, the write permissions also gives all employees the ability to delete all files and subdirectories within it, including the ones that others have added to the directory.

System Consultant

150

Special Permissions: Sticky Bit


If the sticky bit were applied to this common directory, in addition to write permissions, then employees may add files to the directory. However, they may delete only files and directories that they have added/created. Example:
ls ld /tmp
drwxrwxrwt 3 root sys 1077 Jan 25 13:30 /tmp

Sticky Bit

System Consultant

151

Special Permissions
The access permission status that is displayed using the ls l command does not have a section for special permissions However, since special permissions required execute, they mask the execute permission when displayed using the ls l command. rwxrwxrwx rwsrwsrwt
SUID SGID
STICKY BIT 152

System Consultant

Special Permissions
All special permissions also required the execute permission to work properly. Usually, the SUID and SGID apply to executable files. The sticky bit applies to directories, which must have execute permission for access. If special permissions are set on a file or a directory without execute permission, the special permissions are shown in capital letters.

rw-rw-rwrwSrwSrwT
SUID
STICKY SGID System Consultant BIT 153

Setting Special Permissions

suid sgid 4 2 7 Special

stb 1

r 4

w 2 7 user

x 1

r 4

w 2 7 group

x 1

r 4

w 2 7 others

x 1

Use the chmod command with octal mode: chmod 7777 filename
System Consultant
154

Special Permissions: Exercise


Give the full UNIX command to enable all users including others to add files to directory named dir1, but only to delete files that they own in that directory. Answer: chmod 1777 dir1 ____________________________________

System Consultant

155

Regular Expressions
System Consultant

System Consultant

156

Regular Expression Metacharacters


A pattern of special characters used to match strings in a search Can be controlled by special characters called metacharacters Metacharacters are used by UNIX pattern matching editors and utilities:
Editors: ed, ex, vi Utilities: grep, egrep, sed, and awk

System Consultant

157

RE Metacharacters: use with grep


RE Metacharacter
^ $ . * \char [a-z] [^] \< \> \( \) x\{m\} x\{m,\} x\{m,n\}

Matches
A character at beginning of line A character at end of line Any one character, except new line Zero or more of preceding character Escape the meaning of char following it Any one of the enclosed characters (e.g. a-z) One character not in the set Beginning of word anchor End of word anchor Tags matched characters to be used later (max = 9) Repetition of character x, m times (x,m = integer) Repetition of character x, at least m times Repetition of character x between m and m times
System Consultant
158

RE Metacharacters: use with egrep


RE Metacharacter Matches
^ $ . * \char [a-z] [^] A character at beginning of line A character at end of line Any one character, except new line Zero or more of preceding character Escape the meaning of char following it Any one of the enclosed characters (e.g. a-z) One character not in the set One or more of the preceding characters Zero or one of the preceding characters Either a or b Groups characters
System Consultant
159

+
? a|b ()

The grep Family

System Consultant

160

The Command: grep


Syntax: grep [options] RE-pattern file(s) Searches the entire specified input file(s) for a pattern of characters. If pattern contains white space, it must be in quotes. All other words following RE-pattern are treated as files. Sends its output to stdout (default), a file, or a command pipe. grep does not change the original input file(s). grep can receive input from stdin (keyboard), a pipe, or file(s)
System Consultant
161

Commonly Used Grep Options


Commonly used grep options:
-c -i -l -n -s -v -w Print only a count of matched lines. Ignore uppercase and lowercase distinctions. List all files that contain the specified pattern. Print matched lines and line numbers. Work silently; display nothing except error messages. Useful for checking the exit status. Print lines that do not match the pattern. Search for the expression as a word as if surrounded by \< \>

System Consultant

162

Regular Expression

An atom specifies what text is to be matched and where it is to be found. An operator combines regular expression atoms.

System Consultant

163

Atoms
An atom specifies what text is to be matched and where it is to be found.

System Consultant

164

Single-Character Atom
A single character matches itself.

System Consultant

165

Dot Atom
A dot matches any single character except for a new line character (\n)

System Consultant

166

Class Atom
A class matches only single character that can be any of the characters defined in a set, e.g. [A-C] matches either A, B, or C.

Notes: 1) A range of characters is indicated by a dash, e.g. [A-C] 2) Can specify characters to be excluded from the set, e.g. [^0-9] matches any character other than a number.
System Consultant
167

Example: Classes

System Consultant

168

Anchors
Anchors tell where the next character in the pattern must be located in the text data.

System Consultant

169

Back References: \n
Used to retrieve saved text in one of nine buffers Can refer to the text in a saved buffer by using a back reference; e.g. \1 \2 \3 ...\9. More on back references later in this section.

System Consultant

170

Operators

no oper.

System Consultant

171

Sequence Operator
In a sequence operator, if a series of atoms are shown in a regular expression, there is no operator between them.

System Consultant

172

Alternation Operator: |
The alternation operator ( | ) is used to defined one or more alternatives, e.g. A | B matches A or B.

System Consultant

173

Repetition Operator: \{\}


The repetition operator specifies that the atom or expression immediately before the repetition may be repeated.

System Consultant

174

Basic Repetition Forms

System Consultant

175

Short Form Repetition Operators: * + ?

System Consultant

176

Group Operator
In the group operator, when a group of characters is enclosed in parentheses, the next operator applies to the whole group, not only the previous characters.

System Consultant

177

Save Operator: \(..\)


Copies a matched text string to one of nine buffers for later reference. Back references can then be used to retrieve saved text in one of nine buffers. Can refer to the text in a saved buffer by using a back reference; e.g. \1 \2 \3 ...\9. Example: To reverse first name and last name in the following data set; also.
John DeVries Bob Flowers

s/^\([A-Z].*\) \([A-Z].*\)$/\2 \1/


DeVries John Flowers Bob
System Consultant
178

More Examples
The following slides contain examples of using regular expressions with grep, egrep and fgrep. Review them on your own time. Ask your instructor or T.A. if you have any questions.

System Consultant

179

Example: Grep with RE: ^


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00

Print all lines beginning with the letter n.

% grep '^n' grep-datafile northwest NW Charles Main northeast NE AM Main Jr. north NO Ann Stephens

300000.00 57800.10 455000.50

System Consultant

180

Example: Grep with RE: $


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00

Print all lines ending with a period and exactly two zero numbers.

% grep '\.00$' grep-datafile northwest NW Charles Main southeast SE Patricia Hemenway Extra [A-Z]****[0-9]..$5.00
System Consultant

300000.00 400000.00

181

Example: Grep with RE: \char


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00

Print all lines containing the number 5, followed by a literal period and any single character. The dot metacharacter represents a single character, unless it is escaped with a backslash. When escaped, the period is no longer a special character, but represents itself, a literal period.

% grep '5\..' grep-datafile Extra [A-Z]****[0-9]..$5.00

System Consultant

182

Example: Grep with RE: [ ]


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00 Print all lines beginning with either a w or an e.

% grep '^[we]' grep-datafile western WE Sharon Gray eastern EA TB Savage

53000.89 440500.45

System Consultant

183

Example: Grep with RE: [^]


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00 % grep '\.[^0][^0]$' grep-datafile western WE Sharon Gray southwest SW Lewis Dalsass eastern EA TB Savage

Print all lines ending with a period and exactly two non-zero numbers.

53000.89 290000.73 440500.45

System Consultant

184

Example: Grep with RE: x\{m\}


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00 Print all lines where there are at least six consecutive numbers followed by a period. % grep '[0-9]\{6\}\.' grep-datafile northwest NW Charles Main 300000.00 southwest SW Lewis Dalsass 290000.73 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 north NO Ann Stephens 455000.50 central CT KRush 575500.70
System Consultant
185

Example: Grep with RE: \<


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00 Print all lines containing a word starting with north.

% grep '\<north' grep-datafile northwest NW Charles Main northeast NE AM Main Jr. north NO Ann Stephens

300000.00 57800.10 455000.50


186

System Consultant

Example: Grep with RE: \< \>


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00 Print the line if it contains the word north.

% grep '\<north\>' grep-datafile north NO Ann Stephens

455000.50

System Consultant

187

Example: grep with pipe


% ls -l | grep '^d' drwxr-xr-x 2 krush drwxr-xr-x 2 krush drwxr-xr-x 2 krush drwxr-xr-x 2 krush drwxr-xr-x 2 krush drwxr-xr-x 2 krush drwxr-xr-x 2 krush drwxr-xr-x 2 krush drwxr-xr-x 4 krush drwxr-xr-x 2 krush % ls -l | grep -c '^d' 12

Pipe the output of the ls l command to grep and list/select only directory entries.

csci csci csci csci csci csci csci csci csci csci

512 Feb 8 22:12 assignments 512 Feb 5 07:43 feb3 512 Feb 5 14:48 feb5 512 Dec 18 14:29 grades 512 Jan 18 13:41 jan13 512 Jan 18 13:17 jan15 512 Jan 18 13:43 jan20 512 Jan 24 19:37 jan22 512 Jan 30 17:00 jan27 512 Jan 29 15:03 jan29

Display the number of lines where the pattern was found. This does not mean the number of occurrences of the pattern.

System Consultant

188

Example: egrep with RE: a|b


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00 Prints the line if it contains either the expression NW or the expression EA.

% egrep 'NW|EA' grep-datafile northwest NW Charles Main eastern EA TB Savage

300000.00 440500.45

System Consultant

189

Example: egrep with RE: +


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00 Print all lines containing one or more 3's.

% egrep '3+' grep-datafile northwest NW Charles Main western WE Sharon Gray southwest SW Lewis Dalsass

300000.00 53000.89 290000.73

System Consultant

190

Example: egrep with RE: ?


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00

Print all lines containing a 2, followed by zero or one period, followed by a number.

% egrep '2\.?[0-9]' grep-datafile southwest SW Lewis Dalsass

290000.73

System Consultant

191

Example: egrep with RE: ( )


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00

Print all lines containing one or more consecutive occurrences of the pattern no.

% egrep '(no)+' grep-datafile northwest NW Charles Main northeast NE AM Main Jr. north NO Ann Stephens

300000.00 57800.10 455000.50

System Consultant

192

Example: egrep with RE: (a|b)


% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00

Print all lines containing the uppercase letter S, followed by either h or u.

% egrep 'S(h|u)' grep-datafile western WE Sharon Gray southern SO Suan Chin

53000.89 54500.10

System Consultant

193

Example: fgrep
% cat grep-datafile northwest NW Charles Main 300000.00 western WE Sharon Gray 53000.89 southwest SW Lewis Dalsass 290000.73 southern SO Suan Chin 54500.10 southeast SE Patricia Hemenway 400000.00 eastern EA TB Savage 440500.45 northeast NE AM Main Jr. 57800.10 north NO Ann Stephens 455000.50 central CT KRush 575500.70 Extra [A-Z]****[0-9]..$5.00

Find all lines in the file containing the literal string [A-Z]****[0-9]..$5.00. All characters are treated as themselves. There are no special characters.

% fgrep '[A-Z]****[0-9]..$5.00' grep-datafile Extra [A-Z]****[0-9]..$5.00

System Consultant

194

The C Shell

System Consultant

195

Standard UNIX Shell Overview

System Consultant

196

Objectives
C Shell Environment
Startup and logout files Shell variables History Command substitution Command sequences Aliases Directory Stacks Redirections and pipe

C Shell Programming

System Consultant

197

Password File
Login shell

ux% nismatch z036473 passwd.org_dir


z036473:e6qhWRw.YeK0E:1432:202:rush kaisone:/home/mp/z036473:/bin/csh:12068::
gid User login id Encrypted password User name

Home directory

uid

Number of days between 1/1/1970 and the last day that the user changed their password.

System Consultant

198

C Shell Startup

init
Login:

getty login csh

Login: z036473 Passwd:

Login: z036473 Passwd: %

199

NIS Password File


Network Information Service password file named passwd.org_dir contains user account information. The command named niscat displays the entire contents of the passwd.org_dir file. The command named nismatch displays account information for a specified user.
Examples: Display contents of the password file. ux% niscat passwd.org_dir
System Consultant
200

Login At NIU
Default shell: /bin/csh
Can be customized via startup scripts

1) /etc/.login (system shell) 2) $HOME/.cshrc (if C shell is the login shell) 3) $HOME/.login

System Consultant

201

Startup File: $HOME/.cshrc


Executed once:
at login and each time a C shell script is executed (unless #!/bin/csh f was used)

Located in your $HOME directory Contains settings such as:


Command search path Initial permission setting (umask) Terminal setting Mail box, etc.
System Consultant
202

Useful .cshrc content


Define command aliases
alias ll ls al alias ap man k

customize common behavior:


set filec when typing a file/directory name, if you press the esc key, it will complete the rest of the name for you. set ignoreeof prevents accidental logout if you type ctrl-d at the command line. set noclobber prevents overwritten of files by output direction.
System Consultant
203

Startup File: $HOME/.login


Executed once only during login process Located in your $HOME directory Contains settings such as:
Terminal setting: special keyboard character setting stty erase ^h

Mail directory

System Consultant

204

Logout File: $HOME/.logout


Executed once when you log out Located in your $HOME directory useful content:
ps f u $user displays the users active processes

System Consultant

205

C Shell Prompts
C shell has two prompts:
Primary prompt: % Secondary prompt: ?

Primary prompt (%)


Can be reset Use the command named set prompt to customize your command prompt. Example: ux% set prompt = $USER > z036473 >
New prompt

System Consultant

206

C Shell Prompts
Secondary prompt
Appears when writing on-line script at the command prompt
% foreach student (z036473 z036474) ? mail $student < memo ? ^d (type ctrl-d or type end) %

Once the command is entered, and [Return] is pressed, you cannot go back to previous lines. C shell history does not save commands typed at the secondary prompt.
System Consultant
207

Variables
A shell variable is a location in memory where values can be stored
Variables

Pre-defined

User-defined

Special

Shell

Environment

String

Arithmetic

Built-in

Pathname

System Consultant

208

Pre-defined Variables
Two types:
Pre-defined shell variables Pre-defined environment variables

Pre-defined shell variables are used to configure the shell.


Example: history which contains the size of the history list.

Pre-defined environmental variables are used to configure the shell environment.


Example: HOME which contains the full pathname of your home directory.

System Consultant

209

Some Predefined Shell Variables


Name
cwd history ignoreeof noclobber

Contents
The current working directory The size of the history list Prevents the shell from terminating when pressing Control-D. Use the logout or exit command. Prevents existing files from being overridden by output redirection (>), and non-existent files from being appended by append (>>) The shell primary prompt The number of commands to save in the history file $HOME/.history The exit code of the last command
System Consultant
210

prompt savehist status

Predefined Environment Variables


Name
HOME PATH MAIL USER SHELL TERM

Meaning
The full pathname of your home directory A List of directories to search for commands The full pathname of your mailbox Your user id The full pathname of your login shell The type of your terminal

System Consultant

211

Command Search Path


setenv PATH /usr/local/bin:/usr/bin:/usr/ucb:$home/bin:

dev tty null

etc skel

home mp
z036473

usr

ux

bin

local bin

ucb

bin

csci330System Consultant .cshrc

.login

.logout

212

Creating Environment Variables


Syntax: setenv varname value Create C shell environment variable and initialize it to a value If varname does not exist, it is created; otherwise, it is overwritten An Environment variable always holds exactly one value Example:
% setenv TERM vt100 % echo $TERM vt100 % setenv EDITOR pico
System Consultant
213

User-Defined Variables
Created by the user
Syntax: set varname=string

Create a shell variable and initialize it to a string of characters varname:


1-20 characters long Letters, digits, and underscore First character cannot be a digit Should not be the same as one of the pre-defined variables

System Consultant

214

Storing Values in Variables


Command set x = 123 set x = Hello set name = Jane Black set name = Jane Black set x = Go Dons! Result x contains 123 x contains Hello x contains Jane x contains Jane Black x contains Go Dons! Comment x contains a number, but it is stored as string of digits Storing a character string Only Jane is stored When a string contains spaces it must be quoted When a string contains spaces and special characters, it must be quoted

System Consultant

215

Accessing the Values of a Variable

The name of the variable must be preceded by a dollar sign Variable values can be used anywhere in a string Examples:
% set count = 7 % echo $count is greater than 6 and less than 8 7 is greater than 6 and less than 8 % echo The value of count is $count as expected The value of count is 7 as expected % echo My lucky number is $count My lucky number is 7
System Consultant
216

Accessing the Values of a Variable


Variable values can also be stored in another variable Example:
% set count = 5 % set number = $count % echo count contains: $count count contains: 5 % echo number contains: $number number contains: 5

System Consultant

217

Unsetting a Variables
We can clear a variable by assigning a null value to it - examples: set z = ; set y = Another method is to use the unset command Example:
% set x = 1 % echo "(x contains:" $x")" (x contains: 1) % unset x % echo "(x contains:" $x")" x: Undefined variable

System Consultant

218

Command Substitution
A command surrounded by backticks (` `) is replaced by its standard output Any newline in the output are replaced by spaces

Demo
System Consultant
219

Command Sequence (;)


Allows you to enter a series of commands all at once Commands are separated by a semicolon (;) Useful for typing an entire sequence of commands at once
Example: To execute these commands in sequence: date, pwd, and ls % date;pwd;ls Mon Jul 2 11:07:10 CDT 2003 /home/ux/krush/330/Summer-03/Quizzes message.rickert quiz1-sum03 quiz2.sum03

System Consultant

220

C Shell History
C shell keeps a record of previously entered cmds so that they can later be:
Re-executed Edited

Entered cmds are stored in the current C shells history buffer ($HOME/.history) Commands are saved
Per session Per user

System Consultant

221

C Shell History
Each previously run command gets a sequential event number To view the history buffer:
Syntax: history [-rh] [count]
If no options are supplied, list all

Useful options:
-r -h displays history list in reverse order inhibits the display of event numbers

System Consultant

222

C Shell History
You can re-execute history events:
1) By the event number % !5 1) By the number relative to current event % !-3 1) By the text it contains % !ls

System Consultant

223

C Shell History
How big a list ? 1) On the command line, type:
% set history = 20 (per session only)

2) Include in your $HOME/.logout file:


set savehist = 30 At logout, C shell saves the last 30 cmds you executed in the $HOME/.history file

System Consultant

224

C Shell History
Two ways to display event number as part of your cmd prompt: 1) On the command line, type:
% set prompt = \! % 1 % echo csci 330 csci 330 2%_ (per session only)

2) Include in your $HOME/.cshrc file:


set prompt = \! % (per each login)

System Consultant

225

C Shell History
Command
!! !N !str !?str? ^str1^str2

Action
Re-execute the last command Invoke event N (integer) Invoke most recent event beginning with str (string) Invoke most recent event containing str Substitute str2 for str1 in the previous cmd

For more listing of history commands, see Course Notes pp. 13-7
System Consultant
226

C Shell History: Words in Event


Can use any word from any event on the history list C shell numbers the words in each command starting at 0 Example:
grep ^[0-9]\{5\} | sort t: | cut d: -f1 Word: 0 1 2 3 4 5 6 7 8

Can easily identify words since space separate them


System Consultant
227

C Shell History: Words in Event


Can refer to individual words with :n ( n is the word number) Example:
100 % ls l assign1 assign2 assign3 101 % chmod a-w !ls:2-$ $ refers to the last chmod a-w assign1 assign2 assign3 word 102 % ls l !ls:$ ls -l assign3 -r-------- 1 krush csci 0 Feb 14 12:35 assign3

The modifier * refers to words 1 through last (I.e. everything except the command name)
103 % rm !100:2* rm assign1 assign2 assign3
System Consultant
228

C Shell History

Demo

System Consultant

229

Command Substitution
A command surrounded by backticks (` `) is replaced by its standard output Any newline in the output are replaced by spaces

Demo
System Consultant
230

C Shell Aliases
Important feature of C shell Allows you to assign a name, or alias to command(s) Can use alias like any other command Can rename existing commands Can reference shell variables Aliases defined at the parent shells command line are not inherited by subshells

System Consultant

231

C Shell Aliases
C shell user-defined abbreviation for a command Useful if a command syntax is difficult to remember Define aliases at the command line or in $HOME/.cshrc file Alternative: create a file name .myalias in your $HOME and include all alias definitions.
Include this line in your $HOME/.cshrc file:
source $HOME/.myalias

System Consultant

232

Creating Aliases
Use the command: alias
Syntax: alias name command-list

User-defined nickname for the command

The command(s) that will be executed when the alias is invoked Multiple commands are separated by a semicolon Metacharacters are surrounded by single quotes
System Consultant
233

Passing Arguments into Aliases


C shell assumes all arguments come at the end of the alias definition, unless you specified otherwise Two common argument destinations:
!* words 1 through the last word of the current command/event !^ word 1 of the current command/event

Recall:
grep ^[0-9]\{5\} demand | sort t: | cut d: -f1 Word: 0 1 2 3 4 5 6 7 8 9
234

System Consultant

Example: Creating an Alias


% alias cd cd \!*; set prompt = $cwd >
Note: \ prevents the history mechanism from evaluating the !* !* represents the argument from the most recent current command in the history list (i.e. words/fields 1 through the last word of the recent command) \!* allows you to pass any number of arguments to the alias

System Consultant

235

Issue a Command to the C Shell


What happens when you issue a command to the C shell? After history substitution, examine each word that could be a command in the following sequential order:
1. Alias Substitution 2. Built-in command (commands inside the C shell; e.g. echo) 3. Commands in your search path ($PATH)

System Consultant

236

In-Class Exercise: aliases


1. Write an alias named h to list the history of previously executed commands.
alias h history

2. Write an alias named c to clear the contents of the screen.


alias c clear

3. Write an alias named rm to prompt the user for a delete confirmation message before removing file(s).
alias rm rm i \!*
237

System Consultant

In-Class Exercise: aliases


4. Write an alias named cx to give the owner the execute permission on more or more file/directory.
alias cx chmod u+x \!*

5. Write an alias named loc to locate/find any file in your directory hierarchy.
alias loc find $home name \!:1 print

6. Write an alias named ldir to display long listing of only directory names (i.e. not ordinary files).
alias ldir ls l \!* | grep ^d alias fdir find \!:1 typeSystem Consultant d print
238

Output
The output statement of the C shell is the echo command Syntax: echo [option] arg1 arg2 argN Its arguments can be strings or variables Example:
% set time = 2:00pm. % echo "It is now $time." It is now 2:00pm. % echo It is now $time. It is now 2:00pm. % echo 'It is now $time.' It is now $time.

The variable cannot be embedded in single quotes.

System Consultant

239

Output Redirection (>)


Syntax: command > file Sends output of command to file, instead of to terminal Calls the disk usage command for the Examples: current directory and redirects the
% du > status
output to a file called status

% (date; du) > status

( ) indicates command groups. Use it to combine the output of multiple commands. In this example, we place time and date in front of the disk usage
System Consultant
240

Examples: Output Redirection (>)


% date; du > usage-status
Output of date goes to? __________________ Output of du goes to?___________________

% date > usage-status; du > usage-status


The file usage-status contains ? ___________________________________

System Consultant

241

Input Redirection (<)


Syntax: Command < file Command will read (take input) from file, instead of from terminal Example:
tr [A-Z] [a-z] < report.input

System Consultant

242

Examples: Output / Input


Redirecting input and output: % tr [A-Z] [a-z] < report.input > report.output Output of one command becomes input of next: % ls > temp.txt; wc < temp.txt Eliminate the middleman: pipe % ls | wc
System Consultant
243

Appending Output
Syntax: command >> file Places the stdout of command (or command group) at the end of file If file does not exist, C shell creates it given that the variable noclobber is not set Examples:
% date >> usage-status % ls l >> usage-status % du s >> usage-status % date > usage-status
Build the file usage-status from the output of the date, ls, and du commands

System Consultant

244

Using set noclobber


% who > current-users % date >> usage-status % set noclobber
Does not allow output redirection if the file already exist Does not allow appending if the file does not already exist

% who > current-users current-users: File exists Specified incorrect % date >> usage-statis filename! statis: No such file or directory

System Consultant

245

Override the Extra Protection


Add ! To the redirection ! Means do what I mean! Example:
% set noclobber % who >! current-users
Allows redirection even if current-users exists

% date >>! usage-statis


Creates the file named usage-statis even if it does not already exist

System Consultant

246

Handling Standard Errors


Sometimes a command has special output to inform you of problems; for example:
% gcc gets.c > compile.out gets.c:8: syntax error

Syntax: command >& file


Redirect stdout and stderr to file

Syntax: command >>& file


Append stdout and stderr to file

System Consultant

247

Examples: Redirect/Append Stderr


% gcc gets.c >& compile.out % cat compile.out gets.c:8: syntax error % (date; gcc gets.c o gets) >>& compile-log
Add to the file compile-log a running record with the date and time of the compilation of the file gets.c

System Consultant

248

Separating Stdout and Stderr


% cat calendar logfile >& save
Saves both stdout and stderr in file save

% cat save July 11 Planning Meeting July 18Budget Meeting Cat: cant open logfile

stdout stderr

% (cat calendar logfile > save) >& errfile


Redirects stdout to save Redirects stderr to errfile Order is important, > goes inside ( ), and >& goes outside

System Consultant

249

Sending Stdout and Stderr Through a Pipe


Syntax: command1 |& command2
Sends stdout and stderr of command1 to be stdin to command2

Example:
% gcc gets.c |& more Sends stdout and stderr to more for convenient viewing % gcc gets.c |& lpr Plpcsl Produces a hardcopy of the messages

System Consultant

250

Summary: Redirections and Pipe


Command Syntax
command < file command > file command >& file command >> file command1 | command2 command1 |& command2

Meaning
Redirect input from file to command Redirect output from command to file Redirect output and errors to file Redirect output of command and appends it to file Take/pipe output of command1 as input to command2 Take/pipe output and errors of command1 as input to command2
System Consultant
251

You might also like