340
Unix Overview Concept,presentation and training by: Dhanabal S.

Unix Training by Dhanabal

Embed Size (px)

Citation preview

Page 1: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 1/340

Unix Overview

Concept,presentation and training by:

Dhanabal S.

Page 2: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 2/340

This is not an exhaustive training on Unix , coveringall the arenas with full details.

Even the topics covered in this presentation are notall inclusive.

But, it will definitely help you to be a confidentsailor in the ocean of Unix – no matter what thecircumstance is.

Expectation from this training

Page 3: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 3/340

This training will teach you about 90 commands!!!

And will make you bore with more than 350 slides!!!!

But don‟t be nervous – remember the utilities of the

commands –  for this presentation will be with youfor the rest of your life to provide you the syntaxfor using them.

Don‟t be nervous,please 

Page 4: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 4/340

  Duration

15 Hours ( 3 Days)

Schedule29th Sept-1st Oct,2003 

Timings

11:00 am – 1 pm

2:00 pm – 5 pm 

Page 5: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 5/340

Purpose

To get acquainted with the basic Unix commands

To learn a bit of Shell Programming

Get introduced to Unix Operating System

Page 6: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 6/340

Operating System

An operating system is a living, breathing software entity. Thesoul of the computing machine, it is the nervous system thatturns electrons and silicon into a personality. It brings life tothe computer 

- from Mike Gancarz's "The UNIX Philosophy"

The OS sits between users and hardware providing translationservices. It speaks the language of the hardware to perform

basic tasks such as the definition of memory or the allocationof disk space using the hardware.

Page 7: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 7/340

Unix Operating System

In UNIX, the operating system is broken into three pieces: thekernel, the shell, and the built-in utilities. The kernel is responsiblefor low level hardware communication, the shell provides human userswith a user-friendly interface, and the built-in utilities provide basictools for doing work.

Hardware

Kernel

Shell

Page 8: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 8/340

•Heart of The Unix OS.

•Collection of C programs directly communicating withhardware

•Part of Unix system loaded into memory when Unix is booted

Manages:-

1. System resources

2. Allocates time between user and processes

3. Decides process priorities

Kernel

Exit

Page 9: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 9/340

•Human interface point for Unix

•Program layer –  provides anenvironment for the user to entercommands to get desired results.

•Korn Shell, Bourne Shell, C Shell arevarious shells used by Unix users

Shell

Exit

Page 10: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 10/340

User login process

Unix booted.

Program Unix(kernel) is booted into main memory,

and remains active till the computer is shut down

Program init runs as a background task and

remains running till shutdown

Page 11: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 11/340

 User attempts to log in.

Kernel calls program ‘init’. 

‘init’ scans file /etc/inittab , which lists the ports with

terminals and their characteristics and returns anactive open terminal to ‘init’. 

‘init’ calls program ‘getty’, which issues a loginprompt in the monitor

User enters login and password

Page 12: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 12/340

‘getty’ calls program ‘login’ which scans file/etc/passwd to match username and password

After validation, control passes to session startupprogram /bin/sh , session startup program

Program /bin/sh reads file /etc/profile and .profileand sets up system wide and user specificenvironment.

User gets a shell prompt

Exit

Page 13: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 13/340

Understanding Commands

General Purpose Utilities

Handling Files

Filters

Controlling Environment

Process

System Administration

Communication

Page 14: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 14/340

Understanding Unix Commands 

By end of this section , you will be able to know

What a command is?

„Which‟ –  program related to a command

„Man‟ –  get online help of commands

„History‟ –  previously executed commands

„Alias‟ –  Call a Command by another name

Page 15: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 15/340

To get work done by Unix, there is no way

but to execute : commands.

Commands can be typed in from keyboard or

taken from a file

Unix offers a variety of commands for each

category of jobs.

Commands execute a program in the

background which performs the desired job

Page 16: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 16/340

Independent Commands

Do not require any input for execution

$pwd/home/ems2000

Dependent Commands 

Require input for execution

$type ls

ls is a tracked alias for /usr/bin/ls

Mixed Commands:- Can work independently/dependently

$ls

input queuesentwebcatalogs web

$ls queue

queue

Page 17: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 17/340

General syntax to use commands 

<command> [ <option flag> ] [<arguments>]

A statement asking for execution of a command usually consists ofthree distinct sections:-

<command> : Keyword for the command

<option flag> : Starts with „-‟ sign. Decides the nature of output

from the results of command execution

<arguments> : May be a string or the name of file(s) on which thecommand will act upon for desired output.

Example :

$grep -l “ems2000” *.sh 

The above command from the OS prompt searches for the string“ems2000” in all the files with extension : .sh and shows the nameof the files containing it.

Page 18: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 18/340

More than one commands can be stated from OSprompt at a time

$ pwd;tty

/home/ems2000/queue

/dev/pts/tb

First, pwd command is executed which displays the output : /home/ems2000/queue

Then , tty command shows the terminal no: /dev/pts/tb

Any number of commands can be specified at the OS prompt for execution

separated by a ‘ ’ 

A command can spawn more than one lines

$ echo ‘ 

Hello’ 

Hello

Page 19: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 19/340

General Purpose Utilities By end of this section , you will be able know

„banner‟ –  set up poster

„cal‟ –  get Calendar

„date‟ –  get current date

„calendar‟ –  get schedule

„who‟ –  User information

„tty‟ –  terminal information

„uname‟- machine info

„uptime‟ –  System info

„login‟ –  server login

„telnet‟ –  server login

„exit‟- exit current shell

„lock‟ –  lock a terminal

„script‟ –  get the whole job 

„bc‟ –  Unix Calculator

„expr‟ –  Calculations

„factor‟ –  factor : number

„primes‟-Prime number

„units‟-Unit conversion

„tput‟-Control display

„time‟-Time taken bya command

Page 20: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 20/340

Example

Page 21: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 21/340

Use Significance

date +”%A”  Full weekday name(eg., „Wednesday‟) 

date +”%b”  Full month name(„Jan‟) 

date +”%c”  Current date & time representation

date +”%C”  Century(year/100, truncated to nearest number)

date +”%d”  Current Day of the month

date +”%e”  Current Day of the month

date +”%H”  Current hour of time(00-23)

date +”%I”  Current hour of time(12 hour clock)

date +”%m”  Current Month as decimal two digit number

date +”%M”  Current minute of time(00-59)

date +”%n”  Newline character

date +”%p”  AM or PM

Page 22: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 22/340

Use Significance

date +”%R”  Current time as %H:%M

date +”%S”  Current second of time(00-59)

date +”%t”  Tab character

date +”%u”  Weekday as 1 digit number[1-7 for Monday-Sunday]

date +”%w”  Weekday as 1 digit number [ 0-6 for Sunday-Saturday]

date +”%V”  Week number of the yeardate +”%x”  Current date as dd/mm/yy

date +”%X”  Current time in hh:mm:ss

date +”%y”  Two digit year

date +”%Y”  4 digit yeardate +”%Z”  Time zone name

Page 23: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 23/340

Examples of Date

Page 24: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 24/340

$ cat calendar

I have a meeting on 07/10/2003 with Subhomoy

I have a meeting on 08/10/2003 with Biswajit

I have a meeting on 06/10/2003 with Sugata

I have a meeting on 09/10/2003 with my delivery manager

I plan for a dinner on Sep 10, 2003 with my team

I have a meeting on 11/10/2003

I have a meeting on 12/10/2003

I will not come to office on 09/11/2003

$ calendar

I have a meeting on 09/10/2003 with my delivery manager

I plan for a dinner on Sep 10, 2003 with my team

I will not come to office on 09/11/2003

Page 25: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 25/340

$who

sqian pts/td Sep 9 18:27

ems2000 pts/te Sep 10 01:41

$who -Hu

NAME LINE TIME IDLE PID COMMENTS

sqian pts/td Sep 9 18:27 7:32 28935 135.148.207.175

ems2000 pts/te Sep 10 01:41 0:02 29362 129.42.68.104

$who am i

ems2000 pts/te Sep 10 01:41

Page 26: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 26/340

$uptime -u

2:17am up 122 days, 11:03, 2 users, load average: 0.41, 0.41, 0.41

$uptime -h

sqian pts/td 6:27pm 7:39 rlogin htstbw00

ems2000 pts/te 1:41am -ksh

$uptime -l

2:18am up 122 days, 11:04, 2 users, load average: 0.40, 0.41, 0.41

Page 27: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 27/340

Scale:-

By default, bc performs truncated division.One have to set scale tothe number of digits of precision before performing any division.

$bc

scale=2

10/4

2.50

^d

$

If answer to division is greater than the value asdictated by the scale variable, then the valuedictated by the scale is ignored and the real value

is shown.

Page 28: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 28/340

ibase and obase:-

By default, the input and output are interpreted as decimal

values.But, if the demand required input and/or output in differentnumber system(binary, hexadecimal), variables ibase and obase areset.

To convert binary input to decimal output:-

ibase=2

To convert decimal input(default) into binary output:-

obase=2

For hexadecimal systems, value ‟16‟ is used in ibase/obase variable.

Page 29: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 29/340

Handling Variables:-

Variables can be used in bc mode and values can be assigned tothem.

$bc

X=12 ; y=19

Z=x+y

Z

31

Conditional logics(if) ,loops(for,while), arrays, functions are supported

by ‘bc’ 

Page 30: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 30/340

Square Root of a Number

Syntax : sqrt ( x )

Example :$bc

Sqrt ( 4 )

2

Length of a Number

Syntax : length ( x )

Example :

$bclength ( 1234.5678 )

8

Page 31: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 31/340

Trigonometric Functions

To use trigonometric functions of bc, one have to include mathlibrary. For that , issue the following command from Os prompt:-

$ bc –l

Various trigonometric functions available with „bc‟ are:-

s(x)  sine

c(x)  cosinee(x)  exponential

l(x)  log

Page 32: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 32/340

String handling

To find length of a string:-expr “<string>” : „.*‟ 

Example : $ expr “Unix” : „.*‟ 

4

To extract a substring from a string:-

$ expr “Subhendu” : „… (\..\)‟ 

he # Shows 4th to 5th character

To locate first position of a character in a string:-

$expr “Subhendu” : „[^d]*h‟ 

4 # Shows „h‟ is at 4th position

Page 33: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 33/340

factorFinds out factor of the integer provided

Syntax :  factor <number>

$factor 15

15

3

5$

$factor 18

18

2

3

3

$

Page 34: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 34/340

primes

Shows all prime numbers between integers <lower value> and <upper

value>. If upper value is not provided, it is considered to be2,147,483,647.

Syntax : primes <lower value> <upper value>

$primes 0 10

2

3

5

7

$

Page 35: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 35/340

units

Converts quantities expressed in various standard scales to their

equivalents in other scales. Acts interactively as follows:-

System Prompt User Response

You have: inch

You want: cm

The system responds with two factors; one used if multiplying(preceded by *), the other if dividing (preceded by /):

* 2.540000e+00

/ 3.937008e-01For a complete list of units, examine the file:

/usr/share/lib/unittab

Page 36: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 36/340

tputControls screen display

Options Significancetput clear Clears the screen

tput cup <r> <c> Moves cursor to row <r> and column <c>

tput bold Bold display

tput blink Blink display

tput rev Reverse display

tput cols Shows number of columns in the screen

tput bel Echo bell character

tput lines Shows number of lines in the screentput smso Starts reverse display

tput rmso Ends reverse display

Page 37: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 37/340

Examples

Page 38: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 38/340

time

Times a command.

Command <command> is executed and time prints:-

Elapsed time during the command

Time spent in the system

Time spent executing the command

Syntax : time <command>

Page 39: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 39/340

Example:-

$ time grep -i "Subhendu" *

ABCDEF:subhendu

bounce_off.ksh:# Developed by : SUBHENDU MAJUMDAR

drawbox.ksh:# Developed by : SUBHENDU MAJUMDAR

header:# Developer : SUBHENDU MAJUMDAR

heading:# Developer : SUBHENDU MAJUMDAR

real 6.6

user 0.8

sys 0.7

Page 40: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 40/340

Handling Files 

This section will introduce you with

Architecture

Types of Files

Inode

File System

„pwd‟-Current Directory

„cd‟ –  Change Directory

„ls‟ –  List contents of a dir

Cat –  Create,View,append Files

VI –  Visual Editor

„more‟ –  display files

„pg‟ –  View files

„cp‟,‟mv‟ –  

Copy and rename files

„rm‟ –  Remove Files

„wc‟ –  Count word,lineand characters

„file‟ –  know file type

„chmod‟ –  Changepermissions

„chown‟ –  Changeowner

„chgrp‟ –  Change group

„touch‟ –  Change time

stamp of a file

„ln‟ –  Link a file to other

Page 41: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 41/340

Handling Files…contd 

„mkdir‟ –  CreateDirectory

„rmdir‟ –  Remove

Directory

„cmp‟ –  comparetwo files

„comm‟ –  Compare

two files

„sdiff‟ –  find differencesbetween two files

„dircmp‟Compare Directories

„lp‟ Print a file in a printer

„lpstat‟ View printer status

„cancel‟ Cancel print jobs

„pr‟ Format file contents

Compressing and

Uncompressing Files

Page 42: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 42/340

File maintenance architecture

Formatted Disk

Partition Partition Partition

File system

Directory Directory Directory Directory

Directory Directory

File File File

Page 43: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 43/340

Types of Files

Normal files 

Can be text/binary files.

Can be a text file, compiled source code , executables

Directory files

Contains no external data, but details of files and sub-directories itcontains.

Device files

Printers, tapes, floppy drives, CD ROMs, hard disks, terminals – all

are considered as files

Page 44: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 44/340

Inode

•Inode is a fixed format structure containing theattributes of the files stored in the file system.

•Every file has one inode, and a list of such inodesis kept in a disk area not directly accessible byuser.

•Each inode is accessed by inode number, specifyingthe position of the inode in the list.

Page 45: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 45/340

Some of the important information that inode contains are:-

Information SignificanceMode Permission mask and type of file

Link count No. of links associated with the file

User id ID of the owner

Group Id ID of the group

Size No. of bytes in the file

Access time Time of last access of the file

Mod time Time of last modification of the fileInode time Time at which the inode structure was last

modified

Page 46: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 46/340

Typical File System

/ (root)

Unix bin dev usr tmp etc

usr1 usr2 usr3  bin

/Unix folder contains the kernel

/bin contains binary executable files

/dev  contains device related files/usr is the home directory for allusers

/usr/usr1 is the home dir. for user :

usr1

/tmp contains temporary files

/usr/bin contains additional

binary unix commands/etc contains binaryexecutable files for systemadministration

Page 47: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 47/340

pwd

Shows the directory where the user is currently in 

Syntax :  pwd

Example$ pwd

/home/ems2000

Page 48: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 48/340

cd

Navigates from the current directory to another directory 

Syntax : cd <new directory specification>

Commands Significance

cd scriptscd ./scripts

Moves to directory : scripts undercurrent working directory

cd ../program Change to directory programresiding in the current directory'sparent directory

cd /usr/fin/subhendu/ manfiles Change to the directory whoseabsolute pathname is :/usr/fin/subhendu/ manfiles

cd

cd ~

Move to home directory of the user

Page 49: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 49/340

Commands Significance

cd .. Move to the parent directory of the currentworking directory

cd ~/subhendu Move to folder : subhendu under the homedirectory for the user

d th

Page 50: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 50/340

cdpath

This is an environmental variable which specifies the list ofdirectories to be searched when an user issues a „cd‟ command. 

$CDPATH=.:..:$HOME

This means, when a „cd‟ command is issued, search for the newdirectory first in current working directory(.)

If not found, move to the parent directory of the current directoryand search there.

If, still, not found, search for the directory under user‟s homedirectory

Page 51: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 51/340

ls

Shows the contents of a directory/existence of specific files with

their attributes

Syntax : ls [ flag] [string for filename]

Commands Significance

ls Shows the name of all the files and directories under thecurrent directory, excluding those starting with .

ls –a Lists all files including those starting with „.‟ 

ls –x Multi columnar output

ls –R Shows all files and recursive listing of all files in sub-

directoriesls –l Long listing showing seven attributes of a file

ls –F Marks executables with „*‟ and directories with „/‟ 

Commands Significance

Page 52: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 52/340

Commands Significance

ls –t Sorts files by modification time – the file modifiedmost recently comes at the top

ls –ut Sorts files by access time

ls –r Sorts file in reverse order

ls –ltr Shows long listing of files with their attributes, sortedin reverse order by access time(most recently editedfile comes last in the list)

ls –i Shows inode number of a file

ls *.ksh Shows the name of all files with „.ksh‟ at the end oftheir name

ls [aeiou]* Shows the files with name starting with vowels

ls d*.sh Lists all files starting with „d‟ and ending with „.sh‟ intheir name

ls d?l* Lists all files with first letter as „d‟ and third letter as„l‟ 

ls [!aeiou]* Shows the files with names not starting with vowels

Page 53: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 53/340

Page 54: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 54/340

Page 55: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 55/340

Page 56: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 56/340

cat

Creates , shows, concatenates,copies files 

Commands Significance

