114
UNIX Shell Scripting Y.V.S Prasad

Unix 3Days

Embed Size (px)

Citation preview

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 1/114

UNIX Shell Scripting

Y.V.S Prasad

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 2/114

 An Operating System is the Interface between theUser (software) and the Computer (hardware).

USER OS COMPUTER  

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 3/114

UNIX is a multiprogramming

Operating System. It permitsmultiple people to run multipleprograms.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 4/114

I/O management

data management

command execution

program development tools

portability 

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 5/114

time sharing

security 

communications

accounting

graphics

Internet

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 6/114

BASIC COMMANDS

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 7/114

$ clear$ tput clear

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 8/114

$ date Date, Time and TimeZone

$ date +%D Date in dd/mm/yy 

$ date +%T Time in hh:mm:ss$ date +%Z Time Zone

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 9/114

$ pwd

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 10/114

$logname

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 11/114

$tty 

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 12/114

$uname Name of UNIX 

$uname r Current Release of UNIX 

$uname n Host Name or Domain Name

$uname a Available Information of UNIX 

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 13/114

$passwd

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 14/114

$ who

Col. 1: user names

Col. 2: device names of the terminals

Col. 3, 4, 5: date and time of logging in

Col 6: machine name$  who Hu u gives more detailed

info. and H with headers

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 15/114

$cal calendar of the current month

$cal 03 calendar for the year 03 AD

$cal 03 2008 calendar for march 2008 AD

$cal 09 1752 11 days have been adjusted toaccommodate the concept of leap year.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 16/114

$echo computer Displays the word computer

$echo computer\c Displays the word computer

and the cursor continues OTHER FORMATS:

\a bell \t tab

\b backspace \\ back slash

\c no new line \0n ascii character (n is octal)

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 17/114

$ printf my current shell is %s\n $SHELL

OTHER FORMATS:

%d decimal

%o octal

%f float

%s string%30s 30 characters space wide

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 18/114

$ type date/usr/bin/date

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 19/114

$ man date

$ man pwd

$ man man

MANUAL SECTIONS:

1 User Programs 5. Admin. File Formats

2 Kernel System Calls 6. Games

3 Library Functions 7. Macros4 Special Files 8. Admin. Commands

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 20/114

$ stty a Displays all options

$ stty echo suppresses the echo of input$ stty echo restores the echo

$ stty echoe backspace does not remove

the character

$ stty echoe backspace removes the

character

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 21/114

$ stty intr \^c control c is the interrupt key 

$ stty eof \^a control a is eof character

$ stty sane restores sanity to the terminal.COMBINING THE COMMANDS

$ date ; pwd

$ (date ; pwd)

$ (date ; pwd ) > newlistThe combined output of the two commands is sent to

the file newlist

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 22/114

$ exit exits from the current shell

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 23/114

$ cat > abc creating a file abc

.

.^d

$ cat abc displaying the contents of abc

$ cat v abc displays non-printable chars.

$ cat n abc numbering the lines

$ cat abc def > ghi merges two files abc, def 

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 24/114

$ cp abc abc1 copies abc into abc1

$ cp abc abc1 abc2 d1 the last argument must bea directory 

$ cp -i abc abc1 interactive copying

$ cp R progs newprogs recursive copying

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 25/114

$ rm abc removes a file abc

$ rm i abc interactive removal

$ rm r d1 recursive removal$ rm R d1 recursive removal

$ rm f abc forced removal

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 26/114

$ mv abc abc1 renames a file abc to abc1

$ mv d1 d2 renames d1 directory to d2

$ mv a b c d1 moves a b c to d1 directory 

$ mv i abc abc1 interactive renaming

$ mv R d1 d2 recursive renaming

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 27/114

$ lp abc prints the file abc

$ lp dlaser abc default printer is laser

$ lp n3 m abc prints 3 copies and mails

the user a message

$ lp tChapter1 abc prints the title chapter1

$ lpstat gives the status of print jobs$ cancel request-id to cancel the request-id

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 28/114

 W hen you log on to the system, UNIX automatically 

