Documente Academic
Documente Profesional
Documente Cultură
System Consultant
System Consultant
Introduction to UNIX
System Consultant
System Consultant
System Resources
System Resources Hardware Components
Software
System Software
Application Software
System Consultant
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
System Consultant
System Consultant
System Consultant
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
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
System Consultant
12
System Consultant
13
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
System Consultant
16
System Consultant
17
A Directory Hierarchy
System Consultant
18
etc skel
home mp
z036473
Home Directory
z036473 subdirectory
csci330
.cshrc
System Consultant
.logout
Directory Types
Root Directory: /
The first directory in any UNIX file structure Always begin with the forward slash (/)
Parent Directory: ..
The directory immediately above your current working directory.
System Consultant
20
File Types
- Text - Binary
System Consultant
System Consultant
23
Directory Operations
System Consultant
24
System Consultant
25
The ls Command
System Consultant
26
System Consultant
27
System Consultant
System Consultant
29
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
System Consultant
31
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
etc skel mp
home ux
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
etc skel mp
home ux
z036473 csci330
.cshrc
Temp
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
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
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
System Consultant
52
System Consultant
53
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)
55
Inodes in a filesystem
System Consultant
56
System Consultant
57
I-node Structure
I-node
Access, Links, and other information 1 2 . . 9 10 11 12 13
blocks
blocks
blocks
blocks
blocks
blocks
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
. .
System Consultant
60
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
System Consultant
63
System Consultant
64
System Consultant
65
System Consultant
66
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
dir1
dir2 dir3
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
dir1
. .
aa
Contents of dir3
bb
. ..
bb
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
System Consultant
75
System Consultant
76
2 kinds of limits:
Soft limit: ex. 3MB
Maybe exceeded for one week System will nag
System Consultant
77
System Consultant
78
Create
Edit
Display Contents
Others
System Consultant
79
cat
vi
pico
System Consultant
80
Example: mp% cat > myfile This is line 1 of input Line 2 of input ^d mp%
System Consultant
81
vi
pico
sed
awk
System Consultant
82
cat
more
less
pg
head
tail
System Consultant
83
System Consultant
84
System Consultant
85
System Consultant
86
System Consultant
87
System Consultant
88
Combine contents
Extract contents
Compare contents
File size
Compress contents
sort
Unique lines
Encrypt/ decrypt
System Consultant
89
System Consultant
90
System Consultant
91
System Consultant
92
System Consultant
93
See Demo
System Consultant
94
System Consultant
See demo
System Consultant
96
The wc Command
System Consultant
97
System Consultant
99
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
System Consultant
102
System Consultant
103
System Consultant
104
System Consultant
105
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
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)
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
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
System Consultant
118
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
System Consultant
127
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)
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
System Consultant
132
System Consultant
133
System Consultant
134
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
System Consultant
136
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
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
1) Using Symbolic Mode: chmod u=rwx,g=rx,o=x myfile __________________________________ 2) Using Octal Mode: chmod 751 myfile __________________________________
System Consultant
139
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
In $HOME/.cshrc file: umask 022 For regular files: rw-r- - r- For directories: rwxr-xr-x
644 755
System Consultant
143
System Consultant
144
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
System Consultant
148
System Consultant
149
System Consultant
150
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
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
System Consultant
155
Regular Expressions
System Consultant
System Consultant
156
System Consultant
157
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
+
? a|b ()
System Consultant
160
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
System Consultant
174
System Consultant
175
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
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
% grep '^n' grep-datafile northwest NW Charles Main northeast NE AM Main Jr. north NO Ann Stephens
System Consultant
180
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
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.
System Consultant
182
53000.89 440500.45
System Consultant
183
Print all lines ending with a period and exactly two non-zero numbers.
System Consultant
184
% grep '\<north' grep-datafile northwest NW Charles Main northeast NE AM Main Jr. north NO Ann Stephens
System Consultant
455000.50
System Consultant
187
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
300000.00 440500.45
System Consultant
189
% egrep '3+' grep-datafile northwest NW Charles Main western WE Sharon Gray southwest SW Lewis Dalsass
System Consultant
190
Print all lines containing a 2, followed by zero or one period, followed by a number.
290000.73
System Consultant
191
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
System Consultant
192
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.
System Consultant
194
The C Shell
System Consultant
195
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
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:
199
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
Mail directory
System Consultant
204
System Consultant
205
C Shell Prompts
C shell has two prompts:
Primary prompt: % Secondary 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
System Consultant
209
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
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
etc skel
home mp
z036473
usr
ux
bin
local bin
ucb
bin
.login
.logout
212
User-Defined Variables
Created by the user
Syntax: set varname=string
System Consultant
214
System Consultant
215
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
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
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)
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)
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
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
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
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
System Consultant
235
System Consultant
236
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
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.
System Consultant
239
( ) 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
System Consultant
241
System Consultant
242
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
% who > current-users current-users: File exists Specified incorrect % date >> usage-statis filename! statis: No such file or directory
System Consultant
245
System Consultant
246
System Consultant
247
System Consultant
248
% cat save July 11 Planning Meeting July 18Budget Meeting Cat: cant open logfile
stdout stderr
System Consultant
249
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
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