cat >file1 Creates file file1 where a user enters text and presses<Ctrl-D> to end text editing

cat >>file1 Append lines to existing content of file : file1 and isended when <Ctrl-D> is pressed

cat file1 Shows the contents of the file: file1

catfile1>file2

Copies the contents of file : file1 into new or existing file: file2

cat file1file2 > file3

Concatenates the content of file1 and file2 and places itinto new or existing file file3

cat file1>>file2

Appends the contents of file1 after the last line of file2.If file2 does not exist, new file is created

Page 57: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 57/340

Commands Significance

cat –n file1 Displays the contents of file : file1 with linenumber

cat –b file1 Displays the contents of file : file1 with linenumber for all lines excepting the blank lines

cat –e file1 Prints „$‟ at the end of each line 

cat –r file1 Replaces multiple consecutive empty lines withone empty line

cat –t file1 Prints tab character as „^I‟ and form feedcharacter as „^L‟ 

cat –s file1 file2 >file3 Suppresses error and does the job. If filefile2 does not exist, the command will copy

the contents of file1 into file3

Examples

Page 58: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 58/340

Examples

Examples…continued

Page 59: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 59/340

Examples…continued 

Examples…continued

Page 60: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 60/340

Examples…continued 

Page 61: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 61/340

VIFirst Unix Full screen Editor

Page 62: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 62/340

Haters say that it is the worst thing ever happened inthe Unix world.

Lovers are totally biased towards it and would go anylength supporting its cause.

First full screen editor, developed by William(Bill)Joy, a graduate student from University of

California, Berkley.

Divides Unix users into two camps:

•Those who hate vi

•Those who love vi

Page 63: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 63/340

Modes of Operation

First session with vi

Append mode

Command mode

Ex mode

Environmental variables

.exrc & EXINIT variable

view

Page 64: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 64/340

Mode of Operation

Command mode : Default mode when a file is opened usingvi. All the keys pressed by the user are interpreted as usercommands

Append Mode : Permits insertion of new text, editing

existing texts.

Ex mode : Permits commands at the command line(last line ofthe screen)

Page 65: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 65/340

Command Mode

Append Mode Ex Mode

R,R,I,I,c,C,o,

O,s,S,a,A

Esc:Enter

Page 66: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 66/340

First Session with vi

C t fil b t pi th f ll i

Page 67: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 67/340

Step 1 : Create a new file by typing the followingcommand from the OS Prompt : vi newfile

•vi clears the screen and displaya window.

•The „_‟ on the top line indicatesthat the cursor is waiting for

commands

•Every other line starts with „~‟,symbol for empty line.

Page 68: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 68/340

Step 2 : Press „i‟ to enter into Append mode. Add text

to the file 

Step 3 : Press <Esc> key to return to command mode 

Page 69: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 69/340

Step 3 : Press „:‟. The cursor takes to the „ex‟ mode

at the command line. Enter „wq‟ and press enter. 

Append Mode

Page 70: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 70/340

Append Mode

Cmd Significance

i Appends text from the left of the current cursor position

I Appends text at the start of the current line.

a Appends text from the right of the current cursor position

A Appends text at the end of the current line.

o Opens a line immediately below the current line in input mode

O Opens a line immediately before the current line in input mode

Inserting Texts

Changing Texts

Page 71: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 71/340

Cmd Significance

<n>r Replaces <n> characters from current cursor posn. with inserted text

R Replaces text from cursor to right<n>s Replaces <n> characters from cursor with entered text

<n>S Replaces <n> lines from the current cursor line with entered text

c0 Changes from cursor to beginning of line with the text entered

c$ Changes from cursor to end of line with the text entered

C Change from current cursor posn. to end of line with the text entered

<n>cw Changes <n> words from the current cursor position with text entered

<n>cc Replaces <n> lines from the current cursor line with entered text

cG Changes from current cursor position to end of the file with enteredtext.

Command Mode

Page 72: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 72/340

Cmd Significance

ZZ Saves the work done in the file and quits editing by vi editor

Saving work in a file and quit

Deleting texts/lines

Cmd Significance

<n>x Deletes <n> characters from current cursor position

<n>dd or <n>D Deletes <n> lines counting from current cursor line to belowd$ Deletes from current cursor position to end of line

dG Deletes from current cursot position to end of file

d<n>G Deletes from current line to line no <n>

df<char> Deletes from current cursor position to first occurrence ofcharacter <char>

d/<pattern> Deletes from cursor upto the first occurrence of string<pattern> in forward direction

d?<pattern> Deletes from cursor upto the first occurrence of string<pattern> in backward direction

Moving/Copying Texts

Page 73: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 73/340

Cmd Significance

<n>yy or<n>Y

Yank <n> lines starting from current line onwards into undobuffer

<n>yw Yank <n> words starting from current cursor position onwardsinto undo buffer

 y$ Yank from current cursor position to end of the line in undobuffer

 yG Yank from current cursor position to end of the file in undobuffer 

“a<n>yy  Yank <n> lines starting from current line onwards into buffernamed a

p Paste the contents of undo buffer( as a result of deleting or yanking) after the cursor position

P Paste the contents of undo buffer( as a result of deleting or yanking) before the cursor position

“ap  Paste the contents of buffer a after the cursor position

“bd  Delete text into named buffer b

Navigation in same line

C d Si ifi

Page 74: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 74/340

Cmd Significance

<n>h Moves cursor left to nth previous character w.r.t. the current cursorposition

<n>l Moves cursor right to nth next character w.r.t. the current cursorposition

<n>b Moves cursor left to start of nth previous word w.r.t the currentcursor position. Punctuation marks are taken into account.

<n>w Moves cursor right to start of nth next word w.r.t the current

cursor position. Punctuation marks are taken into account.<n>e Moves cursor right to end of nth next word w.r.t the current cursorposition. Punctuation marks are taken into account.

f<ch> Move the character to the next character <ch> on same line

F<ch> Move the character to the prv. character <ch> on same line

t<ch> Move the character to one column before the next character <ch> onsame line

T<ch> Move the character to one column after the next character <ch> onsame line

; Repeats search in the same direction along which the prv. Search was

made using f/t

Page 75: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 75/340

Cmd Significance

, Repeats search in the opposite direction along which the prv. Search

was made using f/t

<n>| Moves the cursor to specified column <n>

0 or ( Moves to 1st character of the current line

$ or ) Moves to last character of current line

^ Moves to 1st non-space character of the line<n>B Moves cursor left to start of nth previous word w.r.t the current

cursor position.Punctuation marks are ignored

<n>W Moves cursor right to start of nth next word w.r.t the currentcursor position. Punctuation marks are ignored

<n>E Moves cursor right to end of nth next word w.r.t the current cursorposition. Punctuation marks are ignored

Navigation across lines

Page 76: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 76/340

Navigation across lines

Cmd Significance

<n>j or<n>^n Move the cursor down to the <n>th next line in the same column

<n>k or<n>^p

Move the cursor up to the <n>th prv line in the same column

H Moves the cursor to the top line of the screen

L Moves the cursor to the last line of the screenM Moves the cursor to the middle line of the screen

<n>G Moves to line number <n>

+ Moves the cursor to next line‟s first non-blank character

- Moves the cursor to previous line‟s first non-blank character

Redraw screen

Page 77: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 77/340

Redraw screen

Cmd Significance

z- Makes the current line the last line of the screenand redrawsthe screen

z+ Makes the current line the first line of the screenand redrawsthe screen

z. Makes the current line the middle line of the screenand redrawsthe screen

Ctrl-l Redraws the screen

/pattern/z- Find the next occurrence of <pattern> and make that last line ofthe screen

Scrolling across pages

Page 78: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 78/340

Scrolling across pages

Cmd Significance

<n>^f Move forward by <n> screens

<n>^b Move backward by <n> screens

<n>^d Move forward by <n> number of half screens

<n>^u Move backward by <n> number of half screens

<n>^e Scroll window down by <n> lines<n>^y Scroll window up by <n> lines

Pattern searching

Page 79: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 79/340

Cmd Significance

/pattern Searches for specified <pattern> forward. IF end of file is

reached, search wraps around.?pattern Searches for specified <pattern> backward.

n Repeat the last search in the same direction as wasspecified in the last search

N Repeat the last search in the opposite direction as was

specified in the last search/pattern/+<n> Positions the cursor <n> number of lines after the line where

the specified <pattern> is found

/pattern/-<n> Positions the cursor <n> number of lines before the linewhere the specified <pattern> is found

% Find the matching braces or parenthesis

Joining lines

Page 80: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 80/340

Cmd Significance

<n>J Joins current line and <n> lines below it together to form a

single line

Undo changes

Cmd Significance

u Undo last changeU Undo all the changes in the current line

Marking text

Cmd Significance

m<char> Marks position of the file with mark <char>

„<char>  Moves to portion of the file marked with <char>

“  Toggle to most recently marked location

Restoring previously deleted line

Page 81: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 81/340

Cmd Significance

“<n>p  Paste the content of <n>th last delete ( n<=9)

“1pu.u.u…  Till the last change is found

Filtering texts

Cmd Significance

!<n>G sort Sort from current line to line no. <n>!<n>G tr „[a-z]‟ „[A-Z]‟  Translates all the characters from current line to

line <n> to uppercase

!! tr „[a-z]‟ „[A-Z]‟  Translates all the characters of current line touppercase

Cmd Significance

<n>i<ch> Inserts <ch> character <n> number of times in input mode ata stretch

Repeat factor

Miscellaneous in command mode

Page 82: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 82/340

Cmd Significance

~ Change the character under cursor from lowercase to

uppercase and vice versa. Repeat the last change

<n>. Repeat the last action „n‟ times 

<< Shift current line to shift width character left

>> Shift current line to shift width character right

Options available with vi command

Page 83: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 83/340

Cmd Significance

vi –r <filename> Recover the file <filename> as much as possible after

system crash and open itvi –R <filename> Open the file <filename> in read-only mode

vi +<n> <filename> Opens the file <filename> with cursor positioned inline number <n>

vi + <filename> Opens the file <filename> with cursor at the last line

vi –w<n> <filename> Opens file <filename> in vi mode with window size of<n> number of lines

vi +/<pattern><filename>

Opens file <filename> in vi editor and places thecursor at first occurrence of pattern <pattern>

vi –x <filename> Opens encrypted file <filename> in vi mode and asksfor the password before opening that

Ex Mode

Page 84: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 84/340

Saving work in a file 

Cmd Significance

:w Save the changes made to the file:w <filename> Same as “Save As..” in windows. Saves the

contents to the specified file <filename> . If itdoes not exist previously, a new file is created

:w! <filename> Save the changes to file <filename>, if the file

already exists:w >> <file1> Append the contents of the opened file after the

last line of the file <file1>. File <file1> shouldexist previously

:<n1>,<n2>w <newfile> Copies the contents of lines <n1> to <n2> into anew file <newfile>

:<n1>,<n2>w! <newfile> Moves the contents of lines <n1> to <n2> into anexisting file <newfile> , overwriting its previouscontents

Cmd Significance

Page 85: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 85/340

Cmd Significance

:.,.+<n>w <newfile> Appends from current line to <n> number oflines below it into file <newfile>

:.,.+<n>w >> <nextfile> Appends from current line to <n> number of linesbelow it after the last line of the file <nextfile>

:q Quits the file editing in vi, provided no unsavedchange remains

:q! Quits vi neglecting all the unsaved changes made to

the file:wq or :x Save the unsaved changes in the opened file and quit

vi editor

Temporary exit to shell

Cmd Significance

:sh Temporarily allows the user to come out of the vi file anduse the shell. After the job of the user is done andcommand : exit is triggered from OS prompt, controlreturns to vi editor again

Navigating to desired line

Page 86: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 86/340

g g

Cmd Significance

:<n> Custor moves to line number <n>

Search and replace texts in ex mode

Syntax :- :<line address>s/<old pattern>/<new pattern>/g

Line address Significance

% All lines where matching pattern is found

. Current line

<n1>,<n2> Refers from line <n1> to <n2>$ Last line

1,$ First to last line

.,.+<n> From current cursor line to <n> number of lines downwards

Examples of Search and Replace

Page 87: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 87/340

Example Significance

:%s/ex/vi/c Substitutes 1st occurrence of string „ex‟ with „vi‟by showing them and asking for confirmation.When each string will be shown with pause incursor, press „y‟ for substitution 

:%s /<amaze \ >\/delight/g Replaces „amaze‟ , where available as a full word,with „delight‟. Note, any word like „amazed‟ will

not be replaced:g/subhendu/s/majumdar/mazumder/g

Replaces every occurrence of string „majumdar‟with „mazumder‟ on all lines containing thepattern „subhendu‟ 