places us in a directory called the Home Directory.

To know your Home,

$ echo $HOME /home/prasad

~/foo here ~ identifies home

~prasad home of Prasad

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 29/114

$ mkdir d1 create the directory d1

$ mkdir p d1/d2/d3 create the path d1/d2/d3

$ rmdir d1 removes d1 (d1 empty)$ rmdir p d1/d2/d3 removes tree (empty)

$ cd /home/kumar changes to kumar directory 

$ cd .. moves to parent directory  

$ cd moves to Home

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 30/114

$ ls lists all in the ascending order

$ ls r lists in the descending order

$ ls a lists all files including . Files$ ls C width wise

$ ls F * (exe), / (dir), @ (links)

$ ls i along with i-node nos.

$ ls l long format

$ ls t sorts files (modi. Time)

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 31/114

$ ls s lists file sizes in blocks

$ ls x multicolumn output$ ls R recursive listing

$ ls xR combined effect of x and R  

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 32/114

Every file or directory is associated with 3 types of 

people. The User (owner of the file), the Group

(people who are close to the owner and whosepriority comes after the owner and perform a

common task) and Others (who are neither Users

nor Group).

The first column of $ ls l command would give the

permissions each category holds.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 33/114

Permission file directory  

read ( r ) view, copy, compileview

  write (w) modify add or del.

files

Execute (x) run to change

to the dir.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 34/114

Symbolic Method:

u user + give

g group - remove

o others = absolute give

a all r read

 w write

x execute$ chmod u+x, g-r abc exec. for user, read for group

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 35/114

 Absolute Method:

4 read

2 write

1 execute0 no permission

$ chmod 777 abc read, write, exe. for all

$ umask to know the default perm.

$ umask 0222 suppress these permissionsto create default permissions

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 36/114

 W henever, a file is created, the filename is stored at

one place and the contents of the file are stored at a

different place on the storage media. Both are linked

together by a no. known as i-node no. or index nodeno.

 W hen we copy a file, altogether a new file is created,

and the contents of the source file are copied to the

target file. This leads to repetition of same contentsand thus disk space wastage.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 37/114

Instead, we can create a link to that file, which is

actually nothing but copying the i-node no. of the

file to the new file rather than the contents. This is

called linking.

Linking is of two types.

1. Hard links

2. Soft links

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 38/114

 W hen we create a link within the same file system, it

is a hard link. A soft link spans across the file

systems.

Hard link will have the same i-node no. But soft link

 will store the path of the source file and thus has a

different i-node no.

For directories, we can create soft links only. Softlinks are also known as symbolic links.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 39/114

In the case of hard link, even if the source file is

removed, the contents can be accessed through the

link.

In the case of soft link, if the source file is removed,

the soft link does not point to anything and thus the

contents can not be accessed.

$ln abc abc1 hard link of abc

$ ln s abc /var/abc1 soft link of abc

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 40/114

 vi stands for visual editor. This was developed by 

Bill joy of BSD.

 vi has 3 modes:

1. Append or Insert mode (data entry)

2. Command or Escape Mode (editing)

3. Last Line or Ex Mode (file operations)

$ vi new

By default, when we invoke vi, we are in mode 2.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 41/114

a append

i insert

l move the cursor right

h move the cursor left

k move the cursor up

  j move the cursor down

x delete one characterdw delete one word

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 42/114

dd deletes entire line

d$ deletes from current position to the end of line

d0 deletes from current position to the beg. of line

u undo

  J join

G go to specific line no.

r replace one characterR replace till esc. is pressed

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 43/114

o open mode after the line

O open mode before the line

  yy yanking

pp pastingLast Line Mode Commands: W hen we press : from

command or escape mode, it is printed at the last

line. These commands are to be given at that colon.

:wq write and quit

:x write and quit

:q! quit without saving

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 44/114

:s ///g substitute globally 

: w write

:w! overwrite

:n next file:e file edit the file

:! execute a UNIX command

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 45/114

Filters are commands that accept data normally from

