1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities...

Post on 21-Dec-2015

225 views 1 download

Transcript of 1 Table of Contents n Introduction to Unix. Introduction to Unix. n Basic Utilities Basic Utilities...

1

Table of Contents

Introduction to Unix. Basic Utilities Sending and Receiving Electronic Mail. Getting Help Creating and Editing Files. Advanced Utilities. Directory Structure. Files and Access Permissions grep Processes sed and awk Shell Variables and Commands.

2

Introduction

What is an operating system– It is a control program for a computer; it allocates

resources and schedules tasks.

3

Overview

UNIX is a time-sharing system. Multi-user (vs. Single user) Multi-tasking (each user may run more than one

job). Interactive (vs. batch) Command-line prompt, or Graphical user interface through X-Windows

4

Overview 2

Utilities Command interpreter is called a SHELL.

– Can also be used as a programming language -- Shell Scripts.

File structure Complaints: unfriendly, terse, treacherous,

unforgiving, and inconsistent. – TRUE? yes. It is designed for advanced programmers,

5

Using UNIX

Need: Login Name and Password. The following is a set of special keys and

commands to use:– BACKSPACE deletes one character.– CONTROL--U deletes an entire line.– CONTROL--W deletes a word.– CONTROL--R redraws a line.– CONTROL--C aborts program execution.– CONTROL--D exits the shell (logs you out).

6

Basic Utilities

Simple Utilities– ls: The "list" command – displays all files in your

current directory Option "-l" – long listing; shows all information. Option "-a“ – “all files”; show hidden files as well

– mv <filename1> <filename2>: The "move" command – move one file to another (effectively “rename”).

– cp: <filename1> <filename2>: The "copy" command – copy one file to another.

– Cat <filename>: The "conCATenate" command – display the file on the screen.

7

Receiving Electronic Mail

$ Mail– ? save [msglist] file– ? ? – ? type [msglist] – ? next – ? edit [msglist] – ? from [msglist] – ? delete [msglist] – ? undelete [msglist] – ? save [msglist] file – ? reply [message] – ? Reply [msglist]

– ? preserve [msglist] – ? mail user – ? quit – ? xit – ? header – ? ! – ? cd [directory] – ? list – ? top [msglist] – ? z [-]

8

Sending Mail

Interactively:– $ Mail mjm– <text of letter goes here; CR's OK>– <CTRL>--D to end message

Note is in a file:– $ Mail mjm < lab1.txt

– DO NOT USE ">" INSTEAD OF "<"

9

Pine

“pine” is a screen-base mail handler– Commands are entered directly, or as <CTRL>

sequences.– Handles attachments:

Text files Binary files “uuencode”d files

– To read your mail: $ pine

– To send mail, optionally give a username: $ pine marty@toolman.wiu.edu

10

Pine’s main menu (top)

After executing pine, you see the following menu:

? HELP - Get help using Pine C COMPOSE MESSAGE - Compose and send a message I MESSAGE INDEX - View messages in current folder L FOLDER LIST - Select a folder to view A ADDRESS BOOK - Update address book S SETUP - Configure Pine Options Q QUIT - Leave the Pine program

11

Pine’s main menu (Bottom)

This is the bottom of the main menu:

Copyright 1989-1998. PINE is a trademark of the University of Washington.

? Help P PrevCmd R RelNotes O OTHER CMDS > [ListFldrs] N NextCmd K KBLock

12

Reading your mail with pine

From the main menu, type “l”, or use the arrow keys to highlight “Folder List” and press <enter>

Select “inbox” from the folder list– pine displays all current messages. Again, use the

arrow keys to move to the note you wish to read, then press <enter> to see it displayed.

13

Commands used in message index:

These command appear at the bottom of the screen when viewing your inbox (or any other message index):

? Help < FldrList P PrevMsg - PrevPage D Delete R Reply O OTHER CMDS > [ViewMsg]N NextMsg Spc NextPage U Undelete F Forward

Note: always look at the bottom of the screen to see

what commands are currently available.

14

Sending Mail

To start:– $ pine <username> from the Unix prompt– <select “Compose Message” from pine’s main menu

and enter the username.

Enter a Subject line

Go to the “message text” area and type your message.– Note the editing commands at the bottom of the screen.

15

Other pine Features