:g/.\ {9\ }9/s/0/*/g Replaces „0‟ with „*‟ at all lines having „9‟ after 9th 

position:g/^$/d Delete all blank lines

:g!/complete/s/$/To bedone/

Append the string „To be done‟ at the end of alllines not containing the string „complete‟ 

Example Significance

Page 88: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 88/340

:g/vi/s/^/editor/ Append the string „editor‟ at the first of alllines containing the string „vi‟ 

:%s/$/ : see my note/g Appends the string „: see my note‟ at the end ofall lines

:g/^….$/d Deletes all lines containing 4 letters

:g/^..o/d Deletes all lines with „o‟ as 3rd character

:%s/…$//g Delete the last three character of every line

Reading below the current line in the vi editor

Command Significance

:r <nextfile> Reads the contents of the file <nextfile> belowcurrent line

:r! <command> Places the output of the command <command>below the current line

Editing another file

Page 89: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 89/340

Command Significance

:e <nextfile> Stops editing the current file; leaves the current file

and starts editing file <nextfile>; provided there areno unsaved changes in the current file

:e! <nextfile> Edits file <nextfile> abandoning all the changes doneto the current file

:e! Loads last saved version of current file

:n Edits next file mentioned in the vi queue:rew Edits first file in the command line

:e +<n> <nextfile> Edit starts at line <n> of file <nextfile>

Abbreviating texts

Page 90: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 90/340

Command Significance

:ab <short_string>

<long_string>

When the user writes the string <short_string> in

input mode, the <long_string> is written

Mapping

Command Significance

:map g :w^M Pressing „g‟, one wants to save the file(:w is for saving,

and ^M is for pressing Enter key. While writing it inthe command line, write ^V^M)

:map z i^M^[ When you position your cursor to any character in aline and press „z‟ , the line will be broken from thatpoint and two lines will be formed. The control willremain in command mode

(^[ represents <Escape> key)

:map z :w^M: %^M Pressing „z‟ in command mode saves the file andexecutes it in one shot

To unmap a key, write at the command line :unmap <key>

Miscellaneous

Page 91: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 91/340

Command Significance

:! <command> Executes the command <command> remaining in vi

editor:f Shows the name of the current file and line

^g Same as :f

Setting environmental variables for vi

Page 92: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 92/340

g

Works in ex mode.To set an environment variable to customize vi, thefollowing syntax needs to be followed:-

:set <env.variable> [= <value>]

Environment Significance

Page 93: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 93/340

Environment

variables

Significance

autoindent(ai) Newly inserted lines of text are indented to the same

distance from left margin as the preceding line. Opposite ofthis option is noautoindent(noai)

autowrite(aw) Automatically saves the unsaved changes in a file beforeopening the next file with :n or using a shell command with :!<command>. The opposite to this option is noautowrite(noaw)

errorbells(eb) Sounds the bell when error occurs. Opposite is „noeb‟ 

exrc(ex) Allows an .exrc file in the current directory to override the.exrc file in user‟s home directory. Opposite is „noex‟ 

list Displays special characters in the screen: tabs are shown as^I, end of line are marked with „$‟. Opposite is „nolist‟ 

mesg System messages allowed when vi is running. Opposite is„nomesg‟ 

number( nu ) Displays line numbers. Opposite is „nonu‟ 

Environment variables Significance

Page 94: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 94/340

Environment variables Significance

report=<val> When any operation affects more lines than thissettings, message is displayed

scroll=<val> Number of screen lines to scrollshiftwidth(sw)=<val> Number of spaces to be used for backtabs/<</>>

showmatch(sm) Shows match for ) or } . Opposite is „nosm‟ 

showmode Indicates type of mode

tabstop=<val> No. of spaces the tab character moves over

ignorecase(ic) Ignores case when searches patterns. Opposite is„noic‟ 

wrapmargin(wrm)=<val> When set to a value >0 , carriage returns areinserted automatically when the cursor gets to

within that number of spaces from the right edgeof the screen

Page 95: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 95/340

.exrc file and EXINIT system variable

One can store all the values for environment variables, all the keymappings and all the abbreviations in a file „.exrc‟ under the homedirectory for the user.VI looks for this file on startup andexecutes the instructions as ex mode commands.

Besides, there is also a system variable , „EXINIT‟ which can alsobe used to save the settings.

EXINIT=“set report=5 ignorecase ai”; export EXINIT  

view

Page 96: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 96/340

Description :Shows the file <filename> in vi mode. File remainsread-only. No changes done to the file cannot be saved. 

Syntax : view <filename>

more

Page 97: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 97/340

Filter for examining continuous text, one screenful at a time. Itpauses after each screenful,printing the filename at the bottom ofthe screen.

•To display one or more next lines, press <Return>

•To display another screenful, press <Space> 

Syntax : more [ -n <number> ] [ -<number>] [ -d] [-i] [-e] [ -c] [-f][-s] [ +/pattern] [filename]

Command Significance

Page 98: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 98/340

Co a d

line option

S g ca ce

-n <number> Sets the no of lines in the display window to that <number>.

-<n> Same as above

-d Prompts user with message :- „Press space to continue, q toquit, h for help‟ at the end of each screenful of display 

-i Performs case independent pattern matching

-c Draw each page by starting at the top of screen, and eraseeach line before drawing on it

-s Squeeze multiple blank lines from the file , showing only oneblank line

+/pattern Start listing such that the current position isset to two

lines above the line matching the regular expressionpattern.

Internal commands in „more‟ mode

Page 99: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 99/340

Options Significance

f Scrolls forward one screen

b Scrolls backward one screen

q Quit

/pattern Searches pattern <pattern> forward

?pattern Searches pattern <pattern> backward

v Opens vi mode for the file viewedn or N Repeats last search

<n>j Scrolls forward by <n> number of lines

<n>G Moves to line number <n>

G Goes to last line of the file. Repeats prv. command

:n Moves to next file specified in the command line

Options Significance

:p Moves to previous file specified in the command line

Page 100: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 100/340

:p Moves to previous file specified in the command line

i<space> Scrolls forward by i number of screens

i<return> Scrolls forward by i number of lines

<i>^d/ <I> Scrolls forward i lines, with a default of ½ of the screen size

<i>^u/<i>u

Scrolls backward i lines, with a default of ½ of the screen size

<n>k/<n>^y

Scrolls backward by <n> number of lines

<n>z Displays <n> more lines and sets the window size to <n>

<n>f /<n>^f

Moves forward by <n> lines

<n>b /

<n>^b

Moves backward by <n> lines

^g Writes the name of the file currently being examined, the no.relative to the total number of files to be viewed, the currentline no,current byte no, total no of bytes to write and what % ofthe file precedes the current position

Options Significance

h Display a descriptive list of all the more commands

Page 101: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 101/340

h Display a descriptive list of all the more commands

!<command> Invoke a shell with a command <command>

:e <newfile> Leaves the current file and starts viewing a new file

<newfile><I>:n Examines the <I>th next file specified in the command

line

r / ^l Refreshes the screen

. Repeats the prv command

m<char> Marks the current position with letter <char>

„<char>  Returns to the position previously marked by thespecified letter <ch>

pg

Page 102: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 102/340

Displays the content of a file one page at a time 

Syntax :pg < flag and string> <filename(s) Flag Significance

-c Clear the screen at the end of each page of display and

start the display at the top of the screen-e Continues to the next file after the end of one file, if more

than one files are specified at the command line

-f Truncate lines longer than the width of the screen display

-p <string> Display the <string> at the pg command prompt.Default is „:‟ .

If string is %d the pageno is displayed atr the prompt-s Highlights all messages and prompts issued by the pg

command

+<n> Start the display from line no <n> of the file specified

Flag Significance

<n> Sets the size of the display screen to <n> number of lines

Page 103: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 103/340

-<n> Sets the size of the display screen to <n> number of lines

+/pattern/ Search for pattern <pattern> in the file and start thedisplay at that line

Keystrokes in pg mode

Option Significance

-<n> Go backward by <n> number of pages+<n> Go forward by <n> number of pages

l Go forward by one line

<n>l Start the display in the file at line specified by <n>

+<n>l Go forward by <n> lines

-<n>l Go backward by <n> lines

d Go forward by ½ screen

-d Go backward by ½ screen

^l Redraws the screen

Option Significance

Page 104: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 104/340

p g

$ Move to last page of the file

<n>/<pattern> Searches forward for the pattern <pattern> in the file

from beginning of the next page. If a number <n> isspecified, pg searches for the specified occurrencenumber <n> of the <pattern>

<n>p Starts showing the <n>th previous file.

n Start showing the next file

q Quits the pg command

cp

Page 105: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 105/340

Copies one file to another file/one directory to anotherdirectory/files into directories 

Syntax :cp [-flag] [ old files/directories] [ new files/directories] Flag Significance

-i Interactive copying : prompt appears for user accentbefore copying

-f Force copying

-p Preserve permissions. Preserves modificationtime,access time,file mode, user id, user group etc

-r Recursive copying

Examples

Page 106: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 106/340

Example Significance

cp file1 file2 Copies the contents of file1 into new or existing file

file2cp file1 file2 file3dir1

Copies files: file1, file2 and file3 into directory dir1

cp –R dir1 dir2 Copies directory dir1 into a new directory dir2(if dir2does not exist before) / copies the directory dir1 as a

sub-directory under dir2(if directory dir2 existsbefore)

cp –R dir1 dir2dir3

If dir3 exists, two sub-directories under it arecreated : dir1 and dir2.

IF dir2 directory does not exist, then a new directorydir2 is created with the contents same as dir1 and oneadditional sub-directory dir2

mv

Page 107: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 107/340

Moves/renames:-A file to new or existing file

One/more files to existing directory

One or more directories to a new or existing directory.

If the access permission of the destination directory or existingdestination file forbids writing, mv command asks for overwritingthe file 

Syntax :mv [-flag] [ old files/directories] [ new files/directories] 

Flag Significance

-f Performs move operation without prompting for permission

-i Interactive moving

rm

Page 108: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 108/340

Removes the entries for one/more files from a directory.

Destroys the file whose last link is deleted

Removal of a file requires write permission for that file

Removal of a file from a directory required write and executepermission in the directory 

Syntax :rm [-f|-i] [ -r|-R] [files|directories] 

Flag Significance

-f Forceful deletion

-i Interactive deletion-r or –R Recursively delete the entire contents of the cirectory

before removing the directory itself

Examples

Page 109: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 109/340

Example Significance

mv *.txt../testdir Moves all the files with .txt extension under thedirectory testdir residing under the parentdirectory of the current directory

rm *.temp Remove all files with extension : .tmp under thecurrent directory

rm –r backupdir Remove the directory backupdir with all itscontents

wc

Page 110: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 110/340

Counts the number of words/bytes/characters/lines in a file 

Syntax :wc [-c|-l|-w] <filename(s)> 

Flag Significance

-w Counts the total no. of words

-l Counts the total no. of lines

-c Counts the total no. of characters

$ wc – c file1

32 file1

$ wc – l file1

2 file1

$ wc – w file1

8 file1

Examples

file

Page 111: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 111/340

Determines the file type of a file or list of files.

Performs a series of tests on each file in an attempt to classify it.If file appears to be an ASCII file, file examines the first512bytes and tries to guess its language.

File /etc/magic is ued to identify files that have some sort ofmagic number, that is, any file containing a numeric or stringconstant that indicates its type. Commentary at the beginning of/etc/magic explains the format.

Syntax : file [-f ffile] [-h] file ...

chmod

Page 112: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 112/340

A file can have three type of permissions:-

Read : Authorized user can read the contents of the file.

Write : Along with read permission, it allows the allowed user tomodify its contents.

Execute : If the file is an executable, any allowed user can executeit

Syntax : chmod [file permission] <files|directories>

A file can be accessed by:-

Page 113: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 113/340

User : Person creating the file. He grants all the authorizations tothe file

Group : Group user for the file

Others: All other users not belonging to the group or are not thecreator.

Value Significance

4 Read permission

2 Write permission

1 Execute permission6(=4+2) Read and write permission

7(=4+2+1) Read,write and execute permission

5(=4+1) Read and execute permission

Numeric representation of permissions

Example of granting numeric authorization

Page 114: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 114/340

Syntax: chmod <val1><val2><val3> <filename(s)|directory name(s)>

val1 is for usersval2 is for group

val3 is for others

Any permission on a directory percolate down to the files and sub-

directories under it.

Example Significance

chmod 744 file1 Grant all permissions to : User, and read permissionto group and others

chmod 776 Grant all permission to user and group, read andwrite permission to others

chmod 777 file1 Grant all permission to all

Alphabetic representation of permissions

Page 115: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 115/340

Value Significance

r Read permission

w Write permission

x Execute permission

Value Significance

u User creating the file

g Other users in the same group of the creator

o Any other users

a All(creator, other users in the same group, and other users)

Example of granting alphabetic authorization

Page 116: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 116/340

Example Significance

chmod u=rwx,go=r file1 Assign read,write and execute permission touser, but only read permission to group andothers

chmod o-x file1 Revoke execute permission from others

chmod a+x file Assign execute permission to everybody

chmod –R ug+r,o-r,a+x/home/ems2000 Traverse the directory subtree underdirectory /home/ems2000 making all regularfiles readable by user and group only, revokeread permission from others and grantexecute permission to all

Chown and chgrp

Page 117: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 117/340

chown changes owner of the file and chgrp changes group of eachfile specified.

To change user or groyp, one must own the file and have theCHOWN privilege granted to him by the system administrator

Syntax : chown [-h] [-R] [owner] [file…]

chgrp [-h] [-R] [group] [file…]

Options Significance

-h Change owner/group of a symbolic link-R Recursively change the owner and group of all the files and

sub-directories under the directory named

E l Si ifi

Page 118: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 118/340

Example Significance

chown ems2000auto.profile

User „ems2000‟ becomes the owner of thefile auto.profile

chgrp ems2000 auto.login User „ems2000‟ becomes the group user toaccess the file auto.login

chown –R ems2000: usersshell_scripts

The command searches the directory :shell_scripts and changes each file in thatdirectory to owner : ems2000 and group :users

touch

Page 119: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 119/340

Updates access time/modification time of file(s) 

Syntax : touch [-a|m|c] [ -r <ref-file> | -t <time> ] <filename(s)>

Flag Significance

-a Change the access time of the file to the time specified/ ifno time is specified, use the current time

-m Change the modification time of the file to the timespecified/ if no time is specified, use the current time.

-r ref-file Use the corresponding time of file ref-file to change themodification/access time of the file

-t <time> Use the specified time <time> instead of current time. Thetime format is : <YYYY><MM><DD><hh><mm>.<ss>

Example Significance

touch – a wot.ksh Changes the access time of wot.ksh with the

Page 120: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 120/340

gcurrent date and time

(Changes can be perceived if the command : ls –ut is

fired in the directory containing the file : the filewot.ksh will come at the top of the list)

touch – m wot.ksh Changes the modification time of wot.ksh withthe current date and time

(Changes can be perceived if the command : ls –lt isfired in the directory containing the file : the file

wot.ksh will come at the top of the list)touch -m -t

201012122300 new.del

Changes the modification time of file : new.delto Dec 12,2010 time : 23:00

Seeing the attributes of the file new.del willreveal the information:-

-rw-rw-r-- 1 ems2000 dba 10 Dec 12 2010 new.del

touch -m -r new.del new1 Change the modification time of file : new1 andmake it same as the modification time of the file: new.del

ln

Page 121: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 121/340

Description Link files and directories 

Command Significance

ln [-f|-i] file1 newfile Links file : file1 to a new or existing file : file1

ln [-f|-i] file1 file2

testdir

Creates link for files new1 and new2 under thedirectory : testdir with two new files/existingfiles new1 and new2

ln [-f|-i] dir1 dir2

testdir

Creates link for directories dir1 and dir2 underthe directory : testdir with two new /existingdirectories : dir1 and dir2

Flag Significance

-f Force esisting files or directories to be removed to allow the link

-i Interactive linking

mkdir

Page 122: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 122/340

Creates directories 

Flag Significance

-p Intermediate directories are created asnecessary.Otherwise, the full path prefix of dirnamemust already exist. mkdir requires write permissionin the parent directory.

-m <mode> Mode of permission for the directory and all thefiles under it

Syntax : mkdir [-p] [-m <mode>] <directory name(s)>

Example Significance

Mkdir m755 testdir Directory : testdir is created with

Page 123: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 123/340

Mkdir –m755 testdir Directory : testdir is created withpermission = 755

mkdir –ptestdir/subhendu If testdir is not created, it is firstcreated. Then, a directory : subhendu iscreated under the directory testdir

rmdir

Page 124: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 124/340

Removes the directory entry for each empty directory referred 

Flag Significance

-f Forcefully remove a directory, even though it is not empty

-i Interactive removal of empty directory

-p Path removal.If, after, removing a directory with more than onepathname component, the parent directory of that directory isempty, rmdir removes the parent directory also. This continues tillrmdir encounters non-empty parent directory

Syntax : rmdir [-f|-I|-p] <directory>

cmp

Page 125: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 125/340

Description Compares two files 

Flag Significance

-l Print the byte number(decimal) and differing

byte(octal) for each difference-s Prints nothing; return exit codes only

Syntax : cmp [-l|-s] <file1> <file2>

Return codes Significance

0Files identical

1 Files not identical

2 Inaccessible/missing arguments

comm

Page 126: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 126/340

Description Select or reject lines common to both files. 

Syntax : comm [-[123]] <file1> <file2>

Case Significance

comm filenew fileold Produces a three columnar output:-

1st

 column displays lines only in file: filenew2nd column displays lines only in file:fileold

3rd column displays lines common to both files

comm – 1 filenew fileold Suppress display of 1st column

comm – 2 filenew fileold Suppress display of 2nd column

comm – 3 filenew fileold Suppress display of 3rd column

comm – 12 filenew fileold Shows only lines common to both files

sdiff

Page 127: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 127/340

Description Side by side file difference program. 

Syntax : sdiff [-l|-s] <file1> <file2> 

$cat file1

Suvendu

Subhasish

Dibyendu

Diptoman

Akash

$cat file2

Subhendu

Subhasish

Arka

Dibyendu

diptoman

$sdiff file1 file2

Suvendu Subhendu

Subhasish Subhasish

> Arka

Dibyendu Dibyendu

Diptoman Diptoman

Akash <

Flag Significance

-l Only print on the left hand side where columns are identical

-s Do not print identical lines

dircmp

Page 128: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 128/340

Description Examines two directories and generates varioustabulated info about the contents of the directories. 

Syntax : dircmp [ -d|-s|-wn] <directory1> <directory2> 

Flag Significance

-d Also compares the contents of the files with same name in both

directories and output a list telling what must be done to bringthem into agreement

-s Suppresses messages about identical files

$ >ls -ltr dir1 dir2dir1:total 10

Page 129: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 129/340

total 10-rw-rw-r-- 1 ems2000 dba 13 Jul 20 07:23 abc-rw-rw-r-- 1 ems2000 dba 26 Jul 20 07:23 def-rw-rw-r-- 1 ems2000 dba 15 Jul 20 07:30 subhendu-rw-rw-r-- 1 ems2000 dba 28 Aug 13 00:30 subha-rw-rw-r-- 1 ems2000 dba 5 Aug 13 00:39 samefile1

dir2:total 8-rw-rw-r-- 1 ems2000 dba 26 Jul 20 07:24 def-rw-rw-r-- 1 ems2000 dba 12 Aug 13 00:32 nsf-rw-rw-r-- 1 ems2000 dba 30 Aug 13 00:32 abc

-rw-rw-r-- 1 ems2000 dba 5 Aug 13 00:39 samefile1

$ dircmp dir1 dir2Aug 13 00:42 2003 dir1 only and dir2 only Page 1

./subha ./nsf

./subhendu

Aug 13 00:42 2003 Comparison of dir1 dir2 Page 1

directory .different ./abcsame ./defsame ./samefile1

lp

Page 130: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 130/340

Description Prints one/more files to a specified printer 

Syntax : lp -d<printer> [-m] [-n<copies>] [-t<title>] <file(s) 

Flag Significance

-d<printer> Specify the printer <printer> where the print request is to

be directed-m Notify the requesting user at successful completion of the

print request by mail

-n<copies> Specifies number of copies to print

-t<title> Print the specified title <title> on the banner page

lpstat

Page 131: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 131/340

Description Shows current status of the print request 

Syntax : lpstat [ -p<printer>] [-t] [-u<username>] [-v

<printername>] 

Flag Significance

-p<printer> Shows all printing jobs queued at printer <printer>

-t Displays detailed status information about the printfrom all users

-u<username> Displays the status of print requests triggered byuser <username>

-v<printername> Displays a list for the specified printername

cancel

Page 132: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 132/340

Description

Used to cancel a specific print request or cancel all queuedrequests to a specific printer queue. Any ordinary user can cancel

only those jobs triggered by his own userid

Example:-

$ cancel 734 # cancels print job with id 734

$ cancel lipi # cancels all queued jobs in printer lipi

 pr

Description

Page 133: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 133/340

Description Prepares a file for printing by adding suitable

headers, footers and formatted texts. 

Example Significance

pr file1 Adds 5 lines of margin at the top and 5 lines at thebottom of file : file1. Header shows date and time of lastmodification of the file along with filename and pageno

pr – l 72 file1 Changes default page size from 66 to 72

pr +10 file Show file file1 from 10th page

pr – 3 file1 Prints a file in three columns

pr – t file1 Suppresses page headers and footers for file : file1

pr – f file1 Form feed is used for a new page instead of a sequenceof line feed characters

pr – w 80 file1 Set the width of each page to 80 instead of a default of72

Example Significance

Pauses after each page is displayed in the

Page 134: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 134/340

pr – P file1 Pauses after each page is displayed in theterminal

pr – 

o5 file1Indent each line by 5 columns

pr – d file1 Generate output with double specing

pr – h “List of files” 

file1

Print “List of files” instead of the filename asheader on each page.

Compressing files

Page 135: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 135/340

Compress

Compress <filename(s)>

Reduces the size of the named files using adaptive Lempel-zev coding. Ifreduction is possible, each file is replaced by a new file with same namesuffixed sith „.Z‟. Original ownership, modes, access and modification timesare preserved.To uncompress a file, issue the command : uncompress<filename>.Z . To view a compressed file, issue : zcat <filename>.Z

Gzip

gzip <filename(s)>

Reduces the size of the named files . If reduction is possible, each file isreplaced by a new file with same name suffixed sith „.gz‟. Original

ownership, modes, access and modification times are preserved. Touncompress a file, issue the command : gunzip <filename>.gz . To view acompressed file, issue : gzcat <filename>.Z 

Page 136: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 136/340

Filters 

Filters 

By end of this section you will be able know

Page 137: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 137/340

By end of this section , you will be able know

„head‟ First few lines of a file

„tail‟ Last few lines of a file

„cut‟ 

View columns/fields„paste‟ 

 join lines of files

„split‟ Break long lines

„fmt‟ Formats texts

„fold‟ Folds long lines

„Sort‟ Sorts file contents

„tr‟ Translates file contents

„nl‟ 

Shows line numbers„Spell‟ 

Catch spelling mistakes

„find‟ Find files

„grep‟ Search Pattern in a file

„Sed‟ Display specific lines

„awk‟ Reporting Tool

head

Page 138: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 138/340

Gives first few lines of a file 

Example Significance

head – 5 newfile Shows first 5 lines of file : newfilehead – n 5 newfile Same as above

head -l -n 5 newfile Same as above

head -c – n 14 newfile Shows first 14 characters of newfile

Syntax :head [-<n>] [-c] [-l] [ -n <I>] [file(s)] 

tail

Gi l t f li / h t f fil

Page 139: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 139/340

Gives last few lines/characters of a file 

Flag Significance

-n <no> Shows last <no> lines of the file if <no> is specified with no sign or–ve sign.

If specified with a „+‟ sign, it shows from <no>th line onwards tillend of the file

-c <no> Shows last <no> bytes of the file if <no> is specified with no sign or–ve sign.

If specified with a „+‟ sign, it shows from <no>th byte onwards tillend of the file

-f Works in constant pilot mode . Used for variable sized files thoseare growing constantly.

Syntax : tail [-f] [-c number] [file]tail [-f] [-n number] [file]

Example Significance

Page 140: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 140/340

tail – 5 newfile Shows last 5 lines of newfile

tail – n 5 newfile Same as above

tail +5 newfile Shows from 5th line to end of the file

tail – n +5 newfile Same as above

tail -c 5 newfile Shows last 5 bytes of a file

tail – c +5 newfileShows from 5

th

 byte till end of the filetail – fn 3 newfile Constantly displays the last three lines of file : newfile

and leave the tail in “follow” mode. The „$‟ prompt doesnot return even after the work is over. One have toabort the process to exit to the shell.

cut

Syntax :

Page 141: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 141/340

Extracts selected fields/characters on each line of a file 

Syntax : cut [-c list] [file]cut [-f list] [-d <char>] [file]

Example Significance

cut – c 6-22,24-32 file1 Extracts texts from column 6 to 22 and 24 to 32and displays in the terminal in two columns

cut – c – 22 newfile Extracts text from column 1 to 22 from the file :newfile and displays it in the terminal

cut – c – 6, 22,24- file1 Extracts texts from column 1 to 6, column 22 andfrom column 24 to end of the line and display it inthe terminal

cut – d”|”  – f 2,3 newfile Extracts field contents of 2nd and 3rd fields anddisplay in the terminal. Use “|” as a field separatorwhile extracting fields

cut – d”|”  – f 1-5 newfile Using „|‟ as field separator, it extracts fieldcontents of 1st to 5th fields and display in theterminal.

paste

Page 142: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 142/340

Merges same lines of several files or subsequent lines of same file

Syntax : paste file1 file2 ...

paste -d list file1 file2 ...

paste -s [-d list] file1 file2 ...

Example Significance

paste file1 file2 Display in the screen the join of line 1 of file1 withline 1 of file 2 with a space between them, line 2of file1 with line 2 of file2 with a space delimiterand so on.

paste – d”|” file1 file2 Same as above with „|‟ as delimiter between thefields of both files on each line.

Paste – s file1 Joins all the lines of file1 into a single line and showit in the terminal

Example Significance

Page 143: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 143/340

paste -s file1 file2 Displays in the screen a single line which is :-

Join of all the lines in file1 + Join of all the lines in

file2

paste –s -d” t\n\” file1 Combines 1st and 2nd lines into a single line, 3rd and4th into another line and so on.

split

S nta :

Page 144: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 144/340

Splits up a file into equiline smaller lines.Large files are sometime difficult to edit with an editor. The splitcommand breaks up a larger file into several equi line smaller files,each containing a default of 100 lines.

It creates a group of files xaa,xab…till xaz and then again fromxba,xbb.. till xbz. Total 26x 26 = 676 files can be created in thisway.

Syntax : split [-<no. of lines to be put in each file>] [<initials>]

Example:

File: newfile consists of 100 lines.$ split –20 newfile

5 files nfa,nfb,nfc,nfd and nfe will be prepared each containing 20 linesfrom newfile

fmt

Syntax :

Page 145: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 145/340

Simple text formatter that fills and join lines /split lines to produceoutput lines upto the number of characters specified in the –woption(default is 72). It also counts the spaces between words in alineand also considers a space between joining of two lines.

The –s option split lines only. It does not join short lines to form

longer ones.

Syntax : fmt [-s] [-w <width>] [file…]

$ >cat file1

Today, we have a meeting.

It will start at 6 pm.

Bye

$ >fmt -w10 file1

Today, we

have a

meeting.

It will

start at 6

pm. Bye

fold

Syntax :

Page 146: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 146/340

Folds long lines 

Syntax : fold [-b|-s] [-w <width>] <file….>

Flag Significance

-b Width in bytes for counting

-s Break the line on the last blank character found before the specified

number of column position specified in the –w<width> option(default : 80)

$ cat file1

Today, we have a meeting. It will start at 6 pm. Bye

$ fmt -s -w15 file1

Today, we have

a meeting. It

will start at 6

pm. Bye

sort

Sorts the contents of a file.

Page 147: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 147/340

Sorts the contents of a file.

Starts with 1st character on each line and proceeds to the next character

only when the prv. Character in the two lines are identical.Can also work on fields. Default field separator is the space.This can alsobe changed.

Example Significance

sort file1 Shows the sorted contents of file1 as percharacter sorting

Sort –t “|” +2 file1  Sort the contents of file1 based on 3rd field

Sort –t “|” – r +1 file1 Reverse sort on 2nd field

Sort – c file1 Checks whether the file : file1 is already sorted or

not. Value of system variable $? Is 0 if it is sortedsort – t “|” +2 – c file1 Checks whether the file is already sorted on 3rd 

field or not

Example Significance

Sort –o sort.lst +3 file1 Sort the contents of file : file1 based on 4th field

Page 148: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 148/340

Sort   o sort.lst 3 file1 Sort the contents of file file1 based on 4  fieldand store the output in a file : sort.lst

Sort –t “|” +3 – 4 file1 Start sorting of file1 after 3rd

 field(+3) and stopsorting after 4th field(-4). Basically sort file : file1on 4th field

Sort – u file1 Sorts the contents of the file : file1 and removesduplicate entries

Sort –t “|” +7.5 –  11.6file1 Start sort after 5th

(+7.5) column of 8th field(+7.5) and stop sort on 6th column(11.6) of 12th field(11.6)

Sort – n file1 Sort numerically file : file1. Used when the filecontains numeric entries.

tr

Page 149: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 149/340

By default, it translates each character in <expr1> found in the file to itsmapped character in <expr2>

Syntax : tr [-c] [-d] <expr1> <expr2> < <file>

Example Significance

tr „ABC‟ „abc‟ < filenew  Translates the uppercase letters A,B and C infile : filenew to lowercase

tr „[a-z]‟ „[A-Z]‟ <filenew  Translates all the lowercase letters in file :filenew to uppercase

tr –d „|‟ < fileold  Deletes all the „|‟ characters from file : fileold 

tr –s „A‟ <fileold  Compress multiple consecutive presence ofcharacter „A‟ to one „A‟ 

tr –cd „|/‟ < fileold  Delete all characters except „|‟ and „/‟. 

nl

S t

Page 150: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 150/340

Shows line number at the left of each line in the file. Reserves sixcharacters for the number

Syntax : nl [-w < number>] [ -s <delimiter_char>] <filename….>

Example Significance

-w <number> Specifies the width option for line numberdisplay to <number> characters

-s <delimiter_char> Used to specify the character used fordelimiting between line no and line content

spell

Page 151: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 151/340

Lists all spellings in the file (s) the program recognizes as mistakes.

Syntax : spell [-a|-b] <filename(s)>

Example Significance

-a Use American spelling system-b Use British spelling system

find

Page 152: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 152/340

Recursively examines all files in the <path_list>

Matches each file for one/more occurrence of <sel-criteria>depending on <mode>

Takes some action on the files selected

Syntax : find <path_list> <mode> <sel-criteria> <action>

Mode and Selection

criteria

Significance

-name <filename> Selects file with name <filename> . Pattern matching isll d

Page 153: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 153/340

allowed

-user <username> Selects file owned by user specified by <username>

-type <f> Selects type of files specified by <f>

-group <grpname> Selects file if owned by group <grpname>

-size +<x>[<c>] Selects file if the size exceeds <x> blocks(charactersif <c> is also specified)

-atime +<x> Selects file if accessed before <x> days

-amin +<x> Selects file if accesses before <x> minutes

-atime -<x> Selects file if accessed within <x> days

-amin -<x> Selects file if accesses within <x> minutes

-mtime -<x> Selects files if modified within <x> days

-mmin -<x> Selects files if modified within <x> minutes

-newer <flname> Selects file if modified after file <flname>

Action Significance

-perm<permission_mode>

Finds the files with permission mode specified

Page 154: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 154/340

-exec <command> {} \; Executes command <command> after finding everyfile on the search

-ok <command> {} ; Executes command <command> after userconfirmation

-print Shows selected files in display

grep

Syntax :

Page 155: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 155/340

Searches a file for a pattern

Syntax : grep [-c|-n|-v|-l|-I|-e|-h|-x ] <pattern…> <filename(s)>

Flag Significance

-c Counts number of occurrences. Output shows only the file name andnumber of times the searched pattern found within it

-n Displays the line number containing the pattern, along with the lines

-v Displays all lines excepting those containing the pattern

-l Displays only file names containing the pattern

-i Ignores case while searching for the pattern

-e Extended search. More than one pattern is to be searched

-h Suppresses printing filename when printing multiple files where thepattern is found

-x Matches are recognized only when the entire line of the file searchedmatches the fixed string

Example Significance

grep while wot.ksh Searches for the string „while‟ in the file :wot.ksh and if found, displays the linescontaining the pattern

Page 156: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 156/340

containing the pattern

grep while *.ksh Searches for the string „while‟ in all files

with extension „.ksh‟ and shows the file nameand the line containing the pattern

grep “List of names” *.txt  Searches for the string „List of names‟ in all.txt files.

grep – li while *.ksh Case-independent searching of the string

„while‟ takes place in all the .ksh files andthe the name of the files where matched isdisplayed only

grep –e “Subhendu” – e“suvendu” name.lst 

Searches for pattern „Subhendu‟ and„Suvendu‟ in the file : name.lst 

grep 'M[ao]noroma' name.lst Searches for the string „Manoroma‟ or„Monoroma‟ in the file : name.lst 

grep “^A” name.lst  Searches all lines starting with A

grep "Sinha$" name.lst Searches all lines ending with „Sinha‟ 

egrep

Extended grep

Page 157: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 157/340

Description Extended grep

Expression Significance<ch>+ Matches one/more presence of character <ch>

<ch>? Matches zero / presence of character <ch>

<expr1>|<expr2> Matches <expr1> or <expr2>

(<exp1>|<exp2>|<exp3> Matches expression <exp1><exp3> or<exp2><exp3>

Expression Significance

egrep '(Mano|Mono)roma'

name.lst

Matches string „Monoroma‟ or „Manoroma‟ 

egrep 'Aka+sh' name.lst Matches „Akash‟ , „Akaash‟,‟Akaash‟ 

egrep – f srchlist name.lst File : srchlist contains all the strings to besearched in file : name.lst

sed

S t

Page 158: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 158/340

Stream text editor, used for:-

•Displaying specific lines from a file by line no/pattern matching

•Inserting or changing texts in a file

•Deleting lines from a file

Can be addressed in two ways:-

By line number

By specifying a pattern which occurs in a line

Syntax : sed <options> <address,action> <filename(s)>

Done by specifying line number , or a pair of them separated by comma

Addressing by lines 

Page 159: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 159/340

to specify the lower and upper boundaries of selection.

Single word specifying action are:-p Print

q Quit

$p Last line

Negative indicator ‘!’ can also be used to signify negation

Examples Significance

sed „3q‟ name.lst  Show first three lines of file : name.lst andquit

sed –n „3,5p‟ name.lst  Show 3rd to 5th line of file : name.lst

sed –n ‟10,$p‟ name.lst  Show from line no. 10 to end of file

sed –n –e „1,2p‟ –e „7,9p‟ –e „$p‟name.lst

Show 1st to 2nd line, 7th to 9th line and last lineof the file : name.lst

Examples Significance

$ sed 'a\ Inserts the line „subhendu‟ after every line in

Page 160: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 160/340

$ sed a\

> subhendu

> ' del>del1

Inserts the line subhendu after every line inthe file : del and save the display in file : del1

$ sed „i\

> subhendu

> ' del>del1

Inserts the line „subhendu‟ before every line inthe file : del and save the display in file : del1

$ sed „$a\> subhendu

> ' del>del1

Inserts the line „subhendu‟ at the end of file :del and saves under file : del1

Example Significance

sed – n „/getopts/p‟ emp.ksh Searches the string „getopts‟ in file : emp.ksh

Context addressing

Page 161: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 161/340

g g p pand displays it

sed – n „/getopts/,/charstring/p‟ emp.ksh

Searches for the string „getopts‟ and„charstring‟ in file : emp.ksh 

sed – n „/[cC]hatterjee/p‟ 

emp.lst

Searches for the string „Chatterjee‟ or„chatterjee‟ in file : emp.lst 

sed -n '/Aka*sh/p' name.lst Searches for any string with the pattern

specifiedsed – n „/^A.a.s/p‟ emp.lst Searches for any line containing „A‟ as 1st 

character, „a‟ as 3rd character and „s‟ as 5th character

sed – n „/^$/ p‟ emp.lst

sed „/^$‟/d‟ emp.lst

Shows all but the blank lines from file emp.lst

sed – n „/^….$/p‟ emp.lst Shows all lines with 4 characters only

sed -n '/. {101, }/p' del Shows lines longer than 100 characters

sed -n '/. {9 }9/p' del Shows lines containing „9‟ after 9 characters ina line

Examples Significance

sed –n „/if/w iflist 

/while/w wlist‟emp ksh

Searches file emp.ksh for pattern : if andwrites matching lines into file : iflist

Searches file emp ksh for pattern : while

Page 162: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 162/340

emp.ksh Searches file emp.ksh for pattern : while and writes matching lines to file : wlist

sed –n –f instr.lst emp.ksh Searches file : emp.ksh for the stringsspecified in the file instr.lst

Substitution

Examples Significance

sed „s/exhausted/tired/‟ list1 

sed „s/exhausted/tired/p‟ list1 Shows the content of the file : list1with the string „exhausted‟ replacedby „tired‟ at all occurences 

sed „1,5s/basic/prelim/‟ list1  Shows from 1st to 5th line of file :list1 with the string „basic‟ replaced

by „prelim‟ at all occurences sed –n „/marketing/s/staff/member/p‟

list1Replaces „staff‟ with „member‟ in allthe lines containing „marketing‟ 

sed „s/student/ex &/‟ name.lst  Replaces „student‟ with „ex student‟ 

awk

Page 163: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 163/340

•Reporting tool for Unix.•Developed by Alfred V Aho, Peter JWeinberger, Brian W Kernighan

•Emerging as programming language.

•Produces facilities similar to SQLlanguage.

 Syntax : awk <options> „ <address> { <actions> }‟ <input|file(s)>

Page 164: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 164/340

„AWK‟ is a pattern matching and processing language .It can search file(s) searching for a pattern, and when

found, performs specified action.Works best with ascii files, preferably not to use withbinary files.

Using awk from command line

Page 165: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 165/340

$ awk „ { print $0 }‟ 

Good morning

Good morning

Hello

Hello

^d

$

$ awk „ { print $1,$3 }‟ 

Happy Birth day

Happy day

How are you?

How you?

^d

$

Read data from a file

Page 166: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 166/340

$ cat name.lst

0001 | Subhendu Majumdar | Team Lead | Avaya | 25000.00 |

0005 | Raghab Das | Programmer| Avaya | 12000.00 |

0006 | Sumit kumar Basu | Programmer| Avaya | 12000.00 |

0011 | Tamal Sen Sharma | Programmer| Avaya | 12000.00 |0010 | Ratna Sengupta | Accountant | Avaya | 12000.00 |

0015 | Raghab Dasgupta | Programmer| Avaya | 12000.00 |

0025 | Rajib Banerjee | Programmer| Avaya | 12000.00 |

0012 | Damini Sen | Operator | Nestle| 10000.00 |

0007| Akash Nag | Salesman | Nestle| 7800.00 |

0009| Anindya Das | Manager | Nestle| 30000.00 |

0019| Sougata Das | Manager | Avaya | 50000.00 |

name.lst

$ awk – F”|” { print $1,$2 }‟ name.lst 

0001 Subhendu Majumdar

Page 167: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 167/340

0005 Raghab Das

0006 Sumit kumar Basu

0011 Tamal Sen Sharma

0010 Ratna Sengupta

0015 Raghab Dasgupta

0025 Rajib Banerjee

0012 Damini Sen

0007 Akash Nag

0009 Anindya Das

0019 Sougata Das

Taking awk instructions from a file

Page 168: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 168/340

$ cat name.awk

{ print "-->" , $2 }$ awk -F"|" -f name.awk name.lst

--> Subhendu Majumdar

--> Raghab Das

--> Sumit kumar Basu

--> Tamal Sen Sharma

--> Ratna Sengupta

--> Raghab Dasgupta

--> Rajib Banerjee

--> Damini Sen

--> Akash Nag

--> Anindya Das

--> Sougata Das

Variables Significance

Pre-defined variables in awk

Page 169: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 169/340

g

ARGC Number of command line arguments

CONVFMT Conversion format for numbers

FIELDWIDTHS Whitespace separated string for the width of input fields.

Provides a facility for fixed-length fields instead of using

field separators.

FILENAME Name of current input file

NR Current record number

FS Input field separator

IGNORECASE 0(Case sensitive)

1 ( Case insensitive)

NF Number of fields in the current record

FNR Current record number

Variables Significance

Pre-defined variables in awk…contd

Page 170: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 170/340

g

OFS Output field separator

ORS Output record separator(default is newline)

RS Input record separator( default is new line)

FILENAME Name of current input file

NR Current record number

FS Input field separator

IGNORECASE 0(Case sensitive)

1 ( Case insensitive)

ENVIRON Unix environment variables

ERRNO Unix system error message number

$ cat name1.lst

Example of using pre-defined variables

Page 171: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 171/340

0001 | Sourav Ghar | Team Lead | Avaya | 25000.00|

$ cat name2.lst

0001 | Sourav Dutta | Project M | Avaya | 25000.00|

0002 | Ratna Sengupta | Programmer| GFS

$ cat name.awk

{

FS = "|" ;

OFS = " ** " ;

ORS = "\n-------------------------------------------\n";

print ARGC , ENVIRON["TERM"], CONVFMT , FILENAME ;

print NR , $2 , $3 , "Total fields" , NF ;

}

Example of using pre-defined variables…contd 

$ awk -f name.awk name1.lst name2.lst

Page 172: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 172/340

$

3 ** vt100 ** %.6g ** name1.lst

-------------------------------------------

1 ** Sourav Ghar ** Team Lead ** Total fields ** 6

-------------------------------------------

3 ** vt100 ** %.6g ** name2.lst

-------------------------------------------

2 ** Sourav Dutta ** Project M ** Total fields ** 6

-------------------------------------------

3 ** vt100 ** %.6g ** name2.lst

-------------------------------------------

3 ** Ratna Sengupta ** Programmer ** Total fields ** 4

------------------------------------------- 

Expression Significance

Regular Expression Metacharacters in Awk:-

Page 173: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 173/340

p g

\n Newline

\t Tab

^ Starts match at the beginning

$ Matches at the end of the string

. Matches any single character

[ABC] Matches any of A,B or C

[A-Ca-c] Matches any of A,B,C,a,b or c

[^ABC] Matches any character other than A,B or C

Desk|Chair Matches any of „Desk‟ or „Chair‟ 

[ABC][DEF] Matches any of A,B or C followed by D,E or F

Expression Significance

Regular Expression Metacharacters in Awk…contd

Page 174: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 174/340

p g

[ABC]* Matches zero/more occurences of A,B or C

[ABC]+ Matches one/more occurences of A,B or C

[ABC]? Matches to an empty string or more of A,B or C

( ) (Blue|Black)berry matches to Blueberry or Blackberry

Operators Significance

Comparison Operators in awk

Page 175: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 175/340

== Is equal to

< Less than

<= Less than or equal to

> Greater than

>= Greater than or equal to

!= Not equal to

~ Matched by regular expression

!~ Not matched by regular expression

Operators Significance

Compound pattern Operators

Page 176: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 176/340

&& Logical AND

|| Logical OR

! Logical NOT

( ) Used to group compound statements

Range pattern Operators

Operators Significance

awk -F"|" 'NR==3,NR==5 { print NR,$2 } '

name.lst

Shows 3rd,4th and 5th record

BEGIN and END blocks

Page 177: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 177/340

BEGIN {

<declare variables>< write headings>

}

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

END {

< show subtotals>

}

$ awk „/Programmer/ { print }‟ name.lst  # Details of all programmers

0005 | Raghab Das | Programmer| Avaya | 12000.00|

0006 | Sumit kumar Basu | Programmer| Avaya | 12000.00|

0011 | Tamal Sen Sharma | Programmer| Avaya | 12000.00|

Page 178: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 178/340

0015 | Raghab Dasgupta | Programmer| Avaya | 12000.00|

0025 | Rajib Banerjee | Programmer| Avaya | 12000.00|

$ awk „/Prog*/ { print }‟ name.lst  # Details of all programmers