the standard input, manipulate it and write results to

the standard output.

1. Simple Filters

2. Filters with Regular Expressions grep and sed

3. Advanced Filtering using awk

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 46/114

Simple Filters include head, tail, tr, sort, uniq, cut,

paste, pr, comm, diff etc.

$ head emp.lst

Displays first 10 lines of the file, from the beginning.

$ head -15 emp.lst

Displays first 15 lines of the file, from the beginning.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 47/114

$ tail emp.st

Displays last 10 lines of the file, from the end.

$ tail -15 emp.lst

Displays last 15 lines of the file, from the end.$ tail +25 emp.lst

Displays lines starting with the line no.25 to the end.

$tail c -512 emp.stCopies last 512 bytes from emp.st

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 48/114

$ tr a A

Translates each a with A in the input

$ tr ax by

Translates a with b and x with y in the input$ ls l | tr s

Squeezes multiple occurrences of spaces to one.

$tr d | < emp.lstDeletes the character | from the file emp.lst

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 49/114

 A file (data base file) can be sorted in the ascending

or descending order by sort.

$ sort emp.lst

Sorts in ASCII collating sequence white space first,numerals next, uppercase letters and finally lower

case letters.

$sort r emp.lstSorts in the reverse order

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 50/114

$ sort n emp.lst

Sorts according to numeric order

$ sort u emp.lst

Sorts uniquely $ sort f emp.lst

Sorts in the insensitive case order

$

sort emp.lst o emp.lst1Sorts and stores the output in the file emp.lst1.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 51/114

$ sort c emp.lst

Checks if the file is sorted

$ sort m emp.lst emp.lst1

Merges two files emp.lst and emp.lst1$ sort t: emp.lst

Sorts by taking : as the delimiter among fields

$

sort k 2 emp.lstSorts on the second field

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 52/114

$ sort k 3,3 -k 2,2 emp.lst

Sorts according to the third field and the secondary 

key is second field.

$ sort k 5.7, 5.8 emp.lstSorts from 7th column of 5th field to the 8th column

of 5th field of emp.lst

Note: Sort considers tab as the default delimiter.However, as a user we should use : as the delimiter.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 53/114

$ uniq emp.lst (emp.lst must be sorted before)

Displays the lines uniquely 

$ uniq u emp.lst

Displays the lines that are only unique$ uniq d emp.lst

Displays the lines that are having duplicates

$

uniq c emp.lstDisplays the frequency of occurrence of each line

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 54/114

$ cut c1 emp.lst

Cuts the file vertically basing on character nos.

$cut c1-5,8 emp.lst

Displays 1 to 5 characters and the 8th character of 

each line

$cut f1 emp.lst

Displays first field of the file

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 55/114

$ cut f1,3 emp.lst

Displays first and third fields.

$ cut f1-3 emp.lstDisplays first, second and third fields

$ cut d: f1 emp.lst

Displays the first field by taking : as the

delimiter

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 56/114

$ paste emp.lst emp.lst1 Joins two files emp.lst and emp.lst1 with the

tab

as the delimiter.$ paste d: emp.lst emp.lst1 Joins two files emp.lst and emp.lst1 with : as

the delimiter.$ paste s emp.lst W ould join all the lines two forma single line

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 57/114

$ pr emp.lst

Prints file by adding suitable headers, footers and

formatted text. Adds five lines of margin at the

Top and five and the Bottom. The header shows

the date and time of lat modification of the file

along with the filename and page number.

$ pr -3 emp.lstPrints in 3 columns

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 58/114

$ pr t emp.lst

Suppresses the header and footer

$pr d emp.lstDisplays in double line spacing

$ pr n emp.lst

Lines are numbered$ pr o 5 emp.lst

Left margin is 5

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 59/114

$ pr h employee file emp.lst

Header is employee file.

$ pr +10 emp.lstPrints from page no. 10

$ pr l 45 emp.lst

Page length is set to 45

$ pr l45 emp.lst | lp

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 60/114

