Upload
krsrajesh1
View
93
Download
4
Tags:
Embed Size (px)
DESCRIPTION
this is useful for product support jobs.all the commands of unix are explained in detail.
Citation preview
This is not an exhaustive training on Unix , covering This is not an exhaustive training on Unix , covering all the arenas with full details.all the arenas with full details.
Even the topics covered in this presentation are not Even the topics covered in this presentation are not all inclusive.all inclusive.
But, it will definitely help you to be a confident But, it will definitely help you to be a confident sailor in the ocean of Unix sailor in the ocean of Unix –– no matter what the no matter what the circumstance is.circumstance is.
Expectation from this training
This training will teach you aboutThis training will teach you about 9090 commands!!!commands!!!
And will make you bore with more thanAnd will make you bore with more than 350350 slides!!!!slides!!!!
But donBut don’’t be nervous t be nervous –– remember the utilities of the remember the utilities of the commands commands –– for this presentation will be with you for this presentation will be with you for the rest of your life to provide you the syntax for the rest of your life to provide you the syntax for using them.for using them.
Don’t be nervous,please
Purpose
To get acquainted with the basic Unix commands
To learn a bit of Shell Programming
Get introduced to Unix Operating System
Operating SystemAn operating system is a living, breathing software entity. The soul of the computing machine, it is the nervous system that turns electrons and silicon into a personality. It brings life to the computer
- from Mike Gancarz's "The UNIX Philosophy"
The OS sits between users and hardware providing translation services. It speaks the language of the hardware to perform basic tasks such as the definition of memory or the allocation of disk space using the hardware.
Unix Operating System
In UNIX, the operating system is broken into three pieces: the kernel, the shell, and the built-in utilities. The kernel is responsible for low level hardware communication, the shell provides human users with a user-friendly interface, and the built-in utilities provide basic tools for doing work.
Hardware
Kernel
Shell
•Heart of The Unix OS.
•Collection of C programs directly communicating with hardware
•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
•Human interface point for Unix
•Program layer – provides an environment for the user to enter commands to get desired results.
•Korn Shell, Bourne Shell, C Shell are various shells used by Unix users
Shell
Exit
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
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 an active open terminal to ‘init’.
‘init’ calls program ‘getty’, which issues a login prompt in the monitor
User enters login and password
‘getty’ calls program ‘login’ which scans file /etc/passwd to match username and password
After validation, control passes to session startup program /bin/sh , session startup program
Program /bin/sh reads file /etc/profile and .profile and sets up system wide and user specific environment.
User gets a shell promptExit
Understanding Commands
General Purpose Utilities
Handling Files
Filters
Controlling Environment
Process
System Administration
Communication
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
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
Independent CommandsDo not require any input for execution$pwd
/home/ems2000
Dependent CommandsRequire input for execution
$type ls
ls is a tracked alias for /usr/bin/ls
Mixed Commands:- Can work independently/dependently
$ls
input queue sentwebcatalogs web
$ls queue
queue
General syntax to use commands<command> [ <option flag> ] [<arguments>]A statement asking for execution of a command usually consists of three 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 the command will act upon for desired output.
Example :
$grep -l “ems2000” *.shThe above command from the OS prompt searches for the string “ems2000” in all the files with extension : .sh and shows the name of the files containing it.
More than one commands can be stated from OS prompt 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
which
Shows the name of the program related to the command
Syntax : which <command>
Example :
$ which ls
/usr/bin/ls
Other similar commands are : type <command>
whence <command>
manDisplays the online manual page for a given command, file, subroutine etc.
Provides a list of summary info about the manual sections in the keyword database for specified keyword <keyword>
man –k <keyword>Provides one line details about the commandman –f <command>Shows the manual help page for the commandman <command>SignificanceUse
aliasShort hand notation of one/more than one commands
Syntax : alias [<alias_name>=<value>]
Example:-$alias sdf=‘ls ;ls|wc –l’$$sdfinput queue sentwebcatalogslogfiles sap webpricelist_feed scripts webcatalogs9
To unalias : unalias <alias_name>
history
•Korn shell command.
•Shows an automatic recording of the commands entered, kept as a numbered list in a special disk file in the home directory and preserve it from login session to session.
•Commands from the history list can be re-executed using the tracking number.
Can be used for:-
Viewing the previously executed commands
Re-submit a previously executed command from the list using r command
Modify a previous command in the list and execute it
Environment variables:-
HISTFILE : File name of Korn shell history file
HISTSIZE: Integer number containing the maximum number of commands to be retained in history list. Default is 16 for Korn shell
Executes the nth last command executedr -nRe-execute the last vi commandr vi
Shows from command number 5 onwards in history list
history 5Shows last 5 commands from history listhistory –5
Shows recent ls command and from that line onwards in history list
history ls
If the earlier vi command was : vi prog1.sh , then the command to be executed is vi prog2.sh
r prog1.sh=prog2.sh vi
Re-execute the command in line number n in history list
r n
Shows from command number 5 onwards in history list
history 5
Shows last 16 lines of history in Korn shellhistorySignificanceUse
General Purpose UtilitiesBy 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 by a command
banner
The word ‘Hello’ will be shown in one line and ‘Jim’ in the next line as poster
Banner ‘Hello’ ‘Jim’
In a single line, ‘Hello Jim’ will be shown as a posterBanner ‘Hello Jim’SignificanceUse
Syntax : banner <text>
Creates posters by blowing up its arguments on the screen. Each line can display a maximum of ten characters
cal
Shows calendar for June 2000cal ju 2000Shows calendar for February 1999cal f 1999
Shows calendar for December 1995cal dec 1995Shows the calendar for February 2003cal 2 1997
Shows calendar for January 1998cal ja 1998
Shows calendar for the year 2003cal 2003SignificanceUse
Syntax : cal [ <mon>] [ <4 digit year>]
Print the calendar for a year / for a particular month of a year
date
Abbreviated weekday name ( eg., ‘Wed’)date +”%a”
Sets the Unix system clock to the date and time specified
date [mmddhhmm[yy]]
Shows the current date as a combination of :-Three letter word for day of the week/ Short abbreviation for the current month/Day of the monthTime in hh:mm:ss/Time zone/4 digit year$dateWed Sep 10 02:06:33 MDT 2003
dateSignificanceUse
Syntax : date [ +”%<flag for formatting options>]
Shows the current date in different formats/sets the current date
Full weekday name(eg., ‘Wednesday’)date +”%A”
Full month name(‘Jan’)date +”%b”Current date & time representationdate +”%c”
AM or PMdate +”%p”Newline characterdate +”%n”Current minute of time(00-59)date +”%M”Current Month as decimal two digit numberdate +”%m”Current hour of time(12 hour clock)date +”%I”
Current Day of the monthdate +”%e”Current Day of the monthdate +”%d”
Current hour of time(00-23)date +”%H”
Century(year/100, truncated to nearest number)date +”%C”
SignificanceUse
Current time as %H:%Mdate +”%R”Current second of time(00-59)date +”%S”Tab characterdate +”%t”Weekday as 1 digit number[1-7 for Monday-Sunday]date +”%u”
Time zone namedate +”%Z”4 digit yeardate +”%Y”Two digit yeardate +”%y”
Current date as dd/mm/yydate +”%x”Week number of the yeardate +”%V”
Current time in hh:mm:ssdate +”%X”
Weekday as 1 digit number [ 0-6 for Sunday-Saturday]
date +”%w”
SignificanceUse
calendar
Provides a useful reminder mechanism for the user. It is a sort of engagement diary.
It searches for the file ‘calendar’ in the current directory for lines containing any date equal to current date or next working date. Identifies numeric dates as : mm/dd/yy.
Other date formats identified are:- <Mon> <dt>
<Mon> <dt> , <year>
$ 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
who
Produces single line of output pertaining to the user who invoked the command
who am I
Produces five column output with column headers at the top. The fourth column shos how many minutes the user is idle, fifth column shows the PID of the parent shell for the user.
who –Hu
Produces a three columnar output on the users logged in:-1st column shows the userid , 2nd column shows their terminal no. and third one shows the date and time of logging in
who
SignificanceUse
Show the other persons logged into the system.
$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
uname
Show the version no of the Operating systemuname –r
Shows the name of the Unix server being used in the network
uname –n
SignificanceUse
Know the name of the machine/Version no of the Operating System
Similar command : hostname
uptime
Print only the first line describing the overall state of the system. This is the default for the uptime command.
-u
Use long output. This option assumes the use of the –w option to uptime.
-l
Suppress the first line and the heading line. This option should not be used with the -u option. This option assumes the use of the -w option to uptime.
-h
SignificanceFlag
Displays the current time, the length of time the system has been up, the number of users logged on to the system, and the average number of jobs in the run queue over the last 1, 5, and 15 minutes for the active processors.
Syntax : uptime [-hlsuw] [user]
$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
login
Starts terminal session.
Used at the beginning of each terminal session to properly identify a prospective user
Can be invoked as an user command also. When used as a command, the previous terminal session is replaced. Prompt for userid and password appears.
Syntax : login <server name| IP address>
telnet
Helps to connect to another Unix server connected to the same network, without being logged out of the current environment.
If one does not specify a server, a prompt ‘telnet>’ appears. One can use a number of commands from that prompt.
Syntax : telnet <server name | IP address|alias>
Exits telnetquit
Returns to ‘telnet>’ prompt after closing connection to the host
exit
Opens a connection to the useropen <hostname|IP address>
SignificanceCommands
lock
Helps to lock an open terminal to prevent other people gaining access
When invoked as a command, password has to be entered and re-entered to confirm it
To unlock the terminal to use it, the user has to use this password
A locked terminal remains locked for default time of 30 minutes after which it logs out. To change the default time:-
$lock –n
(Terminal remains locked for n minutes, after which it logs out)
Syntax : lock [-<minutes to lock after logout>]
script
Helps to record login session in a file called : ‘typescript’ in the current directory. All the commands, their output and error messages are stored for later view.
After starting the scripting, user continues with his job. All the commands he uses, their output and error messages are stored forlater view.
When the user exits from the scripting(writing : exit from OS prompt), the script file is saved and a message is shown:-Script done, file is typescript
Syntax : script [ -a] [<newfile>]
To log activities to a new file : newfilescript newfileAppend activities to existing file : typescriptscript –aSignificanceUses
bcBinary Calculator for Unix.
$bc
12 + 5
17
^d
$
$bc
12*5
60
^d
$
$bc
20/5
4
^d
$
$bc
12 - 5
7
^d
$
$bc
2^3
8
^d
$
Addition Subtraction Multiplication Division Exponential
Scale:-By default, bc performs truncated division.One have to set scale to the 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 as dictated by the scale variable, then the value dictated by the scale is ignored and the real value is shown.
ibase and obase:-By default, the input and output are interpreted as decimal values.But, if the demand required input and/or output in different number system(binary, hexadecimal), variables ibase and obase are set.
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.
Handling Variables:-Variables can be used in bc mode and values can be assigned to them.
$bc
X=12 ; y=19
Z=x+y
Z
31
Conditional logics(if) ,loops(for,while), arrays, functions are supported by ‘bc’
Square Root of a NumberSyntax : sqrt ( x )
Example :
$bc
Sqrt ( 4 )
2
Length of a NumberSyntax : length ( x )
Example :
$bc
length ( 1234.5678 )
8
Trigonometric Functions To use trigonometric functions of bc, one have to include math library. For that , issue the following command from Os prompt:-
$ bc –l
Various trigonometric functions available with ‘bc’ are:-
s(x) sine
c(x) cosine
e(x) exponential
l(x) log
exprAllows to perform calculations and handle strings
$expr 100 + 50
150
$expr 100 – 50
50
$expr 100 \ * 2
200
$expr 12 / 3
4
$expr 12 % 5
2
$z=`expr 100 – 50`
50
Calculations
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
factorFinds out factor of the integer provided
Syntax : factor <number>
$factor 15
15
3
5
$
$factor 18
18
2
3
3
$
primesShows all prime numbers between integers <lower value> and <upper value>. If upper value is not provided, it is considered to be 2,147,483,647.
Syntax : primes <lower value> <upper value>
$primes 0 10
2
3
5
7
$
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-01
For a complete list of units, examine the file:
/usr/share/lib/unittab
tputControls screen display
Starts reverse displaytput smso
Ends reverse displaytput rmso
Echo bell charactertput bel
Shows number of lines in the screentput lines
Shows number of columns in the screentput cols
Reverse displaytput rev
Bold displaytput boldBlink displaytput blink
Moves cursor to row <r> and column <c>tput cup <r> <c>Clears the screentput clearSignificanceOptions
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>
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
Handling FilesThis 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,line and 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
Handling Files…contd
‘mkdir’ – Create Directory
‘rmdir’ – RemoveDirectory
‘cmp’ – compare two files
‘comm’ – Comparetwo files
‘sdiff’ – find differences between 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
File maintenance architecture
Formatted Disk
Partition Partition Partition
File system
Directory Directory Directory Directory
Directory Directory
File File File
Types of Files
Normal filesCan be text/binary files.
Can be a text file, compiled source code , executables
Directory filesContains no external data, but details of files and sub-directories it contains.
Device filesPrinters, tapes, floppy drives, CD ROMs, hard disks, terminals – all are considered as files
Inode
•Inode is a fixed format structure containing the attributes of the files stored in the file system.
•Every file has one inode, and a list of such inodes is kept in a disk area not directly accessible by user.
•Each inode is accessed by inode number, specifying the position of the inode in the list.
Some of the important information that inode contains are:-
Time at which the inode structure was last modified
Inode time
Time of last modification of the fileMod time
Time of last access of the fileAccess time
No. of bytes in the fileSize
ID of the groupGroup Id
ID of the ownerUser id
No. of links associated with the fileLink count
Permission mask and type of fileMode
SignificanceInformation
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 all users
/usr/usr1 is the home dir. for user : usr1
/tmp contains temporary files
/usr/bin contains additional binary unix commands
/etc contains binary executable files for system administration
cdNavigates from the current directory to another directory
Syntax : cd <new directory specification>
Change to the directory whose absolute pathname is : /usr/fin/subhendu/ manfiles
cd /usr/fin/subhendu/ manfiles
Move to home directory of the usercdcd ~
Change to directory program residing in the current directory's parent directory
cd ../program
Moves to directory : scripts under current working directory
cd scriptscd ./scripts
SignificanceCommands
Move to folder : subhendu under the home directory for the user
cd ~/subhendu
Move to the parent directory of the current working directory
cd ..SignificanceCommands
cdpathThis is an environmental variable which specifies the list of directories to be searched when an user issues a ‘cd’ command.
$CDPATH=.:..:$HOME
This means, when a ‘cd’ command is issued, search for the new directory first in current working directory(.)
If not found, move to the parent directory of the current directory and search there.
If, still, not found, search for the directory under user’s home directory
lsShows the contents of a directory/existence of specific files with their attributes
Syntax : ls [ flag] [string for filename]
Long listing showing seven attributes of a filels –l
Marks executables with ‘*’ and directories with ‘/’ls –F
Shows all files and recursive listing of all files in sub-directories
ls –R
Multi columnar outputls –xLists all files including those starting with ‘.’ls –a
Shows the name of all the files and directories under the current directory, excluding those starting with .
lsSignificanceCommands
Lists all files with first letter as ‘d’ and third letter as ‘l’
ls d?l*
Shows the files with names not starting with vowelsls [!aeiou]*
Lists all files starting with ‘d’ and ending with ‘.sh’ in their name
ls d*.sh
Shows the files with name starting with vowelsls [aeiou]*
Shows the name of all files with ‘.ksh’ at the end of their name
ls *.ksh
Shows inode number of a filels –i
Shows long listing of files with their attributes, sorted in reverse order by access time(most recently edited file comes last in the list)
ls –ltr
Sorts file in reverse orderls –rSorts files by access timels –ut
Sorts files by modification time – the file modified most recently comes at the top
ls –tSignificanceCommands
catCreates , shows, concatenates,copies files
Concatenates the content of file1 and file2 and places it into new or existing file file3
cat file1 file2 > file3
Appends the contents of file1 after the last line of file2. If file2 does not exist, new file is created
cat file1 >>file2
Copies the contents of file : file1 into new or existing file : file2
cat file1>file2
Shows the contents of the file: file1cat file1
Append lines to existing content of file : file1 and is ended when <Ctrl-D> is pressed
cat >>file1
Creates file file1 where a user enters text and presses <Ctrl-D> to end text editing
cat >file1SignificanceCommands
Prints tab character as ‘^I’ and form feed character as ‘^L’
cat –t file1
Suppresses error and does the job. If file file2 does not exist, the command will copy the contents of file1 into file3
cat –s file1 file2 >file3
Replaces multiple consecutive empty lines with one empty line
cat –r file1
Prints ‘$’ at the end of each linecat –e file1
Displays the contents of file : file1 with line number for all lines excepting the blank lines
cat –b file1
Displays the contents of file : file1 with line number
cat –n file1SignificanceCommands
Haters say that it is the worst thing ever happened in the Unix world.
Lovers are totally biased towards it and would go any length 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
Modes of Operation
First session with vi
Append mode
Command mode
Ex mode
Environmental variables
.exrc & EXINIT variable
view
Mode of Operation
Command mode : Default mode when a file is opened using vi. All the keys pressed by the user are interpreted as user commands
Append Mode : Permits insertion of new text, editing existing texts.
Ex mode : Permits commands at the command line(last line of the screen)
Step 1 : Create a new file by typing the following command from the OS Prompt : vi newfile
•vi clears the screen and display a window.
•The ‘_’ on the top line indicates that the cursor is waiting for commands
•Every other line starts with ‘~’, symbol for empty line.
Step 2 : Press ‘i’ to enter into Append mode. Add text to the file
Step 3 : Press <Esc> key to return to command mode
Step 3 : Press ‘:’. The cursor takes to the ‘ex’ mode at the command line. Enter ‘wq’ and press enter.
Append Mode
Opens a line immediately below the current line in input modeo
Opens a line immediately before the current line in input modeO
Appends text at the end of the current line.A
Appends text from the right of the current cursor positionaAppends text at the start of the current line.IAppends text from the left of the current cursor positioniSignificanceCmd
Inserting Texts
Replaces <n> lines from the current cursor line with entered text<n>cc
Changes from current cursor position to end of the file with entered text.
cG
Change from current cursor posn. to end of line with the text enteredC
Changes <n> words from the current cursor position with text entered<n>cw
Replaces <n> lines from the current cursor line with entered text<n>S
Changes from cursor to end of line with the text enteredc$Changes from cursor to beginning of line with the text enteredc0
Replaces <n> characters from cursor with entered text<n>sReplaces text from cursor to rightRReplaces <n> characters from current cursor posn. with inserted text<n>rSignificanceCmd
Changing Texts
Command Mode
Saves the work done in the file and quits editing by vi editorZZSignificanceCmd
Saving work in a file and quit
Deleting texts/lines
Deletes from cursor upto the first occurrence of string <pattern> in backward direction
d?<pattern>
Deletes from cursor upto the first occurrence of string <pattern> in forward direction
d/<pattern>
Deletes from current cursor position to first occurrence of character <char>
df<char>
Deletes <n> characters from current cursor position<n>x
Deletes from current line to line no <n>d<n>GDeletes from current cursot position to end of filedGDeletes from current cursor position to end of lined$Deletes <n> lines counting from current cursor line to below<n>dd or <n>D
SignificanceCmd
Moving/Copying Texts
Yank from current cursor position to end of the file in undo buffer
yG
Yank from current cursor position to end of the line in undo buffer
y$
Yank <n> words starting from current cursor position onwards into undo buffer
<n>yw
Delete text into named buffer b“bd
Yank <n> lines starting from current line onwards into undo buffer
<n>yy or <n>Y
Paste the contents of buffer a after the cursor position“ap
Paste the contents of undo buffer( as a result of deleting or yanking) before the cursor position
P
Paste the contents of undo buffer( as a result of deleting or yanking) after the cursor position
p
Yank <n> lines starting from current line onwards into buffer named a
“a<n>yy
SignificanceCmd
Navigation in same line
Move the character to one column after the next character <ch> on same line
T<ch>
Move the character to one column before the next character <ch> on same line
t<ch>Move the character to the prv. character <ch> on same lineF<ch>
Moves cursor right to end of nth next word w.r.t the current cursor position. Punctuation marks are taken into account.
<n>e
Moves cursor right to start of nth next word w.r.t the current cursor position. Punctuation marks are taken into account.
<n>w
Moves cursor left to start of nth previous word w.r.t the current cursor position. Punctuation marks are taken into account.
<n>b
Moves cursor right to nth next character w.r.t. the current cursor position
<n>l
Repeats search in the same direction along which the prv. Search was made using f/t
;
Move the character to the next character <ch> on same linef<ch>
Moves cursor left to nth previous character w.r.t. the current cursor position
<n>hSignificanceCmd
Moves cursor left to start of nth previous word w.r.t the current cursor position.Punctuation marks are ignored
<n>B
Moves cursor right to start of nth next word w.r.t the current cursor position. Punctuation marks are ignored
<n>W
Moves cursor right to end of nth next word w.r.t the current cursor position. Punctuation marks are ignored
<n>E
Moves to 1st non-space character of the line^Moves to last character of current line$ or )Moves to 1st character of the current line0 or (Moves the cursor to specified column <n><n>|
Repeats search in the opposite direction along which the prv. Search was made using f/t
,SignificanceCmd
Navigation across lines
Move the cursor up to the <n>th prv line in the same column<n>k or <n>^p
Moves the cursor to previous line’s first non-blank character-
Moves to line number <n><n>GMoves the cursor to the middle line of the screenMMoves the cursor to the last line of the screenLMoves the cursor to the top line of the screenH
Moves the cursor to next line’s first non-blank character+
Move the cursor down to the <n>th next line in the same column<n>j or <n>^n
SignificanceCmd
Redraw screen
Makes the current line the middle line of the screenand redraws the screen
z.
Makes the current line the first line of the screenand redraws the screen
z+
Find the next occurrence of <pattern> and make that last line ofthe screen
/pattern/z-
Redraws the screenCtrl-l
Makes the current line the last line of the screenand redraws the screen
z-SignificanceCmd
Scrolling across pages
Scroll window up by <n> lines<n>^yScroll window down by <n> lines<n>^eMove backward by <n> number of half screens<n>^uMove forward by <n> number of half screens<n>^dMove backward by <n> screens<n>^bMove forward by <n> screens<n>^fSignificanceCmd
Pattern searching
Positions the cursor <n> number of lines before the line where the specified <pattern> is found
/pattern/-<n>
Repeat the last search in the opposite direction as was specified in the last search
N
Searches for specified <pattern> backward.?pattern
Find the matching braces or parenthesis%
Positions the cursor <n> number of lines after the line where the specified <pattern> is found
/pattern/+<n>
Repeat the last search in the same direction as was specified in the last search
n
Searches for specified <pattern> forward. IF end of file is reached, search wraps around.
/pattern SignificanceCmd
Joining lines
Joins current line and <n> lines below it together to form a single line
<n>JSignificanceCmd
Undo changes
Undo last changeuUndo all the changes in the current lineU
SignificanceCmd
Marking text
Moves to portion of the file marked with <char>‘<char>Toggle to most recently marked location“
Marks position of the file with mark <char>m<char>SignificanceCmd
Restoring previously deleted line
Paste the content of <n>th last delete ( n<=9)“<n>pTill the last change is found“1pu.u.u…
SignificanceCmd
Filtering texts
Translates all the characters from current line to line <n> to uppercase
!<n>G tr ‘[a-z]’ ‘[A-Z]’
Translates all the characters of current line to uppercase
!! tr ‘[a-z]’ ‘[A-Z]’
Sort from current line to line no. <n>!<n>G sortSignificanceCmd
Inserts <ch> character <n> number of times in input mode at a stretch
<n>i<ch>SignificanceCmd
Repeat factor
Miscellaneous in command mode
Repeat the last action ‘n’ times<n>.Shift current line to shift width character left<<Shift current line to shift width character right>>
Repeat the last change.
Change the character under cursor from lowercase to uppercase and vice versa
~SignificanceCmd
Options available with vi command
Opens file <filename> in vi editor and places the cursor at first occurrence of pattern <pattern>
vi +/<pattern> <filename>
Opens the file <filename> with cursor at the last linevi + <filename>
Open the file <filename> in read-only modevi –R <filename>
Opens encrypted file <filename> in vi mode and asks for the password before opening that
vi –x <filename>
Opens file <filename> in vi mode with window size of <n> number of lines
vi –w<n> <filename>
Opens the file <filename> with cursor positioned in line number <n>
vi +<n> <filename>
Recover the file <filename> as much as possible after system crash and open it
vi –r <filename>SignificanceCmd
Ex ModeSaving work in a file
Moves the contents of lines <n1> to <n2> into an existing file <newfile> , overwriting its previous contents
:<n1>,<n2>w! <newfile>
Save the changes made to the file:w
Copies the contents of lines <n1> to <n2> into a new file <newfile>
:<n1>,<n2>w <newfile>
Append the contents of the opened file after the last line of the file <file1>. File <file1> should exist previously
:w >> <file1>
Save the changes to file <filename>, if the file already exists
:w! <filename>
Same as “Save As..” in windows. Saves the contents to the specified file <filename> . If it does not exist previously, a new file is created
:w <filename>
SignificanceCmd
Appends from current line to <n> number of lines below it into file <newfile>
:.,.+<n>w <newfile>
Appends from current line to <n> number of lines below it after the last line of the file <nextfile>
:.,.+<n>w >> <nextfile>
Quits the file editing in vi, provided no unsaved change remains
:q
Quits vi neglecting all the unsaved changes made to the file
:q!
Save the unsaved changes in the opened file and quit vi editor
:wq or :x
SignificanceCmd
Temporary exit to shell
Temporarily allows the user to come out of the vi file and use the shell. After the job of the user is done and command : exit is triggered from OS prompt, control returns to vi editor again
:shSignificanceCmd
Navigating to desired line
Custor moves to line number <n>:<n> SignificanceCmd
Search and replace texts in ex mode
Syntax :- :<line address>s/<old pattern>/<new pattern>/g
First to last line1,$From current cursor line to <n> number of lines downwards.,.+<n>
All lines where matching pattern is found%Current line.Refers from line <n1> to <n2><n1>,<n2>Last line$
SignificanceLine address
Examples of Search and Replace
Replaces ‘0’ with ‘*’ at all lines having ‘9’ after 9th
position:g/.\ {9\ }9/s/0/*/g
Append the string ‘To be done’ at the end of all lines not containing the string ‘complete’
:g!/complete/s/$/To be done/
Substitutes 1st occurrence of string ‘ex’ with ‘vi’by showing them and asking for confirmation. When each string will be shown with pause in cursor, press ‘y’ for substitution
:%s/ex/vi/c
Replaces ‘amaze’ , where available as a full word, with ‘delight’. Note, any word like ‘amazed’ will not be replaced
:%s /<amaze \ >\/delight/g
Replaces every occurrence of string ‘majumdar’with ‘mazumder’ on all lines containing the pattern ‘subhendu’
:g/subhendu/s/majumdar/mazumder/g
Delete all blank lines:g/^$/d
SignificanceExample
Append the string ‘editor’ at the first of all lines containing the string ‘vi’
:g/vi/s/^/editor/
Appends the string ‘: see my note’ at the end of all lines
:%s/$/ : see my note/g
Deletes all lines with ‘o’ as 3rd character:g/^..o/d
Delete the last three character of every line:%s/…$//g
Deletes all lines containing 4 letters:g/^….$/d
SignificanceExample
Reading below the current line in the vi editor
Reads the contents of the file <nextfile> below current line
:r <nextfile>
Places the output of the command <command> below the current line
:r! <command>
SignificanceCommand
Editing another file
Edit starts at line <n> of file <nextfile>:e +<n> <nextfile>Edits first file in the command line:rew
Stops editing the current file; leaves the current file and starts editing file <nextfile>; provided there are no unsaved changes in the current file
:e <nextfile>
Edits file <nextfile> abandoning all the changes done to the current file
:e! <nextfile>
Loads last saved version of current file:e!Edits next file mentioned in the vi queue:n
SignificanceCommand
Abbreviating texts
When the user writes the string <short_string> in input mode, the <long_string> is written
:ab <short_string> <long_string>
SignificanceCommand
Mapping
When you position your cursor to any character in a line and press ‘z’ , the line will be broken from that point and two lines will be formed. The control will remain in command mode(^[ represents <Escape> key)
:map z i^M^[
Pressing ‘z’ in command mode saves the file and executes it in one shot
:map z :w^M:!%^M
Pressing ‘g’, one wants to save the file(:w is for saving, and ^M is for pressing Enter key. While writing it in the command line, write ^V^M)
:map g :w^M
SignificanceCommand
To unmap a key, write at the command line :unmap <key>
Miscellaneous
Executes the command <command> remaining in vi editor
:! <command>
Shows the name of the current file and line:fSame as :f^g
SignificanceCommand
Setting environmental variables for vi
Works in ex mode.
To set an environment variable to customize vi, the following syntax needs to be followed:-
:set <env.variable> [= <value>]
Sounds the bell when error occurs. Opposite is ‘noeb’errorbells(eb)
System messages allowed when vi is running. Opposite is ‘nomesg’
mesg
Displays line numbers. Opposite is ‘nonu’number( nu )
Displays special characters in the screen: tabs are shown as ^I, end of line are marked with ‘$’. Opposite is ‘nolist’
list
Allows an .exrc file in the current directory to override the .exrc file in user’s home directory. Opposite is ‘noex’
exrc(ex)
Newly inserted lines of text are indented to the same distance from left margin as the preceding line. Opposite of this option is noautoindent(noai)
autoindent(ai)
Automatically saves the unsaved changes in a file before opening the next file with :n or using a shell command with :! <command>. The opposite to this option is noautowrite(noaw)
autowrite(aw)
SignificanceEnvironment variables
Ignores case when searches patterns. Opposite is ‘noic’
ignorecase(ic)
When set to a value >0 , carriage returns are inserted automatically when the cursor gets to within that number of spaces from the right edge of the screen
wrapmargin(wrm)=<val>
Number of spaces to be used for backtabs/<</>>shiftwidth(sw)=<val>
No. of spaces the tab character moves overtabstop=<val>Indicates type of mode showmode
Shows match for ) or } . Opposite is ‘nosm’showmatch(sm)
When any operation affects more lines than this settings, message is displayed
report=<val>
Number of screen lines to scrollscroll=<val>
SignificanceEnvironment variables
.exrc file and EXINIT system variable
One can store all the values for environment variables, all the key mappings and all the abbreviations in a file ‘.exrc’ under the home directory for the user.VI looks for this file on startup and executes the instructions as ex mode commands.
Besides, there is also a system variable , ‘EXINIT’ which can also be used to save the settings.
EXINIT=“set report=5 ignorecase ai”; export EXINIT
view
Description :Shows the file <filename> in vi mode. File remains read-only. No changes done to the file cannot be saved.
Syntax : view <filename>
more
Filter for examining continuous text, one screenful at a time. It pauses after each screenful,printing the filename at the bottom of the 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]
Squeeze multiple blank lines from the file , showing only one blank line
-s
Start listing such that the current position isset to two lines above the line matching the regular expression pattern.
+/pattern
Prompts user with message :- ‘Press space to continue, q to quit, h for help’ at the end of each screenful of display
-d
Draw each page by starting at the top of screen, and erase each line before drawing on it
-c
Performs case independent pattern matching-i
Sets the no of lines in the display window to that <number>.-n <number>
Same as above-<n>
SignificanceCommand line option
Moves to next file specified in the command line:nRepeats prv. command.Goes to last line of the fileGMoves to line number <n><n>G
Searches pattern <pattern> backward?pattern
Scrolls backward one screenb
Scrolls forward by <n> number of lines<n>j
Searches pattern <pattern> forward/pattern
Repeats last searchn or NOpens vi mode for the file viewedv
Scrolls forward one screenf
Quitq
SignificanceOptions
Internal commands in ‘more’ mode
Moves backward by <n> lines<n>b / <n>^b
Scrolls backward i lines, with a default of ½ of the screen size<i>^u /<i>u
Scrolls backward by <n> number of lines<n>k/ <n>^y
Scrolls forward by i number of screensi<space>
Writes the name of the file currently being examined, the no. relative to the total number of files to be viewed, the current line no,current byte no, total no of bytes to write and what % of the file precedes the current position
^g
Scrolls forward i lines, with a default of ½ of the screen size<i>^d/ <I>
Moves forward by <n> lines<n>f / <n>^f
Displays <n> more lines and sets the window size to <n><n>z
Moves to previous file specified in the command line:p
Scrolls forward by i number of linesi<return>
SignificanceOptions
Refreshes the screenr / ^lRepeats the prv command.
Invoke a shell with a command <command>!<command>
Examines the <I>th next file specified in the command line
<I>:n
Returns to the position previously marked by the specified letter <ch>
‘<char>Marks the current position with letter <char>m<char>
Display a descriptive list of all the more commandsh
Leaves the current file and starts viewing a new file <newfile>
:e <newfile>
SignificanceOptions
pgDisplays the content of a file one page at a time
Syntax :pg < flag and string> <filename(s)
Highlights all messages and prompts issued by the pg command
-s
Start the display from line no <n> of the file specified+<n>
Continues to the next file after the end of one file, if more than one files are specified at the command line
-e
Display the <string> at the pg command prompt.Default is ‘:’ . If string is %d the pageno is displayed atr the prompt
-p <string>
Clear the screen at the end of each page of display and start the display at the top of the screen
-c
Truncate lines longer than the width of the screen display-f
SignificanceFlag
Search for pattern <pattern> in the file and start the display at that line
+/pattern/Sets the size of the display screen to <n> number of lines-<n>SignificanceFlag
Keystrokes in pg mode
Go backward by ½ screen-dRedraws the screen^l
Go forward by ½ screend
Go forward by <n> lines+<n>lGo backward by <n> lines-<n>l
Start the display in the file at line specified by <n><n>lGo forward by one linelGo forward by <n> number of pages+<n>Go backward by <n> number of pages-<n>SignificanceOption
Quits the pg commandqStart showing the next filenStarts showing the <n>th previous file. <n>p
Searches forward for the pattern <pattern> in the file from beginning of the next page. If a number <n> is specified, pg searches for the specified occurrence number <n> of the <pattern>
<n>/<pattern>Move to last page of the file$ SignificanceOption
cpCopies one file to another file/one directory to another directory/files into directories
Syntax :cp [-flag] [ old files/directories] [ new files/directories]
Force copying-f
Recursive copying-r
Interactive copying : prompt appears for user accent before copying
-i
Preserve permissions. Preserves modification time,access time,file mode, user id, user group etc
-p
SignificanceFlag
Copies files: file1, file2 and file3 into directory dir1cp file1 file2 file3 dir1
If dir3 exists, two sub-directories under it are created : dir1 and dir2.IF dir2 directory does not exist, then a new directory dir2 is created with the contents same as dir1 and one additional sub-directory dir2
cp –R dir1 dir2 dir3
Copies the contents of file1 into new or existing file file2
cp file1 file2
Copies directory dir1 into a new directory dir2(if dir2 does not exist before) / copies the directory dir1 as a sub-directory under dir2(if directory dir2 exists before)
cp –R dir1 dir2
SignificanceExampleExamples
mv
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 existing destination file forbids writing, mv command asks for overwriting the file
Syntax :mv [-flag] [ old files/directories] [ new files/directories]
Interactive moving-iPerforms move operation without prompting for permission-fSignificanceFlag
rm
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 execute permission in the directory
Syntax :rm [-f|-i] [ -r|-R] [files|directories]
Interactive deletion-iRecursively delete the entire contents of the cirectorybefore removing the directory itself
-r or –R
Forceful deletion-fSignificanceFlag
Remove all files with extension : .tmp under the current directory
rm *.temp
Moves all the files with .txt extension under the directory testdir residing under the parent directory of the current directory
mv *.txt ../testdir
Remove the directory backupdir with all its contents
rm –r backupdir
SignificanceExample
Examples
wc
Counts the number of words/bytes/characters/lines in a file
Syntax :wc [-c|-l|-w] <filename(s)>
Counts the total no. of lines-lCounts the total no. of characters-c
Counts the total no. of words-wSignificanceFlag
$ wc –c file1
32 file1
$ wc –l file1
2 file1
$ wc –w file1
8 file1
Examples
file
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 first 512bytes and tries to guess its language.
File /etc/magic is ued to identify files that have some sort of magic number, that is, any file containing a numeric or string constant that indicates its type. Commentary at the beginning of /etc/magic explains the format.
Syntax : file [-f ffile] [-h] file ...
chmod
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 to modify its contents.
Execute : If the file is an executable, any allowed user can execute it
Syntax : chmod [file permission] <files|directories>
A file can be accessed by:-
User : Person creating the file. He grants all the authorizations to the file
Group : Group user for the file
Others: All other users not belonging to the group or are not the creator.
Execute permission1Read and write permission6(=4+2)Read,write and execute permission7(=4+2+1)Read and execute permission5(=4+1)
Write permission2Read permission4SignificanceValue
Numeric representation of permissions
Example of granting numeric authorization
Syntax: chmod <val1><val2><val3> <filename(s)|directory name(s)>
val1 is for users
val2 is for group
val3 is for others
Any permission on a directory percolate down to the files and sub-directories under it.
Grant all permission to allchmod 777 file1
Grant all permission to user and group, read and write permission to others
chmod 776
Grant all permissions to : User, and read permission to group and others
chmod 744 file1SignificanceExample
Execute permissionxWrite permissionwRead permissionrSignificanceValue
Alphabetic representation of permissions
Any other usersoAll(creator, other users in the same group, and other users)a
Other users in the same group of the creatorgUser creating the fileuSignificanceValue
Example of granting alphabetic authorization
Assign execute permission to everybodychmod a+x fileTraverse the directory subtree under directory /home/ems2000 making all regular files readable by user and group only, revoke read permission from others and grant execute permission to all
chmod –R ug+r,o-r,a+x/home/ems2000
Revoke execute permission from otherschmod o-x file1
Assign read,write and execute permission to user, but only read permission to group and others
chmod u=rwx,go=r file1SignificanceExample
Chown and chgrp
chown changes owner of the file and chgrp changes group of each file specified.
To change user or groyp, one must own the file and have the CHOWN privilege granted to him by the system administrator
Syntax : chown [-h] [-R] [owner] [file…]
chgrp [-h] [-R] [group] [file…]
Recursively change the owner and group of all the files and sub-directories under the directory named
-RChange owner/group of a symbolic link-hSignificanceOptions
The command searches the directory : shell_scripts and changes each file in that directory to owner : ems2000 and group : users
chown –R ems2000: users shell_scripts
User ‘ems2000’ becomes the group user to access the file auto.login
chgrp ems2000 auto.login
User ‘ems2000’ becomes the owner of the file auto.profile
chown ems2000 auto.profile
SignificanceExample
touchUpdates access time/modification time of file(s)
Syntax : touch [-a|m|c] [ -r <ref-file> | -t <time> ] <filename(s)>
Use the specified time <time> instead of current time. The time format is : <YYYY><MM><DD><hh><mm>.<ss>
-t <time>
Use the corresponding time of file ref-file to change the modification/access time of the file
-r ref-file
Change the modification time of the file to the time specified/ if no time is specified, use the current time.
-m
Change the access time of the file to the time specified/ if no time is specified, use the current time
-aSignificanceFlag
Changes the modification time of file : new.del to Dec 12,2010 time : 23:00Seeing the attributes of the file new.del will reveal the information:--rw-rw-r-- 1 ems2000 dba 10 Dec 12 2010 new.del
touch -m -t 201012122300 new.del
Change the modification time of file : new1 and make it same as the modification time of the file : new.del
touch -m -r new.del new1
Changes the modification time of wot.ksh with the current date and time(Changes can be perceived if the command : ls –lt is fired in the directory containing the file : the file wot.ksh will come at the top of the list)
touch –m wot.ksh
Changes the access time of wot.ksh with the current date and time(Changes can be perceived if the command : ls –ut is fired in the directory containing the file : the file wot.ksh will come at the top of the list)
touch –a wot.ksh
SignificanceExample
lnDescription Link files and directories
Creates link for directories dir1 and dir2 under the directory : testdir with two new /existing directories : dir1 and dir2
ln [-f|-i] dir1 dir2 testdir
Creates link for files new1 and new2 under the directory : testdir with two new files/existing files new1 and new2
ln [-f|-i] file1 file2 testdir
Links file : file1 to a new or existing file : file1ln [-f|-i] file1 newfile
SignificanceCommand
Interactive linking-i
Force esisting files or directories to be removed to allow the link-f
SignificanceFlag
mkdirCreates directories
Mode of permission for the directory and all the files under it
-m <mode>
Intermediate directories are created as necessary.Otherwise, the full path prefix of dirnamemust already exist. mkdir requires write permission in the parent directory.
-pSignificanceFlag
Syntax : mkdir [-p] [-m <mode>] <directory name(s)>
If testdir is not created, it is first created. Then, a directory : subhendu is created under the directory testdir
mkdir –p testdir/subhendu
Directory : testdir is created with permission = 755
Mkdir –m755 testdirSignificanceExample
rmdirRemoves the directory entry for each empty directory referred
Interactive removal of empty directory-i
Path removal.If, after, removing a directory with more than one pathname component, the parent directory of that directory is empty, rmdir removes the parent directory also. This continues till rmdir encounters non-empty parent directory
-p
Forcefully remove a directory, even though it is not empty-f
SignificanceFlag
Syntax : rmdir [-f|-I|-p] <directory>
cmp
Description Compares two files
Prints nothing; return exit codes only-s
Print the byte number(decimal) and differing byte(octal) for each difference
-l
SignificanceFlag
Syntax : cmp [-l|-s] <file1> <file2>
Files not identical1
Inaccessible/missing arguments2
Files identical0
SignificanceReturn codes
comm
Description Select or reject lines common to both files.
Syntax : comm [-[123]] <file1> <file2>
Suppress display of 3rd columncomm –3 filenew fileold
Suppress display of 2nd columncomm –2 filenew fileold
Suppress display of 1st columncomm –1 filenew fileold
Shows only lines common to both filescomm –12 filenew fileold
Produces a three columnar output:-1st column displays lines only in file: filenew2nd column displays lines only in file:fileold3rd column displays lines common to both files
comm filenew fileold
SignificanceCase
sdiff
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 <
Do not print identical lines-s
Only print on the left hand side where columns are identical-l
SignificanceFlag
dircmp
Description Examines two directories and generates various
tabulated info about the contents of the directories.
Syntax : dircmp [ -d|-s|-wn] <directory1> <directory2>
Suppresses messages about identical files-s
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
-d
SignificanceFlag
$ >ls -ltr dir1 dir2dir1: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 samefile1dir2: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 1directory .different ./abcsame ./defsame ./samefile1
lp
Description Prints one/more files to a specified printer
Syntax : lp -d<printer> [-m] [-n<copies>] [-t<title>] <file(s)
Notify the requesting user at successful completion of the print request by mail
-m
Specifies number of copies to print-n<copies>
Print the specified title <title> on the banner page-t<title>
Specify the printer <printer> where the print request is to be directed
-d<printer>
SignificanceFlag
lpstat
Description Shows current status of the print request
Syntax : lpstat [ -p<printer>] [-t] [-u<username>] [-v <printername>]
Displays detailed status information about the print from all users
-t
Displays the status of print requests triggered by user <username>
-u<username>
Displays a list for the specified printername-v<printername>
Shows all printing jobs queued at printer <printer>-p<printer>
SignificanceFlag
cancelDescription Used to cancel a specific print request or cancel all queued requests 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
prDescription Prepares a file for printing by adding suitable
headers, footers and formatted texts.
Prints a file in three columnspr –3 file1
Set the width of each page to 80 instead of a default of 72
pr –w 80 file1
Form feed is used for a new page instead of a sequence of line feed characters
pr –f file1
Suppresses page headers and footers for file : file1pr –t file1
Changes default page size from 66 to 72pr –l 72 file1
Show file file1 from 10th pagepr +10 file
Adds 5 lines of margin at the top and 5 lines at the bottom of file : file1. Header shows date and time of last modification of the file along with filename and pageno
pr file1
SignificanceExample
Print “List of files” instead of the filename as header on each page.
pr –h “List of files”file1
Indent each line by 5 columnspr –o5 file1
Generate output with double specingpr –d file1
Pauses after each page is displayed in the terminal
pr –P file1
SignificanceExample
Compressing filesCompress
Compress <filename(s)>Reduces the size of the named files using adaptive Lempel-zev coding. If reduction is possible, each file is replaced by a new file with same name suffixed sith ‘.Z’. Original ownership, modes, access and modification times are 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 is replaced by a new file with same name suffixed sith ‘.gz’. Original ownership, modes, access and modification times are preserved. To uncompress a file, issue the command : gunzip <filename>.gz . To view a compressed file, issue : gzcat <filename>.Z
FiltersBy 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
Gives first few lines of a file
Same as abovehead -l -n 5 newfile
Same as abovehead –n 5 newfile
Shows first 14 characters of newfilehead -c –n 14 newfile
Shows first 5 lines of file : newfilehead –5 newfileSignificanceExample
Syntax :head [-<n>] [-c] [-l] [ -n <I>] [file(s)]
tailGives last few lines/characters of a file
Works in constant pilot mode . Used for variable sized files those are growing constantly.
-f
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 till end of the file
-c <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 till end of the file
-n <no>SignificanceFlag
Syntax : tail [-f] [-c number] [file]tail [-f] [-n number] [file]
Shows from 5th byte till end of the filetail –c +5 newfile
Constantly displays the last three lines of file : newfile and leave the tail in “follow” mode. The ‘$’ prompt does not return even after the work is over. One have to abort the process to exit to the shell.
tail –fn 3 newfile
Shows from 5th line to end of the filetail +5 newfile
Same as abovetail –n +5 newfile
Shows last 5 bytes of a filetail -c 5 newfile
Same as abovetail –n 5 newfile
Shows last 5 lines of newfiletail –5 newfile
SignificanceExample
cut
Extracts selected fields/characters on each line of a file
Syntax : cut [-c list] [file]cut [-f list] [-d <char>] [file]
Using ‘|’ as field separator, it extracts field contents of 1st to 5th fields and display in the terminal.
cut –d”|” –f 1-5 newfile
Extracts texts from column 1 to 6, column 22 and from column 24 to end of the line and display it in the terminal
cut –c –6, 22,24- file1
Extracts field contents of 2nd and 3rd fields and display in the terminal. Use “|” as a field separator while extracting fields
cut –d”|” –f 2,3 newfile
Extracts text from column 1 to 22 from the file : newfile and displays it in the terminal
cut –c –22 newfile
Extracts texts from column 6 to 22 and 24 to 32 and displays in the terminal in two columns
cut –c 6-22,24-32 file1
SignificanceExample
paste
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 ...
Joins all the lines of file1 into a single line and show it in the terminal
Paste –s file1
Same as above with ‘|’ as delimiter between the fields of both files on each line.
paste –d”|” file1 file2
Display in the screen the join of line 1 of file1 with line 1 of file 2 with a space between them, line 2 of file1 with line 2 of file2 with a space delimiter and so on.
paste file1 file2
SignificanceExample
Combines 1st and 2nd lines into a single line, 3rd and 4th into another line and so on.
paste –s -d” t\n\” file1
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 file1 file2SignificanceExample
split
Splits up a file into equiline smaller lines.
Large files are sometime difficult to edit with an editor. The split command 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 from xba,xbb.. till xbz. Total 26x 26 = 676 files can be created in this way.
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 lines from newfile
fmt
Simple text formatter that fills and join lines /split lines to produce output lines upto the number of characters specified in the –w option(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, wehave ameeting.It willstart at 6pm. Bye
fold
Folds long lines
Syntax : fold [-b|-s] [-w <width>] <file….>
Break the line on the last blank character found before the specified number of column position specified in the –w<width> option(default : 80)
-sWidth in bytes for counting-bSignificanceFlag
$ cat file1Today, we have a meeting. It will start at 6 pm. Bye
$ fmt -s -w15 file1Today, we havea meeting. Itwill start at 6pm. Bye
sortSorts 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 also be changed.
Checks whether the file : file1 is already sorted or not. Value of system variable $? Is 0 if it is sorted
Sort –c file1
Checks whether the file is already sorted on 3rd
field or notsort –t “|” +2 –c file1
Reverse sort on 2nd fieldSort –t “|” –r +1 file1
Sort the contents of file1 based on 3rd fieldSort –t “|” +2 file1
Shows the sorted contents of file1 as per character sorting
sort file1 SignificanceExample
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 –t “|” +7.5 – 11.6 file1
Sort numerically file : file1. Used when the file contains numeric entries.
Sort –n file1
Sorts the contents of the file : file1 and removes duplicate entries
Sort –u file1
Start sorting of file1 after 3rd field(+3) and stop sorting after 4th field(-4). Basically sort file : file1 on 4th field
Sort –t “|” +3 –4 file1
Sort the contents of file : file1 based on 4th field and store the output in a file : sort.lst
Sort –o sort.lst +3 file1SignificanceExample
tr
By default, it translates each character in <expr1> found in the file to its mapped character in <expr2>
Syntax : tr [-c] [-d] <expr1> <expr2> < <file>
Delete all characters except ‘|’ and ‘/’.tr –cd ‘|/’ < fileold
Deletes all the ‘|’ characters from file : fileoldtr –d ‘|’ < fileoldCompress multiple consecutive presence of character ‘A’ to one ‘A’
tr –s ‘A’ <fileold
Translates all the lowercase letters in file : filenew to uppercase
tr ‘[a-z]’ ‘[A-Z]’ <filenew
Translates the uppercase letters A,B and C in file : filenew to lowercase
tr ‘ABC’ ‘abc’ < filenewSignificanceExample
nl
Shows line number at the left of each line in the file. Reserves six characters for the number
Syntax : nl [-w < number>] [ -s <delimiter_char>] <filename….>
Used to specify the character used for delimiting between line no and line content
-s <delimiter_char>
Specifies the width option for line number display to <number> characters
-w <number>SignificanceExample
spell
Lists all spellings in the file (s) the program recognizes as mistakes.
Syntax : spell [-a|-b] <filename(s)>
Use British spelling system-b
Use American spelling system-a
SignificanceExample
find
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>
Selects file if accessed within <x> days-atime -<x>
Selects file if accesses within <x> minutes-amin -<x>
Selects file if modified after file <flname>-newer <flname>
Selects files if modified within <x> minutes-mmin -<x>
Selects files if modified within <x> days-mtime -<x>
Selects file if accesses before <x> minutes-amin +<x>
Selects file if accessed before <x> days-atime +<x>
Selects file if the size exceeds <x> blocks(characters if <c> is also specified)
-size +<x>[<c>]
Selects file if owned by group <grpname>-group <grpname>
Selects type of files specified by <f>-type <f>
Selects file owned by user specified by <username>-user <username>
Selects file with name <filename> . Pattern matching is allowed
-name <filename>
SignificanceMode and Selection criteria
Finds the files with permission mode specified-perm <permission_mode>
Shows selected files in display-print
Executes command <command> after user confirmation
-ok <command> {} ;
Executes command <command> after finding every file on the search
-exec <command> {} \;
SignificanceAction
grep
Searches a file for a pattern
Syntax : grep [-c|-n|-v|-l|-I|-e|-h|-x ] <pattern…> <filename(s)>
Extended search. More than one pattern is to be searched-e
Suppresses printing filename when printing multiple files where the pattern is found
-h
Matches are recognized only when the entire line of the file searched matches the fixed string
-x
Ignores case while searching for the pattern-i
Displays only file names containing the pattern-l
Displays all lines excepting those containing the pattern-v
Displays the line number containing the pattern, along with the lines-n
Counts number of occurrences. Output shows only the file name and number of times the searched pattern found within it
-cSignificanceFlag
Searches for the string ‘Manoroma’ or ‘Monoroma’ in the file : name.lst
grep 'M[ao]noroma' name.lst
Searches all lines starting with Agrep “^A” name.lstSearches all lines ending with ‘Sinha’grep "Sinha$" name.lst
Searches for pattern ‘Subhendu’ and ‘Suvendu’ in the file : name.lst
grep –e “Subhendu” –e “suvendu” name.lst
Case-independent searching of the string ‘while’ takes place in all the .ksh files and the the name of the files where matched is displayed only
grep –li while *.ksh
Searches for the string ‘List of names’ in all .txt files.
grep “List of names” *.txt
Searches for the string ‘while’ in all files with extension ‘.ksh’ and shows the file name and the line containing the pattern
grep while *.ksh
Searches for the string ‘while’ in the file :wot.ksh and if found, displays the lines containing the pattern
grep while wot.kshSignificanceExample
egrepDescription Extended grep
Matches expression <exp1><exp3> or <exp2><exp3>
(<exp1>|<exp2>|<exp3>
Matches <expr1> or <expr2><expr1>|<expr2>
Matches zero / presence of character <ch><ch>?
Matches one/more presence of character <ch><ch>+
SignificanceExpression
File : srchlist contains all the strings to be searched in file : name.lst
egrep –f srchlist name.lst
Matches ‘Akash’ , ‘Akaash’,’Akaash’egrep 'Aka+sh' name.lst
Matches string ‘Monoroma’ or ‘Manoroma’egrep '(Mano|Mono)roma' name.lst
SignificanceExpression
sed
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 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
Addressing by lines
Show from line no. 10 to end of filesed –n ’10,$p’ name.lst
Show 1st to 2nd line, 7th to 9th line and last line of the file : name.lst
sed –n –e ‘1,2p’ –e ‘7,9p’ –e ‘$p’name.lst
Show 3rd to 5th line of file : name.lstsed –n ‘3,5p’ name.lst
Show first three lines of file : name.lst and quit
sed ‘3q’ name.lstSignificanceExamples
Inserts the line ‘subhendu’ at the end of file : del and saves under file : del1
$ sed ‘$a\> subhendu> ' del>del1
Inserts the line ‘subhendu’ before every line in the file : del and save the display in file : del1
$ sed ‘i\> subhendu> ' del>del1
Inserts the line ‘subhendu’ after every line in the file : del and save the display in file : del1
$ sed 'a\> subhendu> ' del>del1
SignificanceExamples
Shows lines longer than 100 characterssed -n '/. {101, }/p' del
Shows lines containing ‘9’ after 9 characters in a line
sed -n '/. {9 }9/p' del
Shows all lines with 4 characters onlysed –n ‘/^….$/p’ emp.lst
Shows all but the blank lines from file emp.lstsed –n ‘/^$/!p’ emp.lst
sed ‘/^$’/d’ emp.lst
Searches for any line containing ‘A’ as 1st
character, ‘a’ as 3rd character and ‘s’ as 5th
character
sed –n ‘/^A.a.s/p’ emp.lst
Searches for any string with the pattern specified
sed -n '/Aka*sh/p' name.lst
Searches for the string ‘Chatterjee’ or ‘chatterjee’ in file : emp.lst
sed –n ‘/[cC]hatterjee/p’emp.lst
Searches for the string ‘getopts’ and ‘charstring’ in file : emp.ksh
sed –n ‘/getopts/, /charstring/p’ emp.ksh
Searches the string ‘getopts’ in file : emp.ksh and displays it
sed –n ‘/getopts/p’ emp.ksh
SignificanceExampleContext addressing
Searches file : emp.ksh for the strings specified in the file instr.lst
sed –n –f instr.lst emp.ksh
Searches file emp.ksh for pattern : if and writes matching lines into file : iflistSearches file emp.ksh for pattern : whileand writes matching lines to file : wlist
sed –n ‘/if/w iflist/while/w wlist’
emp.ksh
SignificanceExamples
Substitution
Replaces ‘staff’ with ‘member’ in all the lines containing ‘marketing’
sed –n ‘/marketing/s/staff/member/p’list1
Replaces ‘student’ with ‘ex student’sed ‘s/student/ex &/’ name.lst
Shows from 1st to 5th line of file : list1 with the string ‘basic’ replaced by ‘prelim’ at all occurences
sed ‘1,5s/basic/prelim/’ list1
Shows the content of the file : list1 with the string ‘exhausted’ replaced by ‘tired’ at all occurences
sed ‘s/exhausted/tired/’ list1sed ‘s/exhausted/tired/p’ list1
SignificanceExamples
awk
•Reporting tool for Unix.
•Developed by Alfred V Aho, Peter J Weinberger, Brian W Kernighan
•Emerging as programming language.
•Produces facilities similar to SQL language.
Syntax : awk <options> ‘ <address> { <actions> }’ <input|file(s)>
‘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 with binary files.
Using awk from command line
$ 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
$
$ cat name.lst0001 | 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 |
Read data from a file
name.lst
$ awk –F”|” { print $1,$2 }’ name.lst
0001 Subhendu Majumdar
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
$ 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
Taking awk instructions from a file
Input field separatorFS
0(Case sensitive)
1 ( Case insensitive)
IGNORECASE
Number of fields in the current recordNF
Current record numberFNR
Current record numberNR
Name of current input fileFILENAME
Whitespace separated string for the width of input fields. Provides a facility for fixed-length fields instead of using field separators.
FIELDWIDTHS
Conversion format for numbersCONVFMT
Number of command line argumentsARGC
SignificanceVariables
Pre-defined variables in awk
Input field separatorFS
0(Case sensitive)
1 ( Case insensitive)
IGNORECASE
Unix environment variablesENVIRON
Unix system error message numberERRNO
Current record numberNR
Name of current input fileFILENAME
Input record separator( default is new line)RS
Output record separator(default is newline)ORS
Output field separatorOFS
SignificanceVariables
Pre-defined variables in awk…contd
$ cat name1.lst
0001 | Sourav Ghar | Team Lead | Avaya | 25000.00|
Example of using pre-defined variables
$ 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.lst3 ** 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-------------------------------------------
Matches any of ‘Desk’ or ‘Chair’Desk|Chair
Matches any of A,B or C followed by D,E or F[ABC][DEF]
Matches any of A,B or C[ABC]
Matches any of A,B,C,a,b or c[A-Ca-c]
Matches any character other than A,B or C[^ABC]
Matches any single character.Matches at the end of the string$Starts match at the beginning^Tab\tNewline\nSignificanceExpression
Regular Expression Metacharacters in Awk:-
Matches to an empty string or more of A,B or C[ABC]?Matches one/more occurences of A,B or C[ABC]+
(Blue|Black)berry matches to Blueberry or Blackberry( )
Matches zero/more occurences of A,B or C[ABC]*SignificanceExpression
Regular Expression Metacharacters in Awk…contd
Not equal to!=
Matched by regular expression~
Not matched by regular expression!~
Greater than or equal to>=Greater than>Less than or equal to<=Less than<Is equal to==SignificanceOperators
Comparison Operators in awk
Used to group compound statements( )Logical NOT!Logical OR||Logical AND&&SignificanceOperators
Compound pattern Operators
Range pattern Operators
Shows 3rd,4th and 5th recordawk -F"|" 'NR==3,NR==5 { print NR,$2 } ' name.lst
SignificanceOperators
BEGIN {
<declare variables>
< write headings>
}
. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . .
END {
< show subtotals>
}
BEGIN and END blocks
$ 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|
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
$ 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.lst
3 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
Sumit kumar Basu Avaya 12000.00
Tamal Sen Sharma Avaya 12000.00
Raghab Dasgupta Avaya 12000.00
Rajib Banerjee Avaya 12000.00
(Shows 2nd,4th and 5th field of all records where 3rd field contains the word ‘ 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|
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 than Programmer 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
> 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"
}
$3~ /Manager/ { kount++tot = tot + $5print kount,$2,$5
}END {print "\n\nTotal managers found is :",kountprint "Average salary is :",tot/kount
}
$ awk -F"|" -f salavg.awk name.lst
Salary Report of Managers
1 Anindya Das 30000.002 Sougata Das 50000.00
Total managers found is : 2Average salary is : 40000
(A) Concatenating Strings
x = “abc””def” # x = abcdef
y = “ghi”
z = x y # z = abcdefghi
String Operators
Built-in String Functions
Returns the position in string <string> that matches the expression <reg>
match( <string> , <reg>)
Splits string <string> into array elements of <store> based on delimiter <delim>
split(<string>,<store>,<delim>)
Substitutes string <string> in <target> the first time the regular expression <reg> is matched.
sub(<reg>,<string>,<target>)
Returns the length of the string <string>
length(<string>)
Returns the position of the string <search> in string <string>
index( <search>,<string> )
Substitutes string <string> in string <target> every time the regular expression <reg> is matched
gsub( <reg>,<string>,<target>)SignificanceFunctions
Built-in String Functions
Translates the string <string> rtoupper case
toupper(<string>)
Translates the string <string> rtolower case
tolower(<string>)
Extracts the portion of the string <string> starting from position <pos> of length <len>
substr(<string>,<pos>,<len>)SignificanceFunctions
Use of Character Functions in awk
$ cat awkf.awk
$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
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 : Anin
Length 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
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.lstDetails of name of the Employees :
Name First name Last name
………………………...........................................................
Subhendu Majumdar Subhendu Majumdar
Anindya Das Anindya Das
Sougata Das Sougata Das
Special String Constants
Carriage return\rTab character\tVertical tab\vDouble quote\”
Formfeed\fNewline\n
Backspace\bAlert or bell character\aBackslash\\SignificanceExpression
Built-in Numerical Functions
Returns the sine of x in radianssin(x)Returns square root of xsqrt(x)Current time in seconds since midnight, Jan 1,1970systime()
Natural log of xlog(x)Returns random number between 0 and 1rand( )
Returns e raised to power of xexp(x)Integer value of xint(x)Cosine of x in radianscos(x)SignificanceExpression
Arithmetic Functions
Decrements y by 1 and then uses ity--Uses y and then decrements it--y
Subtracts y from xx-y
Divides ‘x’ by ‘y’x/yIncrements x by 1 and then uses itx++Uses x and then increments it by 1++x
Adds y to xx+y
Multiplies y with xx*y
Calculates the reminder of division of x by yx%yRaises x to the power of yx**yRaises x to the power of yx^ySignificanceExpression
Arithmetic Functions
x = x/yx/=yx = x*yx*=y
x = x + 1 + yx++=y
x = x-yx-=yx = x+yx+=ySignificanceExpression
Arrays
Arrays are normally used to handle more than one related piece of data.
One accesses the individual elements within an array by enclosing the subscript within double brackets.
In AWK, one does not have to declare a variable to be an array, and does not have to define the maximum no. of elements. When one uses an element for the first time, it is created.
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-dimensional 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"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.lstName Basic da hra
---------------------------------------------------------------Anindya Das 30000.00 7500 9000Sougata Das 50000.00 12500 15000
-------------------------------------------------------------Total 80000 20000 24000---------------------------------------------------------------
Multidimensional Arrays-Examples
$ 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.awkBEGIN {system(" tput clear " );print "\n\nPopulation in different parts of the cities in
India\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.lstPopulation in different parts of the cities in India
Chennai West 150000Calcutta North 234500Calcutta East 134500Chennai East 100000Calcutta South 234508Calcutta West 100500--------------------------------------------------------Population for Calcutta : 704008Population for Chennai 250000
Conditional Flow
{if ………..else if ……..else if ……..else ………}
$ cat sal.awk4 ~/Nestle/ {if ( $5 > 10000 )print $2 , $5 , "Taxable";else print $2 , $5 , "Non taxable";}
$ awk -F"|" -f sal.awk name.lstDamini Sen 10000.00 Non taxable
Akash Nag 7800.00 Non taxableAnindya Das 30000.00 Taxable
Conditional Flow
To substitute ‘if’ test anywhere in the code:-condition ? True : false
$ 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 taxable
Akash Nag 7800.00 Non taxable
Anindya Das 30000.00 Taxable
Do Loop
do
<statement>
While ( <conditions> )statement in any of the above constructs may be either a simple statement or a series of statements enclosed in { }
Example of Do Loop
$ cat doloop.awk
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 --------------------------
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> )
statementstatement in any of the above constructs may be either a simple statement or a series of statements enclosed in { }
Example of While Loop
$ cat while.loopBEGIN {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 --------------------------
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>
For ( <subscript> in <array> )
<statement>
statement in any of the above constructs may be either a simple statement or a series of statements enclosed in { }
Example of For Loop
$ cat for.loop
BEGIN {
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 --------------------------
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
Leaves the current record and continues with the next record in the loop from beginning of the loop processing statements.
ContinueExits the loopBreakSignificanceStatement
Pretty formatting - printf
Integer%d
Floating point number using scientific notation%eFloating point number(eg., 10.43)%fString of characters%sAwk chooses %e or %f display format(whichever is shorter) suppressing non-significant zeroes.
%g
Integer%i
Ascii character%cMeaningFormat
Syntax : printf( format_specifier, var1, var2,…..varn)
Output to another file
Appends the output at the end of existing file “datafile”or creates a new file.
Printf( “Hello world\n”) >> “datafile”
Creates a file called datafilewith the output
Printf( “Hello world\n”) > “datafile”SignificanceStatement
A file should be closed after it receives output from an awk program.
Syntax : close(“filename”)
FunctionsFunctions
Block of code, accesses usually in multiple places in the code.When awk reaches the end of the function, it implicitly returns the control to the calling routine.
To make an explicit return to the main program, one can explicitly use the return statement.
Syntax :function fname(parameter list) {< function_code>
}
Example of Functions
$ cat subcalc.awkBEGIN {
FS = "|"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---------------------------------------------------------------Anindya Das 30000.00 7500 9000Sougata Das 50000.00 12500 15000
Use of inputs from user in awk program
BEGIN {
FS="|"
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
Subhendu Majumdar Team Lead 25000.00
Anindya Das Manager 30000.00
Sougata Das Manager 50000.00
The End
The Unix environment is controlled by a number of pre-defined environment variables.
They are usually defined in the file ‘.profile’for the user or are defined by user as and when required.
Value of the process id of the parent process of $$PPID
Absolute directory path under which the programs for the commands reside.
PATH
Integer number of seconds since one invoked the Korn shell
SECONDS
Current working directoryPWD
Value is always the full pathname of the directory where the control resided before coming into current working directory
OLDPWD
Meaningful only within a shell script. Its value is the line no. of the line in the script currently being executed
LINENO
Non-zero exit code of last command that failed. Value changes only when a command fails
ERRNO
SignificancePre-defined variable
Pathname of home directory for an userHOME
Integer number containing maximum number of commands to be retained in history files
HISTSIZEFilename of the korn shell history fileHISTFILE
Pathname of the shell script containing commands to be executed when the korn shell is invoked
ENV
List of colon separated directory name, which is followed by the system when the user issues a ‘cd’command
CDPATH
Vi or emacsEDITOR
SignificancePre-defined variable
Display width used by Korn shell edit mode- vi or emacs
COLUMNS
Primary shell prompt string.Prompt where commands are issued by the user
PS1
No. of seconds after which the shell should check for a change in the MAIL file
MAILCHECK
Pathname of a file to be monitored by the shell for a change in its date of last modification. If change is noted, the shell issues the message : You have mail at the next oppurtunity
Integer number representing the number of lines displayed by the terminal.
LINES
Zero/more characters to be treated by the shell as de-limiters when parsing a command line into words or using the read command
IFS
SignificancePre-defined variable
Debug prompt screen , shown when a shell program is executed using –v option.
PS4
Seconds after which automatic logout occurs.TMOUT
Symbolic alpha numeric string that identifies the type of your terminal
TERM
Pathname of the shell the user is usingSHELL
Secondary prompt screen. Shown when a command pawns more than one line.
PS2
SignificancePre-defined variable
sttyControls terminal output and sets terminal characteristics
Sets <Ctrl-a> to terminate output and declare end-of-file, for eg., while creating file using cat command
stty eof \ ^a
Sets <Ctrl-Z> as the interrupt keystty intr ^Z
Typing <Ctrl-d> enables aborting the current shellstty quit ‘^d’
Typing <Ctrl-H> helps to remove the last character typed
stty erase ‘^H’
Keyboard entry is not echoedstty –echoKeyboard entry is echoedstty echo
Enables backspacing remove character from the display
stty –echoeDisplays all current settingsstty –aSignificanceOptions
Set 20 rows in displaystty rows 20Sets 20 rows and 80 columns in displaystty rows 20
column 80
Maps lowercase alphabets to uppercasestty olcucGives the current screen size in terms of rows and columnsstty size
Maps uppercase alphabets to lowercasestty iuclc
Set the terminal characteristics to values that will work for most terminals
stty sanePressing <Ctrl-J> does the job of ending a linestty eol ‘^J’
Enables starting the current halted session by pressing ‘^Q’
stty start ‘^Q’Enables halting the current session by pressing ‘^S’stty stop ‘^S’
Enables suspending a foreground process when <ctrl-Z> is pressed
stty susp ‘^Z’SignificanceOptions
What is a process?
Process is a program that has its own address space. Every command fired in Unix has a process associated with it.
Is born when a program starts execution, and remains alive as long as the program is active.After execution is complete, it dies.
Has a name, usually the name of the program being executed.
Kernel controls the process
Kernel is finally responsible for managing the process.
Determines time and priorities allocated to processed, enabling multiple processes to share CPU resources.
Each one is uniquely identified by PID, Process Identifier, allocated by the Kernel when it is born.
Sh Process
A process is immediately set up by the Kernel when a user logs into Unix system.This os usually a Unix command(sh/ksh/bash etc).
Any command executed is actually input to the shell process.
Can be known from the value of shell variable ‘$$’. This process for the shell remains alive till the user logs out.
Parent and Child process
Every process has a parent process(except the root process).
When a command is executed from the shell prompt, that becomes a child process to the parent shell process.
A parent process can ave more than one child process
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
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
ps –f
Selects the process associated with the current terminal.Shows output in four columns: PID, TTY(terminal info), TIME(total cpu time used by the process), CMD(command gererating the process)
ps
SignificanceCommand
Displays all processes of all users; excepting the system processes
ps –a
Shows command line in extended formatps –x
Displays system processesps –e
Displays processed of user : ems2000ps –u ems2000
SignificanceCommand
How a process is created in Unix?
Forking : A copy of the the process that invokes a new process is created.Child gets a new PID.
Exec : The parent then overwrites its image with a copy of the program to be executed. Done with exec system call. No additional process is created at this stage.
Wait: Parent then executes the wait system call to keep waiting for the child process to complete. After the child process is completely executed, a terminal signal is sent to the parent.
Internal and External Commands
External program : Commands like : cat, ls, grep, sed etc are external programs.Shell creates a process for each of these commands.
Shell Scripts : Shell executes shell scripts by spawning to another shell, which then becomes the parent of the commands featured in the script.
Internal Commands : These are commands which are executed directly by the current shell. No additional processes are generated. Example : cd , echo etc
Running jobs in background
Unix is a multi-tasking system , allowing to perform 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 line interprets the job to be done at background. Shell returns the PID of the background job for tracking.
Job control in Korn and Bash Shells
Korn or bash shell users can use the job control facility of the shell to manipulate jobs.One can put a job in the background, bring it back to foreground, suspend it, run it later, or even kill it.
bg
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
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
Used to bring a background job to foreground and execute it.
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 jobs command) from background to foreground and executes it.
Execution of fg command alone from the OS prompt resumes latest created background process to be executedinforeground.
stop
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.
Syntax : stop [%<job_no>]
kill
System often requires to control execution of a process. 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.
Syntax : kill [options] <PID>
Start the paused process with PID 2900
kill –26 2900kill –s SIGCONT 2900
Kill the latest background jobkill &!
Pause the process with PID 2900kill –24 2900kill –25 2900
Surely kills a process with PID 2905kill –9 2905kill –s SIGKILL 2905
Terminates the process with PID 2906
kill –15 2906kill –s SIGTERM 2906
Kills the process with Pid : 2905kill 2905
SignificanceExample
nice
Processes in the Unix are usually executed with equal priority.
But, high priority jobs must be executed at the earliest. Unix offers : 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 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 queue and can be removed using : at –r , provided it is fired before the job has been executed.
at
Schedules a program for execution at a specified time.
Displays a list of scheduled jobs.
Removes jobs from the scheduled list.
Mail a report of successful execution of the job-m
Remove the jobs specified in the joblist from queue-r <joblist>
Displays the list of jobs scheduled by the user-l
SignificanceFlag
at 2300 today my-job.sh
Schedules execution of njb.sh after 6 hours from now
at now + 6 hours njb.sh
Schedules execution of program :njb.sh at 6:30 pm next week
at 6:30 pm next week njb.sh
Schedules execution of program :njb.sh after 1 year from now
at now + 1 year njb.sh
Schedules execution of program :njb.sh at 3:08 pm tomorrow.
at 3:08 pm + 1 day njb.sh
at 11:00 pm my_job.sh Executes program my_job.sh at 11:00 pm tonight
at 2300 my_job.sh
at 11:00 P my_job.sh
SignificanceExamples
Schedules execution of njb.sh after 5 minutes from now.
at now + 5 minutes <njb.sh
at –f njb.sh now + 5 minutes
Schedule execution of njb.sh at 08:15 am on January 24.
at 08:15 Jan 12 < njb.sh
Schedules execution of njb.sh at 9 am tomorrow.
at 9 am tomorrow
Schedules execution of njb.sh at 9 am coming Monday.
at 9 am Mon njb.sh
SignificanceExamples
cron
Runs jobs periodically.
Mostly dormant, but wakes every minute and checks for a file in /usr/spool/cron/crontabs directory for programs to be executed at that instant.
A user may be permitted to place a crontab file after his login 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 when the command is to be executed. The range 00-10 schedules every minute in the first 10 minutes of the hour.
Second fields indicates the hour in the 24 hour format for scheduling.
3rd field(1 to 31) controls the day of the month. ‘*’ means all.
4th field(1-12) indicates the month
5th field(0-6) indicates the day of the week(Sunday with 0)
6th field contains the command
Sample format for a crontab file
User can modify the file containing his cron commands as follows:-
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 of cron.txt.
One can see the contents of crontab file issuing the command : crontab -l
System Administration shutdownShutdown Unix server
suGo as you like
wallSend message to everybody
umaskSet Default Permission
ulimitSet maxm. Space for user
duShow disk usage
dfFree bytes available
fingerShow User information
cryptPassword protect a file
shutdown
Executed from the root user to shut down the system.
Shutdown and rebootshutdown –r now
Bring the system down to maintenance(single user mode)
shutdown –m
Shutdown and rebootshutdown –y –g0 –i6
Shutdown at 5:30 pmshutdown 17:30
Immediate shutdownshutdown –y –g0
Powers down system after 2 minutesshutdown –g2
SignificanceCommands
su
Takes to super user mode.
One logs in as the user : mat. Asks for the password.
su mat
Takes to super user mode. One can have all the accesses same as the root user. Root password is required after executing the command
su
SignificanceCommands
wallAddresses all the users simultaneously.
$ wall
………….
…………
^D
All the text written will be reflected in the terminals of all the 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 user.
User can modify this default settings.
To give permission 751 as a default to newly created files, 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, a restriction is to be imposed on the mazimum size of a file that an user is permitted to create.
Ordinary user can reduce the default value . Super user can increase / decrease it.
Sets the maxm. size of data blocks in KBulimit –d <size>Set the maxm. Size in blocksulimit –f <size>Set the CPU time in secondsulimit –t <size>
Sets the maxm. Size of memory in KB.ulimit –m <size>
Shows the hard limitsulimit –HaShows soft limitsulimit –a
SignificanceExamples
du
Shows disk usage – amount of space taken by a group of files in units of 512 bytes or KB.
Descends all sub-directories from the directory in which the command is fired.
Print messages about directories that cannot be read, files cannot be accessed etc.
-r
Give the grand total of disk usage for each of the directories-sGive the block count in terms of 1024 bytes-k
Print entries for each file encountered in the directory hierarchies in additional to the normal output
-a
SignificanceFlag
df
Displays the number of free 512 byte blocks and free inodes available for file systems by examining the count kept in thesuperblocks.
Reports the total number of inodes, number of free inodes, number of used inodes,% of inodes in use
-i
Reports the allocation in KB-k
Reports only the actual count of the blocks in the free list-f
Report the number of files free-e
Reports only the number of KB free-b
SignificanceFlag
finger
By default, the command finger lists for each user in the system:- 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 user read the mail.
Suppresses long output-l
Suppresses plan-p
Print user’s hostname-R
Forces short output format-s
Suppresses the info on idle time-i
Suppresses the project information-h
Suppresses the header info-f
Suppresses information on home directory and shell-b
SignificanceFlag
User’s project info/etc/.project
Mail directory/var/mail
User’s plan/etc/.plan
User’s name, office etc/etc/passwd
Last login file/var/admin/utmp
Who file/etc/utmp
SignificanceFile
Files accessed while showing output
crypt
Used to encode/decode files using personal password
Helps to view an encrypted file : file2. Asks for a password
vi –x file2
Decrypts file : file2 to : file1 using the password : passwd
crypt passwd < file2 > file1
Crypts file : file1 to : file2 based on the password : passwd
crypt passwd < file1 > file2
Crypts file ‘uncryped_file’ tocrypted_file. Asks for a password.
crypt < uncrypted_file >crypted_file
SignificanceOptions
Communicationmailx
Send and receive mails
writeSend message to all users
ftpSend 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>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
~.
The job performed above will send a mail to : [email protected] subject of the mail as : Test heading and body of the mail as : this is a test mail for communication.
Case : 1
$ mailx [email protected]
Subject : Test mail number 2
this is a test mail for communication
~.
The job performed above will send a mail to : [email protected] 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 the subject.
Case : 2
$ echo “Hello mike”|mailx –s “Heading1” [email protected]
The job performed above will send a mail to : [email protected] subject of the mail as : “Heading1” and body of the mail as : “Hello mike”.
Case : 3
The job performed above will send a mail to : [email protected] 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] : Test mail number 2this is a test mail for communication
~hTo: [email protected]: testingCc: [email protected]: [email protected](continue)testing mail--please ignore~.EOT.
Case 5- You want to send a mail to some user and cc it to some other users.
Viewing received mails
mailx -emailx [-HL] [-u user]mailx -f [-HL] [filename]
Read user's mailbox.Can be used only if read access to user's mailbox is not read protected.
-u user
Prints header summary only-H/-L
Read messages from filename instead of the user's system mailbox. If filename is not specified, the secondary mbox is used.
-f
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
-e
SignificanceFlag
Options available in viewing received mail
When one executes mailx command for viewing received mails:-
$ mailxmailx 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
Shows next messagenextReply to message number <n>reply <n>Opens mail utility to write mail to user <user>mail <user>
Quit mail utilityquit(q)Shows all the messages in one shot, one by onetype
Shows message number <n> , the number typed at the first column for every message.
<n>
SignificanceFlag
writeType text into other user’s terminal for information
Case : 1$ write ems2000
Hello, 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/tcHello, how are you^d
The command flashes the message : Hello, how are you to that person’s terminal who is working in terminal pts/tc and logged in as user : 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 : ems2000.
User must be logged in and mesg should be set to y( mesg=y) for successful write communication.
ftp
Copies file over a network connection between local host to remote host. ftp runs on client server.
Syntax : ftp [-i] [-v] [-n] <remote_host>
-v Enables verbose output. IT shows the actions when files are passed from one server to another.
-v
Disable auto-login. If auto-login is enabled and user issues an ‘open’command to establish connection to a remote server, he gets login and passwd prompt. If it is disabled by ‘n’, user does not get such prompts. He then writes: user <username> <passwd> in the ftp mode to specify the user and password.
-n
Disable interactive prompting by multiple file commands.-i
SignificanceFlag
Commands fired from ftp mode
Sets the file transfer type to binarybinary
Close the connection to remote host, if it is open, and exit
bye
Remote file names with name containing all letters in uppercase are translated to lowercase when the file is transported from remote server to local.
case
Set the working directory in the remote server to <remdir>
cd <remdir>
Set the current directory of the remote server to the parent of current working directory.
cdup
Sets the file transfer type to ASCIIascii
Copy file <lf> in the local server to the end of file : <rf> in the remote server. If no such file exists in remote server, a new file is created.
append <lf> <rf>
Executes command <command> in the local server! <command>SignificanceCommand
Copt the remote file to local file.get <rem_file> [<lcl_file>]
Shows the last modification time of remote file <rem_file>
modtime <rem_file>
Copy files from local server to remote servermput <local files>
Delete file <rem_file> from the remote server. The file can be an empty directory.
delete <rem_file>
Writes a list of remote files to local file <lcl_file>
mdir <rem_files> <lcl_file>
Copy remote files to local systemmget <rem_files>
Creates a directory <dirname> in remote server.mkdir <dirname>
Writes an abbreviated listing of remote files to local file <lclfile>
mls <remfiles> <lclfile>
Deletes the files in the remote servermdelete <remote_files>Terminate the connection to the remote serverclose
Changes the mode of the file : <file> to mode <mode>
chmod <mode> <file>SignificanceCommand
Specifies the current working directory of the local server to directory : <local_dir> . If unspecified, user’s home directory is considered for navigation.
lcd [local_dir]
get file if remote file <file_name> is newer than local file <file_name>
newer <file_name>
Toggle forcing interactive prompting on multiple commands
prompt
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 to a local file, a ‘.1’ is appended to the name. For another repeatition, a ‘.2’ is appended till 99 such cases are found.
runique
Beeps when command is executedbeepClose the connection to remote hostdisconnect
Shows a listing of remote directory <rem_dir> to terminal/ into local file <lcl_file> .
dir <rem_dir> [<lcl_file>]SignificanceCommand
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 a local file, a ‘.1’ is appended to the name of the file transferred from local to remote server.. For another repeatition, a ‘.2’ is appended till 99 such cases are found.
sunique
Shows the current status of ftp- informations are shown on remote server name and all the settings(prompt,type,bell,case etc)
status
Acts like get. If local file <lclfile> exists and is smaller than the remote file <remfile>, local file is assumed to be partially transferred from remote server.Then, the transfer is continued from the apparent point of failure. This command is very useful to transfer very large files over thenetwoprk that tend to drop connections.
reget <remfile> [<lclfile>]
byequit
Shows the size of the remote filesize <remote_file>SignificanceCommand
If only help is typed from ftp prompt, it shows a list of all commands that can be used from ftp prompt. If a command is mentioned for help, it shows one line help explaining its significance.
help [<command>]
toggle metacharacter expansion of local file names. If put on, one can use ‘*’ , ‘?’ etc characters to specify portion / all of the file name.
glob
Set the default umask on the remote server to new value. IF nothing is specified, the current umask value of the remote server is displayed.
umask [new mask]
Shows the type of operating system running in remote server
systemSignificanceCommand
rlogin
Logs into remote host <rhost> as user <username> with/without any password.
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 of the remote server
Syntax : rlogin <rhost> [-l <username>]
rcp
Copy Single File
rcp [-p] <srcfile1> <rem_user> @<remserver>:<pathname>/<dest_file>
rcp [-p] <srcfile1> <remuser>@<remserver>:<dest_dir>
Copy Multiple Filesrcp [-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>]
Copies file(s)/directory(ies) from one server to another
List of instructions written within a file.
Programming logics can be built in.
Executes a set of related instructions, saving time,resource and imparts better control.
The file has to be executable to make the thing work.
Invoking Sub-Shell manually
When a script file with execute permission starts running, it runs in a sub-shell under the current shell.
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 have execute 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 under the current shell. To make the script run in the current shell, one have to trigger the program as : . <progname>. The program need not have execute permission before executing in this fashion.
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 in Bourne shell.
To specify the interpreter shell, one can execute the program from OS prompt as :-
$ ksh < prog1.ksh
This will execute program prog1.ksh using Korn shell as theinterpretor.
It is always a better approach to specify the shell at the first line of the 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.
‘Echo’ and ‘read’ statement in shell scripts help us to achieve 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 an indispensable practice.
One can declare it at the top of the program /anywhere at the time of usage.
Declares a variable : g_name and assigns a value ‘two words’ to it.Place the value to be assigned within quotes if contains more than one words
g_name=‘two words’
Declares a variable : g_val and assigns a value 20 to it.
g_val=20
SignificanceExample
Declares a variable : g_cnt and assigns a value 20 to it.
let g_cnt=20
Wipes off the value from variable : g_val.unset g_cnt
Makes the value in the variable : g_cnt readonly. Further change in the value is not allowed.
g_cnt=20readonly g_val
Assigns null value to the variable g_valg_val=“”g_val=‘’g_val=
SignificanceExample
Special parameters used by Shell
Shell uses some special parameters which imparts us different information on a program/command , various arguments passed to it, success/failure of the program etc.
Number of arguments passed in command line$#
Exit status of last command$?
Complete set of positional parameters as a single string
$*
First, second etc arguments passed to a program$1,$2….
Name of executed program/command$0
SignificanceParameters
Argument passed to the last command$_
PID of the current shell$$
PID of last background job$!
SignificanceParameters
Set – Manipulating Positional Parameters
The command : set can be used to manipulate different positional parameters($1,$2…) from a string / output of a command
$ 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 positional parameters( $1 to $9). To shift their values one place to the left, this command is used.
$ set I know this is not a valid issue to discuss at the meeting 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`
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 in files.
Creates a file called : del or overwrites an existing file , the content of the file being : happy
string=happy; echo “$string”>del
Appends the contents o the variable $string at the end of the file : del
Echo “$string”>>del
The value of variable a is displayed in terminal
a=256; echo $a
String : Hello sb1dtl02 shown in terminalecho hello `hostname`
String : Hello dear shown in terminalecho Hello dear
SignificanceExamples
Escape sequences used with echo to format output in terminal
Carriage return\r
Positioning the cursor after the statement\c
Tab\t
Display in reverse video\033[7m
Backspace\bBeep\07
Blinking display\033[5mUnderlined display\033[4mBold display\033[1m
Normal display\033[0mSignificanceSequence
tput
Formats display
Clear to end of displayed
Clear to end of lineel
Echoes no. of lines in the screenlinesBells the terminalbelEnds mode underlinermulStarts mode underlinesmulReverse displayrevBlinking displayblinkBold displayboldMoves cursor to row r and column ccup r cClears the screenclear
SignificanceOptions
Syntax : tput [<options>]
Decision making – If
Making decisions based on conditions is the most popular practice in every programming language. Shell provides decision building using “If” statements.
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 100
elif test $val -ge 50 -a $val -lt 100
then
echo Between 50 and 100
elif test $val -gt 0 -a $val -lt 50
then
echo Between 50 and 100
else
echo No comments
fi
Test
Used to compare numerical values,strings,find properties of different files etc.
Lesser than-lt
Greater than or equal to-ge
Lesser than or equal to-le
Not equal to-ne(!=)Equal to-eq(=)
Greater than-gt
SignificanceNumericl test operators
True if string1 is not equal to string2string1 != string2
True if string length = 0-z stringTrue if string length >0-n string
True if string1 is same as string2string1 = string2
SignificanceString test operators
while [ -z "$userid" ]dotput cup 5 5echo "Enter userid:\c"read useriddone
if test $userid = superthentput cup 11 5echo No such userid allowedexitfi
while [ -z "$pswd" ]dotput cup 7 5echo "Enter password:\c"stty -echoread pswdstty echo
done
while [ -z "$npswd" ]dotput cup 9 5echo "Confirm password:\c"stty -echoread npswdstty echo
done
if test $pswd = $npswdthentput cup 11 5echo o.kelseecho wrong entry....
fi
And,Or
echo "Enter city and state:\c"read city stateif test $city = cal -a $state = wbthenecho You stay in Calcutta and in West Bengalelif test $city != cal -a $state = wbthenecho You stay in West Bengal, not in Calcuttaelif test $state != wb -a \( $city = bom -o $city = del \)thenecho you stay in other metrosfi
or-oand-aSignificanceOperators
E x a m p l e
Case
Used to build logic depending on different values of same object.
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;;[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
While
Syntaxwhile <condition is true>
do
<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
<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>
done
for city in bombay delhi calcutta
do
echo $city is a metro city
done
Loop breaking statements
Does not proceed with current record further and starts processing the next record from the start of the loop
continueControl comes out of the loop completely breakSignificanceStatement
n=20while [ $n -le 100 ]doecho $n((n=n+1))if test $n -eq 30thenbreakfidone
n=0while test $n -le 10do((n=n+1))if test $n -eq 5thencontinuefiecho $n>>deldone
Sleep
Syntax: sleep < number of seconds>
Idly waits for number of seconds specified. Mainly used to display an event clearly to user.
Basename
Extracts the base filename from an absolute pathname
Example:-
$ basename /home/ems2000/shell_scripts/ppc1.sh
ppc1.sh
When used with second argument, it strips off the second 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 script to be read from itself rather than from another file. The here operator(<<) comes handy.
echo "Enter city code:\c"read city_codegrep "^$city_code" << stcity01 Calcutta02 Bombaystcityif [ $? -ne 0 ] ; thenecho invalid codefi
E x a m p l e
Putting all the inputs at a time
A program may ask for different inputs several times and at several points of execution.Instead of putting the inputs when they are asked for by the program, one can put all of them at a time while calling the program for execution.
echo “First name:\c"read fnameecho “Middle name:\c"read mnameecho “Last name:\c"read lnameecho your full name is $fname $mname $lname
$ prog8.sh<<end> soma> sen> gupta> endFirst name:Middle name:Last name: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.
By default, set displays all environment variables. This createsproblem 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`
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
Same as above; but uses current shell only. The closing curly brace must be on a separate line. To have both the braces on the same line, terminate the last command with a ‘;’
{ }
Executes the commands placed inside ‘(‘ and ‘)’ separated by ‘;’in a sub-shell under the current shell. They have the same PID
( )SignificanceGroup
$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
Command cmd2 is executed only when command cmd1 fails
cmd1 || cmd2
Metacharacter coming first gets higher priority
cmd1 || cmd2 && cmd3
Command cmd2 is executed only when command cmd1 succeeds
cmd1 && cmd2SignificanceGroup
$grep ems2000 prog.sh && echo “String found”String found$ grep ems99876 prog.sh || echo “String not found”String not found
String Handling
Extracts the fourth and fifth character from the string
$ expr “Subhendu” : ‘…\(..\)’he
Locates position of character ‘l’within string : Anamoly
$expr “Anamoly” : ‘[^d]*l’6
Finds the length of the string : Training
$ expr “Training” : ‘.*’8
SignificanceExample
Extracts the fourth and fifth character from the string
$ expr “<string>” : ‘…\(..\)’
Locates position of character <char> in the string <string>
$expr “<string>” : ‘[^d]*<char>’
Finds the length of the string : <string>
$ expr “<string>” : ‘.*’SignificanceSyntax
Conditional Parameter Substitution
Syntax : {<variable> : <option> <stg>}
Variable <var> is evaluated to <stg> if it is undefined or assigned a null stringEcho “Enter directory to be copied:\c”read dirtodir_child=${dirto:-/home/ems2000/scripts}
IF dirto is null or not set, dir_child=/home/ems2000/scripts. The value of dirto is still null
-
Variable <var> evaluates to <stg> if <var> is defined and a non-null string is assigned to it.dir1=`ls`echo ${dir1:+”This directory is not empty”}
+
SignificanceOptions
Evaluates the parameter if the variable is assigned and non-null, otherwise it echoes the string following it. The shell is also terminated.This option is useful in terminating a script if the user fails to 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>
If the variable <var> is null, it is assigned the string <stg>.echo “Enter filename:\c”read flnamegrep $pattern ${flname:=emp.lst}IF flname is null, flname=emp.lst
=SignificanceOptions
Merging Streams
Standard output1Standard error2
Standard input0SignificanceSymbol
Usually, the inputs required for a program are typed in by user in terminal, all the outputs in terms of messages and all the error messages ( shell genarated) are shown in the terminal .
But, one can make a program take inputs from a file, store outputs(messages) and / or error messages in a file, instead of displaying them in a terminal.
All the output messages and prompts will be taken into file : sclist. However, the inputs to the program will be received from terminal.
$ prog2.sh > sclistSignificanceExample
$ cat prog2.shecho "Enter pattern:\c"read patternecho “\nPattern is $pattern"
$ prog2.sh > outlist # Enter pressedPatternfromterminal # written from terminal$
$ cat outlistEnter pattern:Pattern is Patternfromterminal$
All the inputs prompted by program prog1.sh are taken from file : namelist
$ prog1.sh <namelist
SignificanceExample
$ cat prog1.shecho "Enter first name:\c"read fnameecho “\nEnter last name:\c"read lnameecho “\nWelcome $fname $lname"
$ cat namelistSumanDas
$ prog1.sh <namelistEnter first name:Enter last name:Welcome Suman Das
All the inputs prompted by program prog1.sh are taken from file : namelist and all the output messages are stored in file : sclist
$ prog1.sh < namelist > sclistSignificanceExample
$ cat prog1.shecho "Enter first name:\c"read fnameecho “\nEnter last name:\c"read lnameecho “\nWelcome $fname $lname"
$ cat namelistSumanDas
$ prog1.sh <namelist >sclist$ cat sclistEnter first name:Enter last name:Welcome Suman Das
Send the standard output to the destination of standard error
$ 1>&2SignificanceExample
$ cat prog2.shans=ywhile [ "$ans" = "y" -o "$ans" = "Y" ]doecho "\n Enter city:\c" 1>&2read cityecho "$city"echo "\n More(y/n):\c" 1>&2read ansdone
$prog2.sh > out.1Enter city:calcuttaMore(y/n):yEnter city:bombayMore(y/n):n
$cat outlistcalcuttabombay
Send the errors to the destination of standard output$ 2>&1SignificanceExample
$ cat prog3.shecho "Searching all the .kklfiles"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
$cat out.2Searching all the .kkl files*.kkl not found
Using subroutines
Using subroutines is one of the most popular practice in any programming language.
Codes in a subroutine does a piece of job. It can take inputs from an user, do some jobs and can return some value to the calling program.
Subroutines are used to:-
• Modularize program sections.
•Perform repeatitive jobs.
•Do Calculations.
Syntax:-<subroutine_name> {
<actions>}
function <function_name> {<actions>
}
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.shshow_name() {case "$1" in[bB]) tput bold
echo "$2";;[lL]) tput rev
echo "$2";;*) tput rmso
echo "$2"esac
}show_name b `whoami`show_name l `hostname`show_name n `uname -r`show_name n “The End”
Passing values to subroutines
$ incr_val() {sum=`expr $1 + 1`echo $sum
}
no=12no=`incr_val $no`echo value is $no
Subroutine returning values
$ prog2.shvalue is 13