0005 | Raghab Das | Programmer| Avaya | 12000.00|

0006 | Sumit kumar Basu | Programmer| Avaya | 12000.00|

0011 | Tamal Sen Sharma | Programmer| Avaya | 12000.00|

0015 | Raghab Dasgupta | Programmer| Avaya | 12000.00|

0025 | Rajib Banerjee | Programmer| Avaya | 12000.00|

$ awk '/Operator/ ' name.lst  # Details of all Operators

0012 | Damini Sen | Operator | Nestle| 10000.00|

$ awk -F "|" '/Operator/ { print $2,$5 }' name.lst  #Field 2 and 5

Damini Sen 10000.00

Page 179: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 179/340

$ awk -F"|" '/(Sen|Das)gupta/ { printf "%3d % -20s n" , NR , $2 }'

name.lst

5 Ratna Sengupta

6 Raghab Dasgupta

(Shows the record number and names of all Sengupta-s and Dasgupta-s)

$ awk -F"|" 'NR==3,NR==5 { print NR,$2 } ' name.lst3 Sumit kumar Basu

4 Tamal Sen Sharma

5 Ratna Sengupta

(Shows line number and second field of 3rd-5th records)

$ awk -F"|" '$3==" Programmer" { print $2,$4,$5 }' name.lst