$ diff emp.lst emp.lst1

Displays file differences. Suggests changes in

order that the two files are identical.  Append a

Delete d

Change c$ diff e emp.lst emp.lst1

This produces a set of instructions only 

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 61/114

$ comm emp.lst emp.lst1

Both these files must be sorted. Shows 3 column

output. The first column contains the entries only 

available in the first file, the second columncontains the entries only available to the second

file and the third column contains common

entries.$ comm -1 emp.lst emp.lst1

Suppresses first column in the output.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 62/114

$ comm -12 emp.lst emp.lst1

Suppresses first and second columns in the

output.

$ comm -123 emp.lst emp.lst1

Suppresses all columns in the output.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 63/114

GREP stands for Global Regular ExpressionPrinter.$ grep options pattern filename(s)

Options:-i ignore case -c count lines-v inverse role -l file names only  

-n lines are numbered -f patterns in a file-e multiple patterns -E EREs

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 64/114

Basic Regular Expressions (BRE):* Zero or more occurrences of the prev.

chr.

. A single Character

.* Any no. of characters or none

[abc] a or b or c

[a-z] any character between a to z[1-3] any digit between 1 to 3

[^abc] not a or not b or not c

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 65/114

Basic Regular Expressions (BRE):[^a-zA-Z] non-alphabetic character

abc exact character sequence abc

^abc abc not at the beginning of the line

abc$ abc not at the end of the line

^abc$ abc as the only word in line^$ lines containing nothing

\ nullify the meaning of meta characters

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 66/114

Extended Regular Expressions (ERE):

ab+c a followed by one or more bs followed by c

ab?c a followed by optional b followed by c

abc or ac

a|b either a or b

(a|b)c either ac or bc

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 67/114

Interval Regular Expressions (IRE):ab {2,4}c a followed by 2,3,4 bs followed by c

ab{2,}c a followed by at least 2bs followed by c

ab{2}ca with 2bs and c

Ex:

grep i abc emp.lst emp.lst1

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 68/114

SED is a multipurpose tool which combines the work

of several filters.

Ex: sed options address action file(s) Addressing in sed is done in two ways:1. By one or two line nos.2. By specifying /pattern/

Line Addressing:$ sed 3q emp.lstDisplays first 3 lines of the file and quits from sed.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 69/114

$ sed n 1,3p emp.lstDisplays first 3 lines of the file. (p and n must be

used)$

sed n $p emp.lstDisplays the last line of the file

$ sed n 1,2p7,9p emp.lst

Displays selective groups of lines$ sed n 3,$!p emp.lstDo not print the lines from 3 to the end of the file.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 70/114

Using Multiple Instructions:

$ sed n e 1,2p e 7,9p emp.lst

Putting instructions in a file:

$ cat > patfile

1,2p

7,9p

^d

$ sed n f patfile emp.lst

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 71/114

Context Addressing:

$ sed n /director/p emp.lst

Displays all the lines that contain director

$ sed n /director/, /manager/p emp.lstDisplays all the lines from director to manager

$ sed n 1, /director/p emp.lst

Line nos. and context addresses can be mixed$ sed n /^a/p emp.lst

Displays all the lines that start with p (regular exp.)

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 72/114

 W riting selected lines to a file:

$ sed n /director/w dlist emp.lst

$ sed n /director/w dlist

/manager/w mlist emp.lstText Editing:

Inserting i

 AppendingaChanging c

Deleting d

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 73/114

$ sed 1i\

abc \

pqr emp.lst

$ sed 1a\

abc\

pqr emp.lst

$ sed 1c\

abc emp.lst

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 74/114

$ sed /director/d emp.lst

SUBSTITUTION:

$ sed s/director/director1/g emp.lst

$ sed 1,5 s/director/director1/g emp.lst

MULTIPLE SUBSITUTIONS:

$ sed s/i/m/g

s/x/y/g emp.lst

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 75/114

Named after its authors Aho, W einberger andKernighan, awk, until the advent of Perl, was the mostpowerful utility for text manipulation.

