Top Banner
CIS 218 Advanced UNIX 1 Advanced UNIX Objectives to supplement the “Introduction to UNIX” slides with extra information about the Shell CIS 218 Advanced UNIX 2. The Shell (Ch.5, Sobell)
28

240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

Aug 15, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 1

Advanced UNIX

Objectives

– to supplement the “Introduction to UNIX”

slides with extra information about the Shell

CIS 218 Advanced UNIX

2. The Shell

(Ch.5, Sobell)

Page 2: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 2

Overview

1. Redirection

2. Pipes

3. Background Jobs

4. Filename Generation

Page 3: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 3

command

standard input

standard

output

1. Redirection

Command I/O is stream-based:

Page 4: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 4

$ cat

This is a line of text. This is a line of text.

Cat keeps copying lines of text

Cat keeps copying lines of text

until you press control-D at the

until you press control-D at the

beginning of a line.

beginning of a line.

$

control-D

You type a line;

it is echoed

Page 5: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 5

command

standard input

standard

output file

Redirect Output

Use > to redirect standard output to a ‘file’:

Page 6: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 6

$ cat > sample.txt

This text is being entered at the

keyboard.

Cat is copying it to a file.

Press control-D to indicate the

end of file.

$

$ cat file1.c file2.c file3.c > all-files.c

control-D

Page 7: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 7

command

standard

input

standard

output

Redirect Input

Use < to redirect standard input from a

‘file’:

file

Page 8: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 8

$ cat < supply_orders

2000 sheets letterhead ordered: 10/7/97

1 box masking tape ordered: 10/8/97

$

$ cat supply_orders

$ mail [email protected] < letter.txt

Page 9: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 9

Dangers

Bad: $ cat orange pear > orange

cat: input orange is output

– see noclobber in C Shell

Good: $ cat orange pear > temp

$ mv temp orange

Page 10: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 10

Appending Output to a File

Use >> to append:

$ date > whoson

$ cat whoson

Fri May 29 09:24:19 GMT 2000

$ who >> whoson

$ cat whoson

Fri May 29 09:24:19 GMT 2000

jenny tty02 May 29 07:21

ad tty06 May 28 11:01

$

Page 11: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 11

command1

standard

input

standard

output

command2

2. Pipes

Use a pipe to connect standard output of

one command to standard input of another:

Page 12: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 12

Use the ‘|’ operator between commands:

$ command 1 | command2

Same as:

$ command1 > temp

$ command2 < temp

$ rm temp

Page 13: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 13

$ ls | more

$ who | grep ‘ad’

ad tty06 May 23 10:31

$ who | sort

ad tty06 May 23 10:31

jenny tty02 May 21 15:29

scott tty03 May 23 09:02

Same as: $ who > temp

$ sort < temp or $ sort temp

Page 14: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 14

Filters

A filter is a command that modifies its

standard input, putting the changes onto its

standard output:

$ who | sort | lpr

$ ps | grep ad

Page 15: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 15

The tee Command

Passes its input through to standard output

unchanged. Also saves input into a file:

command1

standard

input

standard

output

command2 tee

file

Page 16: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 16

$ who | tee who.out | grep ad

ad tty06 May 23 10:31

$ cat who.out

jenny tty02 May 21 15:29

ad tty06 May 23 10:31

scott tty03 May 23 09:02

Page 17: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 17

3. Background Jobs

A normal command executes in the

foreground: you wait until it finishes

before another command can be typed.

Commands (jobs) can execute in the

background. No need to wait for them

before another command is typed.

Page 18: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 18

Background jobs end with a ‘&’:

$ gcc big-program.c &

1466

$ ls -l | lpr &

1467

$ vi report.txt

Page 19: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 19

Killing a Background Job Cannot type control-C

Use kill and the process ID (PID): $ kill 1466

Use ps to list PIDs:

$ ps

PID TT STAT TIME COMMAND

1466 03 S 0:05 gcc big-program.c

1467 03 S 0:04 ls -l | lpr

1524 03 R 0:03 ps

$

Page 20: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 20

4. Filename Generation

Commands involving filenames (e.g. cat,

ls) can include special characters in the

filenames.

– called metacharacters

– three kinds:

?

*

[...]

Page 21: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 21

The ? Special Character

? matches any single character

$ ls

mem memo12 memo9 memoalex newmemo5

memo memo5 memoa memos

$ ls memo?

memo9 memo5 memoa memos

$ lpr memo?

continued

Page 22: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 22

$ ls

7may4report may14report may4report.79

mayqreport may.report may4report

may_report mayreport

$ ls may?report

mayqreport may.report may4report

may_report

Page 23: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 23

The * Special Character

* matches any sequence of characters

(0 or more characters)

$ ls

amemo memo memoa memosally

user.memo mem memo.0612

memorandum sallymemo

$ ls memo*

memo memoa memosally memo.0612

memorandum continued

Page 24: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 24

$ ls *.txt

$ lpr *.txt

$ ls *.c

$ cat *.c > all-files

$ more all-files

$ rm *.c

$ mv all-files all-files.c

Page 25: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 25

The [...] Special Characters

Match against any single character given

inside [...]

Can include ‘-’ to give a range

$ ls

part1.txt part2.txt part3.txt part4.txt

part5.txt

$ lpr part[135].txt

$ cat part[1-3].txt

continued

Page 26: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 26

Useful Ranges

[a-z] any letter between a and z

[A-Z] any letter between A and Z

[0-9] any digit betwwn 0 and 9

Can combine:

[a-z,0-9]

continued

Page 27: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 27

$ ls

part0 part1 part2 part3 part4 ...

part32 part33 part34 part35

$ ls part[0-9]

$ ls part[12][0-9]

$ ls part3[0-5]

Page 28: 240-491 Special Topics in Computer Engineering 1 Semester ...1467 03 S 0:04 ls -l | lpr 1524 03 R 0:03 ps $ CIS 218 Advanced UNIX 20 4. Filename Generation Commands involving filenames

CIS 218 Advanced UNIX 28

Combining Special Characters

$ ls [a-m]*

$ ls *[x-z]

$ lpr p*[0-9].c &