Raghab Das Avaya 12000.00

Page 180: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 180/340

Sumit kumar Basu Avaya 12000.00

Tamal Sen Sharma Avaya 12000.00Raghab Dasgupta Avaya 12000.00

Rajib Banerjee Avaya 12000.00

(Shows 2nd,4th and 5th field of all records where 3rd field contains theword „ Programmer‟) 

$ awk -F"|" '$3~ /Manager/ && $4~ /Nestle/ ' name.lst

0009 | Anindya Das | Manager | Nestle| 30000.00| 

(Shows the records where 3rd field contains the string „Manager‟ and 4th 

field contains the string „Nestle‟. 

$ awk -F"|" '$3 ~ /Programmer|Accountant/' name.lst

0001 | Subhendu Majumdar | Team Lead | Avaya | 25000.00|

Page 181: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 181/340

0012 | Damini Sen | Operator | Nestle | 10000.00|

0007 | Akash Nag | Salesman | Nestle| 7800.00 |0009 | Anindya Das | Manager | Nestle| 30000.00|

0019 | Sougata Das | Manager | Avaya | 50000.00| 

(Shows all records where 3rd fiels contains strings other thanProgrammer and Accountant)

$ awk -F"|" '$5>=30000 { printf "%20s t t %d n" , $2, $5 }' name.lst

Anindya Das 30000

Sougata Das 50000

(Shows the name and salary of persons with salary >=30,000)

$ awk -F"|" '$3~ /Manager/ {

> kount = kount + 1

Page 182: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 182/340

> print kount,$2 } ' name.lst

1 Anindya Das

2 Sougata Das

$ awk -F"|" '$5>=30000 { printf "%20s t t %d n" , $2, $5 }' name.lst

Anindya Das 30000

Sougata Das 50000

(Shows the name and salary of persons with salary >=30,000)

$ cat salavg.awk

BEGIN {

printf "\n\t\t Salary Report of Managers \n\n"

Page 183: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 183/340

}

$3~ /Manager/ { kount++

tot = tot + $5

print kount,$2,$5

}END {

print "\n\nTotal managers found is :",kount

print "Average salary is :",tot/kount

}

$ awk -F"|" -f salavg.awk name.lst

Salary Report of Managers

Page 184: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 184/340

Salary Report of Managers

1 Anindya Das 30000.00

2 Sougata Das 50000.00

Total managers found is : 2

Average salary is : 40000

String Operators

Page 185: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 185/340

(A) Concatenating Strings

x = “abc””def”  # x = abcdef

y = “ghi” 

z = x y # z = abcdefghi

Built-in String Functions

Functions Significance

gsub( <reg>,<string>,<target>) Substitutes string <string> in stringt t ti th l

Page 186: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 186/340

<target> every time the regular

expression <reg> is matchedindex( <search>,<string> ) Returns the position of the string

<search> in string <string>

length(<string>) Returns the length of the string<string>

match( <string> , <reg>) Returns the position in string <string>that matches the expression <reg>

split(<string>,<store>,<delim>) Splits string <string> into arrayelements of <store> based on

delimiter <delim>sub(<reg>,<string>,<target>) Substitutes string <string> in <target>

the first time the regular expression<reg> is matched.

Built-in String Functions

Functions Significance

substr(<string> <pos> <len>) Extracts the portion of the string

Page 187: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 187/340

substr(<string>,<pos>,<len>) Extracts the portion of the string

<string> starting from position <pos>of length <len>

tolower(<string>) Translates the string <string> rtolower case

toupper(<string>) Translates the string <string> rto

upper case

Use of Character Functions in awk

$ cat awkf.awk

Page 188: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 188/340

$5 >= 30000 { print "Length of the string is : " , length ( $2 )

print "First three characters are : " , substr( $2,1,3 )

print "String in lowercase is : " , tolower( $2 )

print "String in uppercase is : " , toupper( $2 )

nm = substr( $2,2,4 )print " 2nd to 5th characters are :", nm

}

Use of Character Functions in awk…Contd 

$ awk -F"|" -f awkf.awk name.lst

Page 189: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 189/340

Length of the string is : 19

First three characters are : An

String in lowercase is : anindya das

String in uppercase is : ANINDYA DAS

2nd to 5th characters are : AninLength of the string is : 19

First three characters are : So

String in lowercase is : sougata das

String in uppercase is : SOUGATA DAS

2nd to 5th characters are : Soug

Use of Character Functions in awk…Use of Split function

$ cat split.awk

Page 190: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 190/340

BEGIN {

FS="|"

printf "\n Details of name of the Employees : "

print "\n\t Name \t\t\t\t First name \t\t Last name"

print "\n\t............................................................"}

$5 > 15000 { split( $2 , a , " " )

print "\n\t", $2, "\t\t", a[1] , "\t\t" , a[2]

}

Use of Character Functions in awk…Use of Split function

$ awk -f split.awk name.lst

D il f f h E l

Page 191: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 191/340

Details of name of the Employees :

Name First name Last name

………………………...........................................................  

Subhendu Majumdar Subhendu MajumdarAnindya Das Anindya Das

Sougata Das Sougata Das

Special String Constants

Expression Significance

\\ Backslash

Page 192: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 192/340

\\ Backslash

\a Alert or bell character\b Backspace

\f Formfeed

\n Newline

\r Carriage return\t Tab character

\v Vertical tab

\”  Double quote

Built-in Numerical Functions

Expression Significance

cos(x) Cosine of x in radians

Page 193: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 193/340

cos(x) Cosine of x in radians

int(x) Integer value of xexp(x) Returns e raised to power of x

log(x) Natural log of x

rand( ) Returns random number between 0 and 1

sin(x) Returns the sine of x in radianssqrt(x) Returns square root of x

systime() Current time in seconds since midnight, Jan 1,1970

Arithmetic Functions

Expression Significance

x^y Raises x to the power of y

Page 194: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 194/340

x y Raises x to the power of y

x**y Raises x to the power of yx%y Calculates the reminder of division of x by y

x+y Adds y to x

x-y Subtracts y from x

x*y Multiplies y with xx/y Divides „x‟ by „y‟ 

x++ Increments x by 1 and then uses it

++x Uses x and then increments it by 1

 y-- Decrements y by 1 and then uses it--y Uses y and then decrements it

Arithmetic Functions

Expression Significance

x+=y x = x+y

Page 195: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 195/340

x+=y x = x+y

x-=y x = x-yx*=y x = x*y

x/=y x = x/y

x++=y x = x + 1 + y

Arrays

Arrays are normally used to handle more than one relatedi f d t

Page 196: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 196/340

piece of data.

One accesses the individual elements within an array byenclosing the subscript within double brackets.

In AWK, one does not have to declare a variable to be anarray, and does not have to define the maximum no. of

elements. When one uses an element for the first time, it iscreated.

In awk arrays, subscript is a string ; viz.,

tot_sales[“Cal”] = 10.15 

One can use this in conditional flow:-

If “Cal” in tot_sales 

Delete tot_sales[“Cal”] 

Multidimensional Arrays

One can use two subscripts to form a multi-dimensionalarray

Page 197: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 197/340

array.

Example : tot_sales[“India”,”Calcutta”] = 100 

And can use this in conditional statements also:-

If (“India”,”Calcutta”) in tot_sales 

Examples of Arrays

BEGIN {

FS = "|"

print "\n\t Name \t\t Basic \t\t da \t\t hra \n"

Page 198: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 198/340

print \n\t Name \t\t Basic \t\t da \t\t hra \n

print "---------------------------------------------------------------"

}

$3~ /Manager/ {

da = $5 * 0.25; hra = $5 * 0.3;

print $2 ,"\t" , $5 , "\t" , da , "\t\t" , hra ;tot[1] += $5; tot[2] += da ; tot[3] += hra ; tot[4] += $5 + da + hra ;

}

END {

print "\n-------------------------------------------------------------"print "Total" , "\t\t\t" , tot[1] , "\t\t" , tot[2] , "\t\t" , tot[3] ;

print "---------------------------------------------------------------"

print "\n\n Grand Total : ", tot[4] ;

}

Examples of Arrays…contd 

$ awk – f salcalc.awk name.lst

Page 199: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 199/340

Name Basic da hra

---------------------------------------------------------------

Anindya Das 30000.00 7500 9000

Sougata Das 50000.00 12500 15000

-------------------------------------------------------------

Total 80000 20000 24000

---------------------------------------------------------------

Multidimensional Arrays-Examples

$ cat pop lst

Page 200: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 200/340

$ cat pop.lst

India|Chennai|West|150000

India|Calcutta|North|234500

India|Calcutta|East|134500

India|Chennai|East|100000

India|Calcutta|South|234508

India|Calcutta|Wast|100500

Multidimensional Arrays-Examples…contd 

$ cat pop.awk

BEGIN {

system(" tput clear " );

Page 201: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 201/340

system( tput clear );

print "\n\nPopulation in different parts of the cities inIndia\n\n"

FS = "|"

}

{ print $2 , "\t" , "\t" , $3 , "\t" , $4 ;tot_pop[$1,$2]+=$4;

}

END {

print "--------------------------------------------------------"

print Population for Calcutta : ,tot_pop[ India , Calcutta ];

print Population for Chennai , tot_pop[ India , Chennai ];

}

Multidimensional Arrays-Examples…contd 

$ awk – f pop.awk pop.lst

Page 202: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 202/340

Population in different parts of the cities in India

Chennai West 150000

Calcutta North 234500

Calcutta East 134500

Chennai East 100000

Calcutta South 234508

Calcutta West 100500

--------------------------------------------------------

Population for Calcutta : 704008Population for Chennai 250000

Conditional Flow