Syntax:awk options selection_crateria {action} file(s)

The selection_crateria filters the input and selects lines

for action component to act upon.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 76/114

Examples:

$awk /director/ { print } emplist

Checks for the pattern director and prints the entire

line(s). If selection_crateria is missing, the actionapplies to all the lines. If action is missing, the entireline is printed. Either of the two is optional (but notboth), but they must be enclosed within a pair of single

(not double) quotes.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 77/114

The following formats are equivalent:

$ awk /director/ emplist

$ awk /director/ {print} emplist$ awk /director/ {print $0} emplist

awk uses the special parameter, $0, to indicate the entire

line. It also, identifies the fields by $1, $2, $3,

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 78/114

$ awk /director/ { print $1, $2} emplist

Unlike other Unix filters, awk uses a contiguoussequence of space and tabs as a single delimiter. If thedelimiter is other this, we have to explicitly express.

$ awk F| /director/ { print $1, $2} emplist

Line addressing is allowed in awk with the help of the

built-in variable NR. This prints the lines from 3 to 6.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 79/114

$ awk F| NR==3, NR==6 {print NR, $1, $2, $3}emplist

C-like printf statement is available in awk to format theoutput.

$awk F| /director/ { printf %3d %-20s %d\n, NR,$1,$2} emplist

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 80/114

Every print or printf statement can be separately redirected with the > and | symbols. However, makesure that the filename or command that follows thesesymbols is enclosed within double quotes.

$ awk F| /director/ { print $1, $2 | sort } emplist

$ awk F| /director/ { print $1, $2 > abc } emplist

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 81/114

Every expression in awk is interpreted either as a stringor a number, and awk makes the necessary conversionaccording to context. Awk allows the use of user-defined

 variables but without declaring them. Variables are case

sensitive.Ex: x=sun; y=com

print x y gives suncom

x=5; y=6;

print x+y gives 11

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 82/114

Logical Operators:

|| (or), && (and), ! (not)

$ awk $3==director || $3==chairman {print } emplist

Regular Expression Operators:~ (match), !~ (no match)

$ awk $3 ~ /^a { print } emplist

Number Comparison: >, >=, <, <=, ==, !=

 Arithmetic Operators: +, -, *, /, %

$ awk $3 > 2000 { printf %d\n, $2*0.5 } emplist

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 83/114

Built-in Variables:

NR cumulative no. of lines read

FS input field separator

OFS output field separatorNF no. of fields

FILENAME current input file

  ARGC no. of command line arguments  ARGV list of arguments

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 84/114

 Awk patterns can be put in a file and we can awk tolook for the pattern in that file and execute on theinput file. Here the file is pattern.awk.

$

awk f pattern.awk emplistBEGIN & END Sections: BEGIN performs actionsbefore processing each line and END performsactions after the last line of the file. BEGIN {action}

and END {action} is the syntax.$ awk BEGIN {print welcome} /director/ {print} END

{print Bye} emplist

awk reads standard input when filename is omitted.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 85/114

 Arrays:

 An array is considered declared the moment it is used

 Array elements are initialized to zero or empty string

unlessinitialized explicitly. Arrays expand automatically. The

index

can be anything even a string

Ex:$ awk BEGIN { print REPORT } /director/ {tot[1]=tot[1]+$6 } END {print

$tot[1] } emplist

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 86/114

 Associative Arrays: Awk does not treat array indexes as integers, the arraysare associative, where the information is held as key-

 value pairs. The index is the key that is saved internally 

as a string. W hen we set an array element usingmon[1]=mon, awk converts the number 1 to a string.There is no specified order in which the array elementsare stored.

$ nawk BEGIN {print HOME = ENVIRON [HOME]}

emp.lst

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 87/114

Functions:int (x) returns integer value of xsqrt (x) returns square root of xlength returns the length of a complete line

length (x) returns the length of xsubstr (string,m,n) starting from m, n characters asa string

index (s1,s2) returns the position of s2 in s1split(string,array,ch) splits the string into an array 

using ch asthe delimiter