Maintain multiple message indexes (folders) by simply saving your e-mail notes to a different index.– To create a new folder or append to an old one, simply

save an e-mail note to the folder

Keep commonly used e-mail addresses in the “address book”.– Enter the address book from the main menu and follow

the directions.– May also “take addresses” while reading a note.

16

Getting Help

$ man --- Run command interactively

$ man <Topic> --- Get help on <Topic> $ man -k <Keyword> --- Find all Topics which

contain <Keyword>

17

Creating And Editing Files

$ vi <filename> Modes:

– vi Input mode– vi Command mode– vi last line mode– ex Command mode– ex input mode

18

Starting and Stopping

$ vi lab2.txt Commands to exit (Note Case):

– ZZ -- save and exit from vi– :wq -- save and exit from vi– :w lab2.bak -- save as "lab2.bak"– :q -- QUIT, no changes have been made– :q! -- QUIT, destroy all changes– $ vi --r lab2.txt -- start & recover

19

Basic Commands

h -- move one character to the left. l -- move one character to the right. j -- move one line down. k -- move one line up. w -- move one word right. b -- move one word left. i -- enter input mode before current point. I -- enter input mode at beginning of current line. a -- enter input mode after current point. A -- enter input mode at end of current line.

20

Basic Commands 2

O -- open a new line before the current line and enter input mode. o -- open a new line after the current line and enter input mode. <ESC> -- switch from input mode to command mode. r <char> -- replace the current character with <char>. R <text> -- replace characters with text (end with <ESC>). u -- undo the command just executed. U -- undo all changes to the current line. /<text> -- search for the next occurrence of <text> in the buffer. ?<text> -- search for the last occurrence of <text> in the buffer. :s/<old>/<new> -- substitute new for old text on the current line. :s/<old>/<new>/g -- substitute new for old text globally.

21

Basic Commands 3

cw back last delete before current -- change to end of word. c3w -- change to end of 3rd word. dd -- delete current line. 5dd -- delete next five lines. dw -- delete current word. p -- bring back last delete after current character. P -- bring character.

22

PICO

“pico” is another editor whose format looks suspiciously similar to pine’s.

To run– $ pico <filename>

Commands appear at the bottom of the screen (as with pine). Most are self-explanatory.

23

Advanced Utilities

who: Displays ``who'' is currently logged into the system.

write <User>: Allows you to write to another user. mesg: Turn on and off "write" access. echo <text>: Displays <text> on the screen. date: Displays the current date. lpr <filename>: Prints <filename>.

24

Even More Utilities

head <filename>: Displays the first 10 lines of <filename>.

tail <filename>: Displays the last 10 lines of <filename>.

uniq: Prints only unique rows (duplicates omitted). diff <filename 1> <filename 2>: Compares files. sort <filename>: sorts <filename>. finger <Username>: displays information about

<Username>.

25

Directories

Special Directories:– HOME directory -- where you initially log in to.

Manipulating directories:– Creating Directories -- use mkdir <directory name>.– Deleting Directories -- use rmdir <directory name>.– Changing Directories -- use cd <directory name>.

26

THE PATH

echo $PATH gave you your PATH. All the places to look for programs to execute.

27

Files

Special files:– .profile (or .login)

Commands to be executed every time you log in.

– .shrc (or .cshrc, or .bashrc) Commands to be run every time you spawn a new shell.

– .plan A text file to be displayed when another user “finger”s your

account.

Note: all filenames that begin with a “.” are hidden.

28

ACCESS PERMISSIONS

Owner, Group, All Access May be Read, Write, Execute for all three. Current settings found with

ls -l:

-rwxr-x-r-- mjm mjm Sept 10 myfile.txt

29

GRANTING ACCESS

$ chmod 604 .plan– each different user group:

1 is execute privilege 2 is write privilege 4 is read privilege

– 604: owner has read and write group has no privileges all has read privilege

30

GRANTING ACCESS 2

$ chmod a+r .plan– First character is:

o owner g group a all users

– Second character is: + add this privilege - remove this privilege = set privilege

– Third character is: r read w write x execute.

31

FILE WILDCARDS

* -- matches any string. ? -- matches exactly one character. [ ... ] -- matches any element of a list.

32

INPUT/OUTPUT REDIRECITON

Output Redirection:– $ who -b > .lastboot

Input Redirection:– $ Mail marty < MyNote.txt