{

if

$ cat sal.awk

4 ~/Nestle/ {

Page 203: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 203/340

if ……….. 

else if …….. 

else if …….. 

else ……… 

}

4 ~/Nestle/ {

if ( $5 > 10000 )print $2 , $5 , "Taxable";

else print $2 , $5 , "Non taxable";

}

$ awk -F"|" -f sal.awk name.lst

Damini Sen 10000.00 Non taxable

Akash Nag 7800.00 Non taxable

Anindya Das 30000.00 Taxable

Conditional Flow

To substitute „if‟ test anywhere in the code:-

condition ? True : false

Page 204: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 204/340

$ cat sal.awk

$4 ~/Nestle/ {

$5 > 10000 ? taxyn = "Taxable" : taxyn = "Non taxable" ;

print $2 , $5 , taxyn ;}

$ awk -F"|" -f sal.awk name.lst

Damini Sen 10000.00 Non taxableAkash Nag 7800.00 Non taxable

Anindya Das 30000.00 Taxable

Loops

Page 205: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 205/340

Loops can be of three types :

• Do

• While

• For

Used to perform some repeatitive jobs.

Do Loop

do

<statement>

Page 206: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 206/340

<statement>

While ( <conditions> )

statement  in any of the above constructs may be either a simplestatement or a series of statements enclosed in { }

Example of Do Loop

$ cat doloop.awk

BEGIN {

Page 207: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 207/340

BEGIN {

linesep = "-"; linedraw = "-"; FS = "|";

}

{

res = $5 / 1000 ;do

{ linedraw = linedraw"-" ; res--; }

while ( res >= 1 );

print $2 , linedraw ;

linedraw = linesep ;

}

Example of Do Loop…contd 

$ awk – f doloop.awk name.lst

Subhendu Majumdar --------------------------

Page 208: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 208/340

Subhendu Majumdar

Raghab Das -------------

Sumit kumar Basu -------------

Tamal Sen Sharma -------------

Ratna Sengupta -------------Raghab Dasgupta -------------

Rajib Banerjee -------------

Damini Sen -----------

Akash Nag --------

Anindya Das -------------------------------

Sougata Das ---------------------------------------------------

While Loop

While ( <condition> )

statement

Page 209: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 209/340

statement

statement  in any of the above constructs may be either a simplestatement or a series of statements enclosed in { }

Example of While Loop

$ cat while.loop

BEGIN {

Page 210: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 210/340

{

linesep = "-"; linedraw = "-"; FS = "|";

}

{

res = $5 / 1000 ;

while ( res >= 1 )

{

linedraw = linedraw linesep ; res--;

}

print $2 , linedraw ;

linedraw = linesep ;

}

Example of While Loop…contd 

$ awk – f while.loop name.lst

Subhendu Majumdar --------------------------

Page 211: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 211/340

Subhendu Majumdar

Raghab Das -------------

Sumit kumar Basu -------------

Tamal Sen Sharma -------------

Ratna Sengupta -------------Raghab Dasgupta -------------

Rajib Banerjee -------------

Damini Sen -----------

Akash Nag --------

Anindya Das -------------------------------

Sougata Das ---------------------------------------------------

For Loop

For ( <var> = <value> , var <operator> <value_last> , <var> [++|--]

<statements>

Page 212: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 212/340

statements

For ( <subscript> in <array> )

<statement>

statement  in any of the above constructs may be either a simplestatement or a series of statements enclosed in { }

Example of For Loop

$ cat for.loop

BEGIN {

li " " li d " " FS "|"

Page 213: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 213/340

linesep = "-"; linedraw = "-"; FS = "|";

}

{

res = $5 / 1000 ;

for ( res = res ; res >= 1 ; res -- )

{

linedraw = linedraw linesep ; res--;

}

print $2 , linedraw ;

linedraw = linesep ;

}

Example of For Loop…contd 

$ awk – f for.loop name.lst

Subhendu Majumdar --------------------------

Page 214: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 214/340

j

Raghab Das -------------

Sumit kumar Basu -------------

Tamal Sen Sharma -------------

Ratna Sengupta -------------Raghab Dasgupta -------------

Rajib Banerjee -------------

Damini Sen -----------

Akash Nag --------

Anindya Das -------------------------------

Sougata Das ---------------------------------------------------

Loop Breaking Statements

Statement Significance

Break Exits the loop

Page 215: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 215/340

Continue Leaves the current record and continues with the nextrecord in the loop from beginning of the loop processingstatements.

Pretty formatting - printf

Syntax : printf( format_specifier, var1, var2,…..varn) 

Page 216: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 216/340

Format Meaning

%c Ascii character

%d Integer

%i Integer

%e Floating point number using scientific notation

%f Floating point number(eg., 10.43)

%s String of characters

%g Awk chooses %e or %f display format(whichever is

shorter) suppressing non-significant zeroes.

Output to another file

Statement Significance

Printf( “Hello world\n”) > “datafile”  Creates a file called datafile

Page 217: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 217/340

with the outputPrintf( “Hello world\n”) >> “datafile”  Appends the output at the

end of existing file “datafile”or creates a new file.

A file should be closed after it receives output from an awkprogram.

Syntax : close(“filename”) 

Functions

Block of code, accesses usually in multiple places in the code.

Page 218: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 218/340

y p p

When awk reaches the end of the function, it implicitlyreturns the control to the calling routine.

To make an explicit return to the main program, one canexplicitly use the return statement.

Syntax :

function fname(parameter list) {

< function_code>

}

Example of Functions

$ cat subcalc.awk

BEGIN {

FS = "|"

show heading( "\n\t Name \t\t Basic \t\t da \t\t hra \n" ) ;

Page 219: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 219/340

show_heading( \n\t Name \t\t Basic \t\t da \t\t hra \n ) ;

show_heading( "-------------------------------------------------");

}

$3~ /Manager/ {

da = calc_comp(0.25,$5) ; hra = calc_comp(0.3,$5) ;

print $2 ,"\t" , $5 , "\t" , da , "\t\t" , hra ;}

function show_heading(heading) {

print heading ;

}function calc_comp(rate,val) {

val = val * rate ;return val ;

}

Example of Functions…contd 

$ awk –f salcalc.awk name.lst

Name Basic da hra

Page 220: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 220/340

Name Basic da hra

---------------------------------------------------------------

Anindya Das 30000.00 7500 9000

Sougata Das 50000.00 12500 15000

Use of inputs from user in awk program

BEGIN {

FS="|"

Page 221: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 221/340

printf "%20s%3d\n\n", "Cut-off basic pay : ",cobp

}

$5 > cobp {

kount++print $2 , $3 , $5 , "\n"

}

END {

print "\n\n The End\n\n"

}

Use of inputs from user in awk program…Contd 

$ awk -v cobp=15000 -f tkinp.awk name.lst

Cut-off basic pay : 15000

Page 222: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 222/340

Subhendu Majumdar Team Lead 25000.00

Anindya Das Manager 30000.00

Sougata Das Manager 50000.00

The End

Page 223: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 223/340

Controlling Environment 

The Unix environment is controlled by a

Page 224: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 224/340

number of pre-defined environmentvariables.

They are usually defined in the file „.profile‟ 

for the user or are defined by user as andwhen required.

Pre-defined

variable

Significance

ERRNO Non-zero exit code of last command that failed.Value changes only when a command fails

Page 225: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 225/340

LINENO Meaningful only within a shell script. Its value is theline no. of the line in the script currently beingexecuted

OLDPWD Value is always the full pathname of the directorywhere the control resided before coming intocurrent working directory

PWD Current working directory

SECONDS Integer number of seconds since one invoked the

Korn shellPPID Value of the process id of the parent process of $$

PATH Absolute directory path under which the programsfor the commands reside.

Pre-defined

variable

Significance

EDITOR Vi or emacs

Page 226: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 226/340

EDITOR Vi or emacs

CDPATH List of colon separated directory name, which isfollowed by the system when the user issues a „cd‟command

ENV Pathname of the shell script containing commands

to be executed when the korn shell is invokedHISTFILE Filename of the korn shell history file

HISTSIZE Integer number containing maximum number ofcommands to be retained in history files

HOME Pathname of home directory for an user

Pre-defined

variable

Significance

IFS Zero/more characters to be treated by the shell asde-limiters when parsing a command line into words

Page 227: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 227/340

or using the read commandCOLUMNS Display width used by Korn shell edit mode- vi or

emacs

LINES Integer number representing the number of linesdisplayed by the terminal.

MAIL Pathname of a file to be monitored by the shell fora change in its date of last modification. If changeis noted, the shell issues the message : You have

mail  at the next oppurtunity

MAILCHECK No. of seconds after which the shell should checkfor a change in the MAIL file

PS1 Primary shell prompt string.Prompt where commandsare issued by the user

Pre-defined

variable

Significance

PS2 Secondary prompt screen. Shown when a commandpawns more than one line.

Page 228: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 228/340

PS4 Debug prompt screen , shown when a shell programis executed using –v option.

SHELL Pathname of the shell the user is using

TERM Symbolic alpha numeric string that identifies thetype of your terminal

TMOUT Seconds after which automatic logout occurs.

 stty

Controls terminal output and sets terminal characteristics

Page 229: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 229/340

Options Significancestty – a Displays all current settings

stty – echoe Enables backspacing remove character from thedisplay

stty – echo Keyboard entry is not echoed

stty echo Keyboard entry is echoedstty intr ^Z Sets <Ctrl-Z> as the interrupt key

stty eof \ ^a Sets <Ctrl-a> to terminate output and declare end-of-file, for eg., while creating file using cat command

stty erase „^H‟  Typing <Ctrl-H> helps to remove the last charactertyped

stty quit „^d‟  Typing <Ctrl-d> enables aborting the current shell

Options Significancestty susp „^Z‟  Enables suspending a foreground process when <ctrl-Z> is

pressed

stty stop „^S‟  Enables halting the current session by pressing „^S‟ 

stty start „^Q‟  Enables starting the current halted session by pressing

Page 230: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 230/340

„^Q‟ stty rows 20 Set 20 rows in display

stty rows 20column 80

Sets 20 rows and 80 columns in display

stty iuclc Maps uppercase alphabets to lowercase

stty olcuc Maps lowercase alphabets to uppercase

stty size Gives the current screen size in terms of rows and columns

stty eol „^J‟  Pressing <Ctrl-J> does the job of ending a line

stty sane Set the terminal characteristics to values that will workfor most terminals

Page 231: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 231/340

Process 

This section will give you a brief idea on

Process

Page 232: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 232/340

Running Jobs

Scheduling Jobs

What is a process?

Process is a program that has its own address

Page 233: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 233/340

p gspace. Every command fired in Unix has a processassociated with it.

Is born when a program starts execution, andremains alive as long as the program is active.Afterexecution is complete, it dies.

Has a name, usually the name of the programbeing executed.

Kernel controls the process

Kernel is finally responsible for managing the

Page 234: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 234/340

y p g gprocess.

Determines time and priorities allocated toprocessed, enabling multiple processes to share CPUresources.

Each one is uniquely identified by PID, ProcessIdentifier, allocated by the Kernel when it is born.

Sh Process

A process is immediately set up by the Kernel

Page 235: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 235/340

p y p ywhen a user logs into Unix system.This os usually aUnix command(sh/ksh/bash etc).

Any command executed is actually input to theshell process.

Can be known from the value of shell variable „$$‟.This process for the shell remains alive till the userlogs out.

Parent and Child process

Every process has a parent process(except the

Page 236: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 236/340

root process).

When a command is executed from the shellprompt, that becomes a child process to the parentshell process.

A parent process can ave more than one childprocess

Cat name.lst| grep „student‟ 

Both the process : cat and grep have same parent (shell)

 process

ps –  process status

Displays the attributes of a process

Page 237: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 237/340

Command Significance

ps Selects the process associated with the current

terminal.Shows output in four columns: PID,

TTY(terminal info), TIME(total cpu time used bythe process), CMD(command gererating the

process)

ps – f Displays process ancestry.Produces 8 column

output:- UID(userid),PID,PPID(Parent PID), C( amt

of CPU time consumed by a process),STIME(time

the process started),TIME,CMD

Command Significance

ps – u ems2000 Displays processed of user : ems2000

ps – a Displays all processes of all users;

Page 238: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 238/340

excepting the system processes

ps – x Shows command line in extended format

ps – e Displays system processes

How a process is created in Unix?

Forking : A copy of the the process that invokes a

Page 239: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 239/340

new process is created.Child gets a new PID.

Exec : The parent then overwrites its image with acopy of the program to be executed. Done withexec system call. No additional process is createdat this stage.

Wait: Parent then executes the wait system callto keep waiting for the child process to complete.

After the child process is completely executed, aterminal signal is sent to the parent.

Internal and External Commands

External program : Commands like : cat, ls,

Page 240: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 240/340

p ggrep, sed etc are external programs.Shell creates aprocess for each of these commands.

Shell Scripts : Shell executes shell scripts by

spawning to another shell, which then becomes theparent of the commands featured in the script.

Internal Commands : These are commandswhich are executed directly by the current shell.

No additional processes are generated.Example : cd , echo etc 

Running jobs in background

Page 241: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 241/340

Unix is a multi-tasking system , allowing toperform more than one job at a time.

One job can be executed in foreground at a time,but many can be executed at background.

„&‟ operator at the end of the command lineinterprets the job to be done at background. Shellreturns the PID of the background job for tracking. 

Job control in Korn and Bash Shells

Page 242: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 242/340

Korn or bash shell users can use the job controlfacility of the shell to manipulate jobs.One can puta job in the background, bring it back toforeground, suspend it, run it later, or even kill it.

bg

Page 243: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 243/340

You are running a job in the foreground.It is taking a lot of time. You want to do some other jobs.

Press ^Z. The job running in the foreground will be

suspended.

Enter the command : bg at the OS prompt and push it to be

executed in the background.

Unix will return you a PID for the process pushed to

background.

 jobs

Page 244: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 244/340

Shows the jobs running in background.Command : jobs –l prints PID of each job besides job

number.

This command is used by the user to push a job from

background to foreground.

fg

Page 245: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 245/340

Used to bring a background job toforeground and executeit.

fg [ <job_no>] brings the job from background to

foreground and executes it. For example , issuing of the

command : fg 2 executes job number 2 (viewed by jobscommand) from background to foreground and executes it.

Execution of fg command alone from the OS prompt

resumes latest created background process to be executed

inforeground.

stop

Syntax : stop [%<job_no>]

Page 246: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 246/340

Pauses a job which is being executed in background.

One can use the command : bg to resume execution of

the stopped job in background or can issue command :fg to bring it to foreground and execute it.

kill

Syntax : kill [options] <PID>

Page 247: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 247/340

System often requires to control execution of aprocess. This is done by sending signals to the process.

The process, after receiving the signal, may ignore it,terminate itself, or do something else.

The command : kill is used to terminate or suspend a

process running in background, taking unusual long time.

Example Significance

kill 2905 Kills the process with Pid : 2905

kill –9 2905

k ll

Surely kills a process with PID 2905

Page 248: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 248/340

kill –s SIGKILL 2905

kill –15 2906

kill –s SIGTERM 2906

Terminates the process with PID2906

kill –24 2900

kill –25 2900

Pause the process with PID 2900

kill –26 2900

kill –s SIGCONT 2900

Start the paused process with PID2900

kill &! Kill the latest background job

nice

Processes in the Unix are usually executed with equal priority.

B hi h i i j b b d h li U i

Page 249: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 249/340

But, high priority jobs must be executed at the earliest. Unixoffers : nice command to change the priority of execution of jobs.

Ordinary users can only reduce priority; super user can do both.

Nice value ranges from 0 to 39 ; commands run with a value of 20 in

both.Example : nice –n 15 prog1.sh &

The above command reduces the pririty value to 35

The nice and priority values of the processes can be displayed with

ps –l command.

batch

Schedules job for later execution, when the system load

i i

Page 250: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 250/340

permits it.Prevents too many high load jobs to run at the same

time.

Syntax for running a program : batch < prog1.sh

Any job scheduled in this way also goes to the at queueand can be removed using : at –r , provided it is firedbefore the job has been executed.

at

Schedules a program for execution at a specified time.

Di l li t f h d l d j b

Page 251: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 251/340

Displays a list of scheduled jobs.

Removes jobs from the scheduled list.

Flag Significance

-l Displays the list of jobs scheduled by the user

-m Mail a report of successful execution of the job

-r <joblist> Remove the jobs specified in the joblist from queue

Examples Significance

at 2300 my_job.sh

Executes program my_job.sh at

11 00 t i htat 11:00 pm my_job.sh

Page 252: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 252/340

11:00 pm tonightat 11:00 P my_job.sh

at 2300 today my-job.sh

at now + 6 hours njb.sh Schedules execution of njb.sh after6 hours from now

at 6:30 pm next week njb.sh Schedules execution of program :njb.sh at 6:30 pm next week

at now + 1 year njb.sh Schedules execution of program :njb.sh after 1 year from now

at 3:08 pm + 1 day njb.sh Schedules execution of program :njb.sh at 3:08 pm tomorrow.

Examples Significance

at 9 am Mon njb.sh Schedules execution of njb.sh at 9 am

i M d

Page 253: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 253/340

coming Monday.at 9 am tomorrow Schedules execution of njb.sh at 9 am

tomorrow.

at now + 5 minutes <

njb.sh Schedules execution of njb.sh after 5

minutes from now.at – f njb.sh now + 5

minutes

at 08:15 Jan 12 < njb.sh Schedule execution of njb.sh at 08:15am on January 24.

cron

Runs jobs periodically.

Page 254: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 254/340

Mostly dormant, but wakes every minute and checks fora file in /usr/spool/cron/crontabs directory for programsto be executed at that instant.

A user may be permitted to place a crontab file after hislogin name in this directory. This file contains a list of

commands, along with a schedule for execution.

 First field indicates number of minutes after the hour

h th d i t b t d Th 00 10

Sample format for a crontab file

Page 255: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 255/340

when the command is to be executed. The range 00-10schedules every minute in the first 10 minutes of the hour.

Second fields indicates the hour in the 24 hour formatfor scheduling.

3rd field(1 to 31) controls the day of the month. „*‟ meansall.

4th field(1-12) indicates the month

5th

 field(0-6) indicates the day of the week(Sunday with0)

6th field contains the command

 User can modify the file containing his cron commands asfollows:-

C t fil t t i th f t d ib d li

Page 256: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 256/340

Create a file : cron.txt in the format described earlier.Issue the following command from the Os prompt:crontab cron.txt.

There will be now a file „ems2000‟( for eg., the name of

the user) in /usr/spool/cron/crontabs with the contents ofcron.txt.

One can see the contents of crontab file issuing thecommand : crontab -l

Page 257: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 257/340

System Administration 

System Administration shutdown

Shutdown Unix server

suGo as you like

wallSend message to everybody

Page 258: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 258/340

umaskSet Default Permission

ulimitSet maxm. Space for user

duShow disk usage

dfFree bytes available

finger

Show User information

cryptPassword protect a file

shutdown

Executed from the root user to shut down the system.

Page 259: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 259/340

Commands Significance

shutdown – g2 Powers down system after 2 minutes

shutdown – y – g0 Immediate shutdown

shutdown – y – g0 – i6 Shutdown and reboot

shutdown 17:30 Shutdown at 5:30 pm

shutdown – r now Shutdown and reboot

shutdown – m Bring the system down tomaintenance(single user mode)

su

Takes to super user mode.

Page 260: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 260/340

Commands Significance

su Takes to super user mode. One can have all theaccesses same as the root user. Root password is

required after executing the commandsu mat One logs in as the user : mat. Asks for the

password.

wall

Addresses all the users simultaneously.

$ wall

Page 261: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 261/340

$ wall…………. 

………… 

^DAll the text written will be reflected in the terminals of allthe users.

$ wall < mesg.txt

Contents of the file mesg.txt are sent as the wall message.

umask

Used by System Administrator to set the default

permissions to assign to each file to be created by the

Page 262: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 262/340

permissions to assign to each file to be created by theuser.

User can modify this default settings.

To give permission 751 as a default to newly createdfiles, umask value will be (777 – 751) = 026.

This value is assigned as a command : umask 026

ulimit

Faulty programs or processes can eat up disk space.So, arestriction is to be imposed on the mazimum size of a file thatan user is permitted to create.

h f l l

Page 263: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 263/340

Ordinary user can reduce the default value . Super user canincrease / decrease it.

Examples Significance

ulimit –a Shows soft limits

ulimit –Ha Shows the hard limits

ulimit –t <size> Set the CPU time in seconds

ulimit –f <size> Set the maxm. Size in blocks

ulimit –d <size> Sets the maxm. size of data blocks in KBulimit –m <size> Sets the maxm. Size of memory in KB.

du

Shows disk usage – amount of space taken by a group of files inunits of 512 bytes or KB.

Descends all sub directories from the directory in which the

Page 264: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 264/340

Descends all sub-directories from the directory in which thecommand is fired.

Flag Significance

-a Print entries for each file encountered in the directoryhierarchies in additional to the normal output

-k Give the block count in terms of 1024 bytes

-s Give the grand total of disk usage for each of the directories

-r Print messages about directories that cannot be read, filescannot be accessed etc.

df

Displays the number of free 512 byte blocks and free inodesavailable for file systems by examining the count kept in the

superblocks

Page 265: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 265/340

superblocks.

Flag Significance

-b Reports only the number of KB free

-e Report the number of files free-f Reports only the actual count of the blocks in the free list

-i Reports the total number of inodes, number of free inodes,number of used inodes,% of inodes in use

-k Reports the allocation in KB

finger

By default, the command finger lists for each user in thesystem:-

Login name

Page 266: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 266/340

  Login name  Full given name  Terminal write status( if write permission is denied)  Idle time

  Login time  User‟s home directory and login shell   Project on which the user is working  Any plan put by user in the file .plan under home directory

  Office location and telephone number  Last time the user received the mail, the last time userread the mail.

Flag Significance

Suppresses information on home directory and shell

Page 267: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 267/340

-b Suppresses information on home directory and shell-f Suppresses the header info

-h Suppresses the project information

-i Suppresses the info on idle time

-l Suppresses long output-p Suppresses plan

-R Print user’s hostname

-s Forces short output format

File Significance

Wh fil

Files accessed while showing output

Page 268: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 268/340

/etc/utmp Who file

/var/admin/utmp Last login file

/etc/passwd User’s name, office etc

/etc/.plan User’

s plan/etc/.project User’s project info

/var/mail Mail directory

crypt

Used to encode/decode files using personal password

Options Significance

Page 269: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 269/340

crypt < uncrypted_file >crypted_file

Crypts file „uncryped_file‟ tocrypted_file. Asks for a password.

crypt passwd < file1 > file2 Crypts file : file1 to : file2 based onthe password : passwd

crypt passwd < file2 > file1 Decrypts file : file2 to : file1 usingthe password : passwd

vi –x file2 Helps to view an encrypted file : file2.Asks for a password

Page 270: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 270/340

Communication 

Communication mailx

Send and receive mails

write

Page 271: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 271/340

writeSend message to all users

ftp

Send and receive files

rloginRemote login

rcpCopy files to remote server

mailx

Used to send/receive mails

Syntax for sending mails:-

echo <body> | mailx s <subject> <remote address>

Page 272: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 272/340

echo <body> | mailx –s <subject> <remote_address>

cat <message file> | mailx –s <subject> <remote_address>

Syntax for receiving mails:-

mailx -emailx [-HL] [-u user]

mailx -f [-HL] [filename]

Sending mail

$ mailx –s “Test heading” [email protected] 

this is a test mail for communication

Case : 1

Page 273: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 273/340

this is a test mail for communication

~.

The job performed above will send a mail to : [email protected] 

with subject of the mail as : Test heading and body of the mail as :this is a test mail for communication.

 $ mailx [email protected]

Subject : Test mail number 2

Case : 2

Page 274: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 274/340

Subject : Test mail number 2

this is a test mail for communication

~.

The job performed above will send a mail to : [email protected] 

with subject of the mail as : Test mail number 2 and body of the

mail as : this is a test mail for communication.

If no subject is entered at the command line, a prompt asks for thesubject.

 $ echo “Hello mike”|mailx –s “Heading1” [email protected] 

The job performed above will send a mail to :ith bj t f th il : “ ” d b d f th il :

Case : 3

Page 275: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 275/340

The job performed above will send a mail to [email protected] with subject of the mail as : “Heading1” and body of the mail as :

“Hello mike”.

The job performed above will send a mail to : [email protected] with subject of the mail as : “Heading1” and body of the mail with

the texts written in the file : msgfile. 

Case : 4

$ cat msgfile| mailx –s “Heading1” [email protected] 

 $ mailx [email protected]

Subject : Test mail number 2

this is a test mail for communication

Case 5- You want to send a mail to some user and ccit to some other users.

Page 276: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 276/340

this is a test mail for communication

~h

To: [email protected]

Subject: testing

Cc: [email protected]

Bcc: [email protected]

(continue)

testing mail--please ignore

~.

EOT

.

Viewing received mails

mailx -e

mailx [-HL] [-u user]

mailx -f [-HL] [filename]

Flag Significance

Page 277: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 277/340

Flag Significance

-e Test for presence of mail. mailx prints nothing and exits

with a successful return code if there is mail to read.

0 Mail present

1 No mail

2 Other error

-f Read messages from filename instead of the user's system

mailbox. If filename is not specified, the secondary mbox is

used.-H/-L Prints header summary only

-u user Read user's mailbox.Can be used only if read access to user's

mailbox is not read protected.

Options available in viewing received mail

When one executes mailx command for viewing receivedmails:-

$ mailx

Page 278: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 278/340

$mailx Revision: 1.179.214.2 date: 98/12/01 01:29:55 Type ? for help.

"/var/mail/ems2000": 2 messages 2 unread

>U 1 [email protected] Mon Aug 18 04:50 33/1199 RE: 3rd mail

U 2 ems2000 Mon Aug 18 03:55 16/561 Program to be re-schedule

?_

It shows a list of unread mails with a „?‟ prompt below, waiting for

commands from the user.

Commands available at „?‟ command prompt Flag Significance

<n> Shows message number <n> , the number typed at thefirst column for every message.

Page 279: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 279/340

f f y g

type Shows all the messages in one shot, one by one

quit(q) Quit mail utility

next Shows next message

reply <n> Reply to message number <n>

mail <user> Opens mail utility to write mail to user <user>

writeType text into other user‟s terminal for information 

Case : 1

$ write ems2000Hello how are you

Page 280: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 280/340

$ write ems 000Hello, how are you

^d

The command flashes the message : Hello, how are you to all

person‟s terminal who have logged in as user : ems2000.Case : 2

$ write ems2000 pts/tc

Hello, how are you

^d

The command flashes the message : Hello, how are you to thatperson‟s terminal who is working in terminal pts/tc and logged in asuser : ems2000.

Case : 3

$ write ems2000 < fileshow.txt

The command flashes the message written in the file :fileshow.txt  to all person’s terminal who have logged in as user :2000

Page 281: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 281/340

ems2000.

User must be logged in and mesg should be set to y( mesg=y) forsuccessful write communication.

ftp

Copies file over a network connection between local host to remotehost. ftp runs on client server.

Syntax : ftp [-i] [-v] [-n] <remote_host>

Page 282: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 282/340

pf p

Flag Significance

-i Disable interactive prompting by multiple file commands.

-n Disable auto-login. If auto-login is enabled and user issues an „open‟command to establish connection to a remote server, he gets loginand passwd prompt. If it is disabled by „n‟, user does not get such

prompts. He then writes: user <username> <passwd> in the ftp modeto specify the user and password.

-v -v Enables verbose output. IT shows the actions when files arepassed from one server to another.

Commands fired from ftp mode

Command Significance

! <command> Executes command <command> in the local server

append <lf> <rf> Copy file <lf> in the local server to the end of file : <rf>

Page 283: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 283/340

in the remote server. If no such file exists in remoteserver, a new file is created.

ascii Sets the file transfer type to ASCII

binary Sets the file transfer type to binary

bye Close the connection to remote host, if it is open, andexit

case Remote file names with name containing all letters inuppercase are translated to lowercase when the file is

transported from remote server to local.cd <remdir> Set the working directory in the remote server to

<remdir>

cdup Set the current directory of the remote server to theparent of current working directory.

Command Significancechmod <mode> <file> Changes the mode of the file : <file> to mode

<mode>

close Terminate the connection to the remote server

mdelete <remote_files> Deletes the files in the remote server

mdir <rem files> <lcl file> Writes a list of remote files to local file

Page 284: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 284/340

mdir <rem_files> <lcl_file> Writes a list of remote files to local file<lcl_file>

mget <rem_files> Copy remote files to local system

get <rem_file> [<lcl_file>] Copt the remote file to local file.

mkdir <dirname> Creates a directory <dirname> in remote server.

mls <remfiles> <lclfile> Writes an abbreviated listing of remote files tolocal file <lclfile>

modtime <rem_file> Shows the last modification time of remote file

<rem_file>mput <local files> Copy files from local server to remote server

delete <rem_file> Delete file <rem_file> from the remote server.The file can be an empty directory.

Command Significancedir <rem_dir> [<lcl_file>] Shows a listing of remote directory <rem_dir>

to terminal/ into local file <lcl_file> .

disconnect Close the connection to remote host

beep Beeps when command is executed

lcd [local dir] Specifies the current working directory of the

Page 285: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 285/340

lcd [local_dir] Specifies the current working directory of thelocal server to directory : <local_dir> . Ifunspecified, user‟s home directory is consideredfor navigation.

newer <file_name> get file if remote file <file_name> is newerthan local file <file_name>

prompt Toggle forcing interactive prompting on multiplecommands

runique Toggle store unique for local files. IF turned on,then while receiving files from remote host, if a

remote file already exists with a name equal toa local file, a „.1‟ is appended to the name. Foranother repeatition, a „.2‟ is appended till 99such cases are found.

Command Significancesize <remote_file> Shows the size of the remote file

quit bye

reget <remfile>

[<lclfile>]

Acts like get. If local file <lclfile> exists and is

smaller than the remote file <remfile>, local file isassumed to be partially transferred from remote

Page 286: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 286/340

p yserver.Then, the transfer is continued from theapparent point of failure. This command is veryuseful to transfer very large files over thenetwoprk that tend to drop connections.

status Shows the current status of ftp- informations areshown on remote server name and all thesettings(prompt,type,bell,case etc)

sunique Toggle store unique for remote files. If turned on,then while transferring files to remote host, if a

remote file already exists with a name equal to alocal file, a „.1‟ is appended to the name of the filetransferred from local to remote server.. Foranother repeatition, a „.2‟ is appended till 99 suchcases are found.

Command Significancesystem Shows the type of operating system running in

remote server

umask [new mask] Set the default umask on the remote server to newvalue. IF nothing is specified, the current umask

value of the remote server is displayed.h l [< d>] If l h l is t d f ft t it sh s list

Page 287: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 287/340

help [<command>] If only help is typed from ftp prompt, it shows a listof all commands that can be used from ftp prompt.

If a command is mentioned for help, it shows oneline help explaining its significance.

glob toggle metacharacter expansion of local file names.If put on, one can use „*‟ , „?‟ etc characters tospecify portion / all of the file name.

rlogin

Logs into remote host <rhost> as user <username> with/without anypassword.

Syntax : rlogin <rhost> [-l <username>]

Page 288: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 288/340

p

If the users are same on both remote and local server, then option<username> is not required.

Prompting for password depends on the entries in file :

/etc/hosts.equiv or .rhosts under remote user‟s home directory ofthe remote server

rcp

Copy Single File

Copies file(s)/directory(ies) from one server to another

Page 289: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 289/340

rcp [-p] <srcfile1> <rem_user> @<remserver>:<pathname>/<dest_file> 

rcp [-p] <srcfile1> <remuser>@<remserver>:<dest_dir>

Copy Multiple Files

rcp [-p] <srcfile1> [<file2>]... <remuser>@<remserver>:<dest_dir>

Copy One or More Directory Subtrees

rcp [-p] -r <srcdir1> [<dir2>]... <remuser>@<remserver>:<dest_dir>

Copy Files and Directory Subtrees

rcp [-p] -r file_or_dir1 [file_or_dir2]... <remuser>@<remserver>:<dest_dir>

Copy files from server1 to server2 by server3

rcp <user1>@<server1>:<pathname>/<file>

<user2>@<server2>:<pathname>[/<file>] 

Shell Programming

Page 290: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 290/340

List of instructions written within a file.

Programming logics can be built in

Page 291: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 291/340

Programming logics can be built in.

Executes a set of related instructions, savingtime,resource and imparts better control.

The file has to be executable to make thething work.

Invoking Sub-Shell manually

When a script file with execute permission starts running, itruns in a sub-shell under the current shell.

Page 292: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 292/340

One can invoke a sub-shell manually to execute the scripts :-

$ sh prog1.sh

Or$ sh < prog1.sh

where prog1.sh is the name of the program. It may not haveexecute permission at this point of time.

Execute a script in the current shell

Syntax : . <progname>

Whenever a shell script is executed, it runs in a sub-shell underthe current shell To make the script run in the current shell one

Page 293: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 293/340

the current shell. To make the script run in the current shell, onehave to trigger the program as : . <progname>. The programneed not have execute permission before executing in thisfashion. 

Specifying the type of shell

There are different kind of shells with different utilities and

syntaxes. A shell script running fine in Korn shell, may not run ok inBourne shell

Page 294: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 294/340

Bourne shell.

To specify the interpreter shell, one can execute the programfrom OS prompt as :-

$ ksh < prog1.kshThis will execute program prog1.ksh using Korn shell as theinterpretor.

It is always a better approach to specify the shell at the first line ofthe program:-

#!/bin/ksh

Reading from Keyboard

The most common practice of any programming is to

take input from user at runtime and act on that.E h d h ll h l

Page 295: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 295/340

p„Echo‟ and „read‟ statement in shell scripts help us toachieve that.

$ cat prog1.sh

Echo “Enter name:\ c”  

read p_name

Echo Hello $p_name

$ prog1.sh

Enter name: Suman

Hello Suman

Defining own variables

Defining global and local variables for programming is anindispensable practice.

One can declare it at the top of the program /anywhere at the

Page 296: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 296/340

time of usage.

Example Significance

g_val=20 Declares a variable : g_val and assignsa value 20 to it.

g_name=„two words‟  Declares a variable : g_name andassigns a value „two words‟ to it.Placethe value to be assigned within quotesif contains more than one words

Example Significance

g_val=“” 

g_val=„‟ g val=

Assigns null value to the variable g_val

Page 297: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 297/340

g_val=

let g_cnt=20 Declares a variable : g_cnt and assigns a value20 to it.

g_cnt=20readonly g_val Makes the value in the variable : g_cntreadonly. Further change in the value is notallowed.

unset g_cnt Wipes off the value from variable : g_val.

Special parameters used by Shell

Shell uses some special parameters which imparts usdifferent information on a program/command ,

various arguments passed to it, success/failure of theprogram etc.

Page 298: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 298/340

p g

Parameters Significance

$0 Name of executed program/command

$1,$2….  First, second etc arguments passed to a program

$* Complete set of positional parameters as a singlestring

$# Number of arguments passed in command line

$? Exit status of last command

Parameters Significance

$! PID of last background job

$$ PID of the current shell

Page 299: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 299/340

$_ Argument passed to the last command

Set – Manipulating Positional Parameters

The command : set can be used to manipulatedifferent positional parameters($1,$2…) from a string

/ output of a command

Page 300: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 300/340

$ set Happy birthday to you

$

$echo $1 $2

$ Happy birthday

$ set `date`

$

$echo $1 $3 $2 $6

$ Fri 19 Aug 2003

Shift- Shifting Positional Parameters

At a time, one can have values for 9 positionalparameters( $1 to $9). To shift their values one place

to the left, this command is used.

Page 301: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 301/340

$ set I know this is not a valid issue to discuss at themeeting before the board of directors

$echo $1 $2 $3 $4 $5 $6 $7 $8 $9$ I know this is not a valid issue to

$ shift; echo $1 $2 $3 $4 $5 $6 $7 $8 $9

$ know this is not a valid issue to discuss

Performing Calculations

Expr, echo let and (( )) comes handy

cnt_val=`expr $cnt_val + 1`

Page 302: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 302/340

cnt_val=`echo $cnt_val + 1|bc`

let cnt_val=cnt_val + max_limit

((z=x+y+2))

cnt=`expr $a \* 2`

a=`expr $rt / 2`

per=`expr \( $m1 + $m2 \) / 5`

echo

Displays strings , values in terminal or stores them infiles.

Examples Significance

Page 303: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 303/340

p g

echo Hello dear String : Hello dear shown in terminal

echo hello `hostname` String : Hello sb1dtl02 shown in terminal

a=256; echo $a The value of variable a is displayed interminal

string=happy; echo

“$string”>del 

Creates a file called : del or overwrites an

existing file , the content of the filebeing : happy

Echo “$string”>>del  Appends the contents o the variable$string at the end of the file : del

Escape sequences used with echo to format output interminal

Sequence Significance

\033[0m Normal display\033[1m Bold display

Page 304: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 304/340

\033[1m Bold display

\033[4m Underlined display

\033[5m Blinking display

\033[7m Display in reverse video\07 Beep

\b Backspace

\t Tab

\r Carriage return\c Positioning the cursor after the statement

tput

Formats display

Options Significanceclear Clears the screen

Syntax : tput [<options>]

Page 305: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 305/340

clear Clears the screen

cup r c Moves cursor to row r and column c

bold Bold display

blink Blinking displayrev Reverse display

smul Starts mode underline

rmul Ends mode underline

bel Bells the terminal

lines Echoes no. of lines in the screen

ed Clear to end of display

el Clear to end of line

Decision making – If

Making decisions based on conditions is the mostpopular practice in every programming language.

Shell provides decision building using “If” statements.

Page 306: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 306/340

If <conditions>

then

<actions>

fi

If <conditions>

then

<actions>else

<actions>

fi

If <conditions>

then

<actions>elif <conditions>

then

<actions>

else<actions>

fi

echo "Enter a value:\c"read val

if  test $val -gt 100

then

echo Value $val greater than 100elif test $val -ge 50 -a $val -lt 100

Page 307: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 307/340

elif  test $val ge 50 a $val lt 100

then

echo Between 50 and 100

elif  test $val -gt 0 -a $val -lt 50then

echo Between 50 and 100

else

echo No commentsfi

Test

Used to compare numerical values,strings,findproperties of different files etc.

Numericl test operators Significance

Page 308: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 308/340

-gt Greater than

-lt Lesser than

-eq(=) Equal to

-ne(!=) Not equal to

-ge Greater than or equal to

-le Lesser than or equal to

String test operators Significance

string1 = string2 True if string1 is same as string2

string1 != string2 True if string1 is not equal to string2

-n string True if string length >0-z string True if string length = 0

Page 309: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 309/340

g g g

while [ -z "$userid" ]

dotput cup 5 5echo "Enter userid:\c"read userid

done

if test $userid = superthen

while [ -z "$npswd" ]

do

tput cup 9 5

echo "Confirm password:\c"

stty -echo

read npswdstty echo

Page 310: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 310/340

tput cup 11 5echo No such userid allowedexit

fi

while [ -z "$pswd" ]dotput cup 7 5echo "Enter password:\c"stty -echoread pswdstty echo

done

done

if test $pswd = $npswd

thentput cup 11 5

echo o.k

else

echo wrong entry....fi

And,Or

echo "Enter city and state:\c"

Operators Significance

-a and

-o or

Page 311: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 311/340

read city state

if test $city = cal -a $state = wb

then

echo You stay in Calcutta and in West Bengal

elif test $city != cal -a $state = wb

then

echo You stay in West Bengal, not in Calcutta

elif test $state != wb -a \( $city = bom -o $city = del \)then

echo you stay in other metros

fi

Ex

ample

Case

Used to build logic depending on different values ofsame object.

Syntax

Page 312: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 312/340

Syntaxcase <variable> in

<value1>) <actions> ;;

<value2>) <actions> ;;

*) <actions>

esac

 echo "Enter an animal:\c"

read animal

case "$animal" in

[Tt]iger|[Ll]ion) echo He is the king;;

Crow|Sparrow|parrot) echo They are birds;;

Page 313: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 313/340

Crow|Sparrow|parrot) echo They are birds;;

[0-9]*) echo Name cannot start with numbers;;

?) echo You entered a single alphabet only;;

??) echo You entered two alphabets only;;[hH]ip*m*) echo Is it hippo?;;