system (cmd) executes operating system commandsand returns exit status

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 88/114

CONTROL FLO W :if (condition) { statements }if (condition ) { statements } else {statements}for (k=1;k<=10;k++){

statements}for ( k in array){

statements

} while (condition){

statements}

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 89/114

FIND is a one of the power tools of UNIX. It recursively examines a directory tree to look for files matchingsome criteria and then takes some action on the selectedfiles.

Ex: find path_list selection_criteria action

Path List: Path List can be one or more sub-directoriesseparated by white space.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 90/114

SELECTION_CRITERIA:-inum n Having inode no. n-type x where x is f (ordinary file),

d (directory), l (sym. Link)-perm nnn matches permissions nnn-links n having n links-user username owned by username-group gname owned by group name-size +x[c] if size is > x blocks (c for char)

-mtime x if modified in < x days

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 91/114

SELECTION_CRITERIA:-newer fname if modified after fname-atime +x if accessed in more than x days-name fname file name

-prune dont descend directory if  matched

 ACTION:-print prints on the output

-ls executes ls lids command-exec cmd executes UNIX command cmdfollowed by {}\;

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 92/114

xargs is a command of Unix and most Unix-like operatingsystems. It is useful when one wants to pass a largenumber of arguments to a command. Arbitrarily long listsof parameters can't be passed to a command,so xargs will

break the list of arguments into sublists small enough tobe acceptable.For example, commands like:

rm /path/*rm ̀ find /path -type f`

 will fail with an error message of "Argument list too long"if there are too many files in /path.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 93/114

find /path -type f -print0 | xargs -0 rmIn this example, find  feeds the input of xargs with a longlist of file names. xargs then splits this list into sublistsand calls rm once for every sublist. This is more efficientthan this functionally equivalent version:find /path -type f -exec rm '{}' \;

 which calls rm once for every single file. Note howeverthat with modern versions of  find , the following variantdoes the same thing as the xargs version:

find /path -type f -exec rm '{}' +

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 94/114

zipping the files abc, pqr and lmn:$ zip final.zip abc pqr lmn

recursive zipping:$ zip r final.zip d1 (d1 is a directory)unzipping the zipped files:$

unzip final.zip viewing the zipped files:$ unzip v final.zip

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 95/114

Zipping a file:$ gzip abc.txt lmn.txtFiles abc.txt.gz and lmn.txt.gz are createdHow much zipping is done?

$ gzip l abc.txt.gz lmn.txt.gzUnzipping a file:$ gzip d abc.txt.gz lmn.txt.gz$ gunzip abc.txt.gz lmn.txt.gz

Recursive zipping:$ gzip r d1 (d1 is a directory)Unzipping recursively:$ gzip dr d1 or $ gunzip r d1

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 96/114

 A process is the instance of a running program. A process is said to be born when the programstarts execution and remains alive as long as theprogram is active. After execution is complete,

the process is said to die. Each process isuniquely identified by a unique integer calledthe process-id or PID.

$ psDisplays PID, TTY, TIME (cumulative processortime that has been consumed since the processstarted) and the CMD (process name)

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 97/114

$ ps f 

Displays full listing$ ps e or $ ps A  All processes including user and system processes$ ps u user

Displaying the processes of a User$ ps aProcesses of all users excluding processes notassociated with terminal.$ ps lLong listing showing memory related information$ ps t termDisplays processes running on the terminal

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 98/114

 A process can be run in the background. This isachieved by placing an & at end of thecommand.$ sort emp.lst o emp.lst &

 W hen a user logs out, the Shell is killed and allthe background processes are also killed. W ecan avoid this by using nohup command.

$ nohup sort emp.lst o emp.lst &This sends the output to nohup.out file. Even if the parent is killed, the background processruns and the result is sent to the file, nohup.out

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 99/114

Processes in UNIX are usually executed withequal priority. The priority levels can be alteredusing nice command. A higher nice value alower priority.

Nice priorities normally range from 0 to 39.

$ nice n 5 wc l uxmanual &Nice value is increased by 5 units.

$ ps o nice shows the nice value.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 100/114

 A Signal is used to communicate the occurrenceof an event to a process. Each Signal isidentified by a number and is designed toperform a specific function. Signals can begenerated from the keyboard or by the killcommand. Signals are represented by theirsymbolic names having the SIG prefix.If you want to terminate a program, younormally press the interrupt key. This sends theprocess the SIGINT signal (no. 2). The defaultaction of this signal is to kill the process. A process may also ignore a signal or executesome user-defined code written to handle thatsignal.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 101/114

There are two signals that a process cannotignore or run user-defined code to handle. They are SIGKILL and SIGSTOP.

kill command sends a signal, usually with theintention of killing one or more processes.

$ kill 105$ kill s KILL 105 (or) $ kill -9 105

$ kill l will display the list of signal names and theirnos.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 102/114

 A job is the name given to a group of process.Process activity is related to kernel whereas the

 job activity is related to shell.$ wc c /

Say that this command is taking too long, then  we can suspend this command by pressingcontrol-z.[1] + stopped wc c /$ bgforces the command to run in the background$ jobs

 will show the list of background jobs

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 103/114

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 104/114

  AT tells UNIX when to execute a set of commands.$ at 14:08.control-d$ at l

gives the list of at jobs$ at rto remove the list of jobs from the queue.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 105/114

Other formats of AT are:

$ at 15$

at 5pm$ at 3:06pm$ at noon$ at now + 1 year

$ 3:08pm + 1 day $ 15:08 December 18, 2008$ at 9am tomorrow

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 106/114

BATCH commands are executed as soon as thesystem load permits.

$batch..

..control-d

 job 10411856731.b at Sun Dec 29 13:14:33 2009

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 107/114

CRON is a system process (daemon) thatexecutes programs at regular intervals. It mostly dormant, but every minute, it wakes up andlooks into a control file called the crontab file.Creating a crontab file:

create a file cron.txt with the following 6 fields.$ vi cron.txt00-10 17 * 3,6,9 5 wc c abcfield 1: 00-59 minutes field 4: month

field 2: 1-24 hours field 5: Friday  field 3: 1-31 day field 6: command$ crontab cron.txt

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 108/114

$ crontab ldisplay the contents of crontab file$ crontab rremoves the contents of crontab file$ time sort emp.lst o emp.lstdisplays 3 timesreal: time elapsed from the invocation of the

command until its termination.user: time spent by the program in executing

itself.sys: time spent by the kernel

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 109/114

TRAP traps the signals and executes thecommands. It is normally put at the beginningof the shell script.$ trap command_list signal_list

 W hen a script is sent any of the signals insignal_list, trap executes the commands in thecommand_list. The signal list can contain the

integer values or names (without the SIGprefix) of one or more signals the ones which  you use with the kill command. Instead of using 2 15 to represent the signal list, you canalso use INT TERM etc.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 110/114

$ trap Pressed control c or control z ; exit INT TERM while truedo

sleep 60done You may ignore the signal. This can be achieved by,$ trap 1 2 15That is you should put a null command list.$ trap To reset the signal to their defaults

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 111/114

 W hat is a Shell? A Shell is the user interface to the

Unix Operating System (Kernel). Ittakes the input from the user andinterprets to the Operating System

and conveys the output from theOperating System back to the user.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 112/114

SHELL PROGRAMMING

User Shell Kernel

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 113/114

SHELL PROGRAMMINGUnix is one of the first operatingsystems to make the user interface

independent of the operatingsystem. Even though there is only one Kernel running on the system,there can be several Shells in action one for each user who is loggedin.

8/8/2019 Unix 3Days

http://slidepdf.com/reader/full/unix-3days 114/114

SHELL PROGRAMMINGPopular Shells:

1. Bourne Shell (sh)

2. Korn Shell (ksh)

3. C Shell (csh)

4. Bourne Again Shell (bash)

5. Tenex C Shell (tcsh)

6. Zee Shell (zsh)