33

GREP and Regular Expressions

GREP: "Get Regular ExPression"– Searches files for a string.– For example:

$ grep Smith test.txt $ grep "Aaron, Mary" test.txt

34

GREP Options

--c : COUNT matches. --i : IGNORE case. --l : LIST file names -n : precede each match with a line NUMBER. -v : reVERSE match.

35

Regular Expressions

The "string" is a regular expression. May contain wild cards.

– A period: match exactly one character.– Square bracketes [...]: a character class– *: match zero or more occurences of the previous RE.

EXAMPLE: ab*c

– ^ : match the beginning of a line.– $ : match the end of a line.– \. \* \^ \$ : Match special character.

36

Pipes

Take output of one command as input to another:– $ who | sort

This is equivalent to – who > temp– sort temp– rm temp

37

"tee" And The Background

"tee”: send the output of one command to the input of another command

Example:– who | tee who.out | grep cs– Find people logged in with "cs" in their username– display it AND save it to a file called "who.out"

38

Running In The Background

Multitasking Aspect of UNIX. One task is in the foreground; others in

background. To run in background: append an ampersand (\&)

to the command line. Example:

– ls -l | grep -i ”^d" > temp.txt &

39

Terminating Processes

To kill all in the background,– kill 0

To kill One:– Find its PID (process ID) with "ps" (process status)– kill <PID>

40

sed AND awk sed

sed: "stream editor"– A non-interactive editor– Input:

A list of editing commands. A file to operated on.

– Output: The lines of the file affected.

41

Example:

Data File (called "new"):– Line one– The second line– The third– This is line four– Five– This is the sixth sentence– This is line seven– Eighth and last

42

Example (cont)

sed '/line/p' new– Prints all lines containing "line".– Prints all lines it processes. – Lines 2, 4, and 7 will be printed twice.

43

Example (cont)

sed --n '/line/p' new– The "--n" option tells sed to print just the lines that

match.

sed --n '3,6 p' new– This command prints out lines 3 through 6.

sed 's/line/sentence/g' new.txt– This command substitutes each occurrence of "line"

with "sentence" and prints the output.

44

Editing Scripts

sfile: 2,4 c\ SED WILL INSERT THESE\ THREE LINES IN PLACE\ OF THE SELECTED LINES\

45

Editing Scripts (cont)

sed -f sfile new.txt– Line one– SED WILL INSERT THESE– THREE LINES IN PLACE – OF THE SELECTED LINES– Five– This is the sixth sentence– This is line seven– Eighth and last

What if I use the --n option here?

46

Observation

What goes in the script is a regular expression, preceded by an optional address range. If none is given, sed will work on every line in the file, one at a time. Also a command may be given. These are VERY similar to the command line inputs in VI (note the similarity with substitute, change, etc).

47

awk

A pattern scanning and processing language. Constructs reminiscent of "C”. <PLACE CARS FILE HERE!!!!!>

48

awk Examples

To find all chevys: – awk '/chevy/' cars

To print the year and make of every car:– awk '{print $3, $1}' cars

To print the year and make of all chevys:– awk '/chevy/ {print $3, $1}' cars

To print those cars whose make begins with a "h":– awk '/$1/^h/' cars

To print all cars made in 65:– awk '$3 == 65' cars

49

Using awk Files

To print a header for the file, create a file called "awkfile" which contains:– BEGIN { print "Make model year mileage price"– print "----------------------------------------"}– {print}

awk -f awkfile cars

50

Variables

Pre-Defined Shell Variables– HOME -- The path to your home directory.– PATH -- your current path.– MAILPATH -- your current mail path.– PS1 -- your current primary shell prompt.– PS2 -- your current secondary shell prompt.

To see any of these variables:– echo $<VAR>

51

User Variables

Program to maintain a checkbook: $ awk 'NR == 1 {balance = $0} NR > 1

{balance+=$3; print $0, balance}' infile To save typing: $ docheck=awk 'NR == 1 {balance = $0} NR > 1

{balance+=$3; print \$0, balance}' infile To execute: $ $docheck To save command, put it in your ".profile" file.

52

Command Substitution

Variables may be assigned the RESULT of a command:– $ currdir=`pwd`– $ echo You are currently using the $currdir directory

This can be done in one step:– $ echo You are currently using the `pwd` directory