*) echo Correct entry , but could not be identified

esac

Loop processing

Three common loops used in shell scripts are :-

1. While

Page 314: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 314/340

2. Until

3. For

While

Syntaxwhile <condition is true>

do

Page 315: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 315/340

<actions>

done

while [ -z "$userid" ]

do

tput cup 5 5

echo "Enter userid:\c"read userid

done

Until

Syntaxuntil <control does not return true>

do

Page 316: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 316/340

<actions>

done

echo "Enter number:\c"read no

until [ $no -gt 10 ] # till $no is not greater than 10

do

echo $no

((no=no+1))

done

For

Syntaxfor <variable> in <value1> <value2> <value3> …. 

do<actions>

Page 317: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 317/340

done

for city in bombay delhi calcuttado

echo $city is a metro city

done

Loop breaking statementsStatement Significance

break Control comes out of the loop completely

continue Does not proceed with current record further and startsprocessing the next record from the start of the loop

Page 318: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 318/340

n=20

while [ $n -le 100 ]

doecho $n

((n=n+1))

if test $n -eq 30

then

break

fi

done

n=0

while test $n -le 10

do((n=n+1))

if test $n -eq 5

then

continue

fi

echo $n>>del

done

Sleep

Syntax: sleep < number of seconds>

Idly waits for number of seconds specified. Mainlyused to display an event clearly to user

Page 319: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 319/340

used to display an event clearly to user.

Basename

Extracts the base filename from an absolutepathname

Example:-

Page 320: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 320/340

$ basename /home/ems2000/shell_scripts/ppc1.sh

 ppc1.sh

When used with second argument, it strips off thesecond argument from the first

Example:-

$ basename ppc1.sh .sh

 ppc1

The here document (<<)

Shell offers a mechanism of data to be read by a shell scriptto be read from itself rather than from another file. The hereoperator(<<) comes handy.

echo "Enter city code:\c"

Page 321: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 321/340

read city_code

grep "^$city_code" << stcity

01 Calcutta02 Bombay

stcity

if [ $? -ne 0 ] ; then

echo invalid codefi

Ex

ampl

e

Putting all the inputs at a timeA program may ask for different inputs several times and at

several points of execution.Instead of putting the inputs whenthey are asked for by the program, one can put all of them at a

time while calling the program for execution.

h “Fi \ " $ 8 h d

Page 322: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 322/340

echo “First name:\c"

read fname

echo “Middle name:\c"read mnameecho “Last name:\c"read lname

echo your full name is$fname $mname $lname

$ prog8.sh<<end

> soma

> sen> gupta

> end

First name:Middle name:Lastname:your full name is soma sen

gupta

Set -- : Help Command Substitution

Command „set‟ is often required with command substitution.

If the output of the command executed begins with „-‟( as with ls – l),

„set‟ interprets it as one of its options ,thus giving error messages. 

Page 323: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 323/340

By default, set displays all environment variables. This creates

problem when the argument in its command line evaluates to null

string ( for eg., in set `grep <srchstring> <filename(s)> ` statements.

IF the pattern cannot be located, set will operate with no arguments

and will display all the environment variables on the terminal.

To avoid this, use : set -- `command line` option.

echo "Enter city code:\c"

read city

set --`grep "^$city" <<tillend

01 Calcutta India WestBengal

02 Mumbai India Maharashtra

tillend`

Page 324: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 324/340

tillend

if [ $? -eq 0 ] ; then

echo "\n\tCity code : $1"

echo "\n\tCity name : $2"

echo "\n\tCountry : $3"

echo "\n\tState : $4"

else

echo Invalid code

fi

Command grouping

Group Significance

( ) Executes the commands placed inside „(„ and „)‟ separated by „;‟in a sub-shell under the current shell. They have the same

PID{ Same as above; but uses current shell only. The closing curly

b b l T h b h h b

Page 325: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 325/340

} brace must be on a separate line. To have both the braces onthe same line, terminate the last command with a „;‟ 

$pwd /home/ems2000

$ ( cd scripts;pwd )

 /home/ems2000/scripts

$pwd /home/ems2000

$pwd /home/ems2000

$ { cd scripts;pwd; }

 /home/ems2000/scripts

$pwd /home/ems2000/scripts

Conditional Execution

Group Significance

cmd1 && cmd2 Command cmd2 is executed only when

command cmd1 succeedscmd1 || cmd2 Command cmd2 is executed only when

d d1 f il

Page 326: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 326/340

command cmd1 fails

cmd1 || cmd2 && cmd3 Metacharacter coming first gets higherpriority

$grep ems2000 prog.sh && echo “String found”  

String found

$ grep ems99876 prog.sh || echo “String not found”  

String not found

String Handling

Syntax Significance

$ expr “<string>” : „.*‟  Finds the length of the string :<string>

$ expr “<string>” : „…\(..\)‟  Extracts the fourth and fifthcharacter from the string

Page 327: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 327/340

Example Significance

$ expr “Training” : „.*‟ 

8

Finds the length of the string :Training

$ expr “Subhendu” : „…\(..\)‟ he Extracts the fourth and fifthcharacter from the string

$expr “Anamoly” : „[^d]*l‟ 

6

Locates position of character „l‟within string : Anamoly

character from the string

$expr “<string>” : „[^d]*<char>‟  Locates position of character <char>in the string <string> 

Conditional Parameter Substitution

Syntax : {<variable> : <option> <stg>}

Option

s

Significance

+ Variable <var> evaluates to <stg> if <var> is defined and a non-

Page 328: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 328/340

Var a ar a uat s to stg f ar s f n an a nonnull string is assigned to it.

dir1=`ls`

echo ${dir1:+”This directory is not empty”} - Variable <var> is evaluated to <stg> if it is undefined or

assigned a null string

Echo “Enter directory to be copied:\c” 

read dirto

dir_child=${dirto:-/home/ems2000/scripts}

IF dirto is null or not set, dir_child=/home/ems2000/scripts.The value of dirto is still null

Options Significance

= If the variable <var> is null, it is assigned the string <stg>.

echo “Enter filename:\c” 

read flname

grep $pattern ${flname:=emp.lst}

IF flname is null, flname=emp.lst? Evaluates the parameter if the variable is assigned and non-null, otherwise it echoes the string following it. The shell is

Page 329: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 329/340

null, otherwise it echoes the string following it. The shell isalso terminated.

This option is useful in terminating a script if the user failsto respond properly to shell directives.

echo “Enter pattern:\c” 

read pattern${pattern:? “No pattern”} 

Shell variables($1,$2 etc) can also be used instead of <var>

Merging Streams

Usually, the inputs required for a program are typed in by userin terminal, all the outputs in terms of messages and all the errormessages ( shell genarated) are shown in the terminal .

But, one can make a program take inputs from a file, storeoutputs(messages) and / or error messages in a file, instead of

Page 330: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 330/340

Symbol Significance

0 Standard input

1 Standard output

2 Standard error

p ( g ) gdisplaying them in a terminal.

Example Significance$ prog2.sh > sclist All the output messages and prompts will be

taken into file : sclist. However, the inputs tothe program will be received from terminal.

$ cat prog2.sh

h " \ "

$ cat outlist

Enter pattern:

Page 331: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 331/340

echo "Enter pattern:\c"

read pattern

echo “\nPattern is $pattern"

$ prog2.sh > outlist # Enter pressed

Patternfromterminal # written from terminal$

Enter pattern:

Pattern is Patternfromterminal

$

Example Significance$ prog1.sh <namelist

All the inputs prompted by program prog1.sh aretaken from file : namelist

$ cat prog1.sh

echo "Enter first name:\c"read fname $ prog1.sh <namelist

Page 332: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 332/340

echo “\nEnter last name:\c"

read lname

echo “\nWelcome $fname $lname"

$ cat namelistSuman

Das

Enter first name:

Enter last name:

Welcome Suman Das

Example Significance$ prog1.sh < namelist > sclist All the inputs prompted by

program prog1.sh are taken fromfile : namelist and all the outputmessages are stored in file : sclist

$ cat prog1.sh

h "E t fi t \ "$ prog1.sh <namelist >sclist

Page 333: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 333/340

echo "Enter first name:\c"

read fname

echo “\nEnter last name:\c"

read lname

echo “\nWelcome $fname $lname"

$ cat namelist

Suman

Das

$ cat sclist

Enter first name:

Enter last name:Welcome Suman Das

Example Significance$ 1>&2 Send the standard output to the destination of standard

error 

$ cat prog2.sh

ans=ywhile [ "$ans" = "y" -o "$ans" = "Y" ]

$prog2.sh > out.1

Enter city:calcutta

More(y/n): y

Page 334: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 334/340

do

echo "\n Enter city:\c" 1>&2

read city

echo "$city"

echo "\n More(y/n):\c" 1>&2

read ans

done

M (y/ ) y

Enter city:bombay

More(y/n):n

$cat outlist

calcutta

bombay

Example Significance$ 2>&1 Send the errors to the destination of standard output 

$ cat prog3.sh

echo "Searching all the .kklfiles"

l lt * kkl

$cat out.2

Searching all the .kkl files

* kkl not found

Page 335: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 335/340

ls -ltr *.kkl

$prog3.sh > out.2*.kkl not found

$ prog3.sh>out.2 2>&1$ 

$  prog3.shSearching all the .kkl files

*.kkl not found

$cat out.2Searching all the .kkl files

.kkl not found

Using subroutines

Using subroutines is one of the most popular practice in anyprogramming language.

Codes in a subroutine does a piece of job. It can take inputs froman user, do some jobs and can return some value to the callingprogram.

Page 336: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 336/340

Subroutines are used to:-• Modularize program sections.

•Perform repeatitive jobs.

•Do Calculations.

Syntax:-<subroutine_name> {

<actions>

}

function <function_name> {

<actions>

}

Page 337: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 337/340

}

Inputs to a subroutine are identified within the subroutine as :

$1,$2,$3 …etc. 

A subroutine is called from a program as :-

<subroutine_name> [ <value1>] [ <value2> …] 

Subroutines are always declared at the top of a program

$ cat prog0.sh

show_name() {

case "$1" in

[bB]) tput boldecho "$2";;

Passing values to subroutines

Page 338: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 338/340

[lL]) tput rev

echo "$2";;

*) tput rmsoecho "$2"

esac

}

show_name b `whoami`

show_name l `hostname`

show_name n `uname -r`

show_name n “The End” 

$  incr_val() {

sum=`expr $1 + 1`

echo $sum}

Subroutine returning values

Page 339: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 339/340

no=12

no=`incr_val $no`echo value is $no

$ prog2.sh

value is 13

$  function show_name {grep "$1" *.kshif [ $? -eq 0 ] ; thenreturn 1

elsereturn 2Fi

Page 340: Unix Training by Dhanabal

8/21/2019 Unix Training by Dhanabal

http://slidepdf.com/reader/full/unix-training-by-dhanabal 340/340

Fi}

E