UNIX Shell Scripting: Y.V.S Prasad
UNIX Shell Scripting: Y.V.S Prasad
Y.V.S Prasad
An Operating System is the Interface between the
User (software) and the Computer (hardware).
USER OS COMPUTER
UNIX is a multiprogramming
Operating System. It permits
multiple people to run multiple
programs.
I/O management
data management
command execution
program development tools
portability
time sharing
security
communications
accounting
graphics
Internet
BASIC COMMANDS
$ clear
$ tput clear
$ date Date, Time and Time
Zone
$ date ‘+%D’ Date in dd/mm/yy
$ date ‘+%T’ Time in hh:mm:ss
$ date ‘+%Z’ Time Zone
$ pwd
$logname
$tty
$uname Name of UNIX
$uname –r Current Release of UNIX
$uname –n Host Name or Domain Name
$uname –a Available Information of
UNIX
$passwd
$who
Col. 1: user names
Col. 2: device names of the terminals
Col. 3, 4, 5: date and time of logging in
Col 6: machine name
$who –Hu u gives more detailed
info. and H with headers
$cal calendar of the current month
1. Simple Filters
2. Filters with Regular Expressions – grep and sed
3. Advanced Filtering using awk
Simple Filters include head, tail, tr, sort, uniq, cut,
paste, pr, comm, diff etc.
$ head emp.lst
Displays first 10 lines of the file, from the beginning.
Ex:
grep –i ‘abc’ emp.lst emp.lst1
SED is a multipurpose tool which combines the
work
of several filters.
Ex: sed options ‘address action’ file(s)
Addressing in sed is done in two ways:
1. By one or two line nos.
2. By specifying /pattern/
Line Addressing:
$ sed ‘3q’ emp.lst
Displays first 3 lines of the file and quits from sed.
$ sed –n ‘1,3p’ emp.lst
Displays first 3 lines of the file. (p and n must be
used)
$ sed –n ‘$p’ emp.lst
Displays the last line of the file
$ sed –n ‘1,2p
7,9p’ emp.lst
Displays selective groups of lines
$ sed –n ‘3,$!p’ emp.lst
Do not print the lines from 3 to the end of the file.
Using Multiple Instructions:
$ sed –n –e ‘1,2p’ –e ‘7,9p’ emp.lst
Putting instructions in a file:
$ cat > patfile
1,2p
7,9p
^d
$ sed –n –f patfile emp.lst
Context Addressing:
$ sed –n ‘/director/p’ emp.lst
Displays all the lines that contain ‘director’
$ sed –n ‘/director/, /manager/p’ emp.lst
Displays all the lines from director to manager
$ sed –n ‘1, /director/p’ emp.lst
Line nos. and context addresses can be mixed
$ sed –n ‘/^a/p’ emp.lst
Displays all the lines that start with ‘p’ (regular exp.)
Writing selected lines to a file:
$ sed –n ‘/director/w dlist’ emp.lst
$ sed –n ‘/director/w dlist
/manager/w mlist’ emp.lst
Text Editing:
Inserting i
Appendinga
Changing c
Deleting d
$ sed ‘1i\
abc \
pqr’ emp.lst
$ sed ‘1a\
abc\
pqr’ emp.lst
$ sed ‘1c\
abc’ emp.lst
$ sed ‘/director/d’ emp.lst
SUBSTITUTION:
$ sed ‘s/director/director1/g’ emp.lst
$ sed ‘1,5 s/director/director1/g’ emp.lst
MULTIPLE SUBSITUTIONS:
$ sed ‘s/i/m/g
s/x/y/g’ emp.lst
Named after its authors Aho, Weinberger and
Kernighan, awk, until the advent of Perl, was the most
powerful utility for text manipulation.
Syntax:
awk options ‘selection_crateria {action}’ file(s)
$ ps
Displays PID, TTY, TIME (cumulative processor
time that has been consumed since the process
started) and the CMD (process name)
$ ps –f
Displays full listing
$ ps –e or $ ps –A
All processes including user and system processes
$ ps –u user
Displaying the processes of a User
$ ps –a
Processes of all users excluding processes not
associated with terminal.
$ ps –l
Long listing showing memory related information
$ ps –t term
Displays processes running on the terminal
A process can be run in the background. This is
achieved by placing an & at end of the
command.
$ sort emp.lst –o emp.lst &
$ kill 105
$ kill –s KILL 105 (or) $ kill -9 105
$ kill –l
will display the list of signal names and their
nos.
A job is the name given to a group of process.
Process activity is related to kernel whereas the
job activity is related to shell.
$ wc –c /
Say that this command is taking too long, then
we can suspend this command by pressing
control-z.
[1] + stopped wc –c /
$ bg
forces the command to run in the background
$ jobs
will show the list of background jobs
A background job can be brought to the
foreground by fg command.
$ fg %1
$ fg %wc
similarly,
$ bg %2
$ bg %?perm(string perm)
we can terminate a job with kill command.
$ kill %1
$ kill –s KILL %wc
kills the wc command etc.
AT tells UNIX when to execute a set of
commands.
$ at 14:08
……
…….
control-d
$ at –l
gives the list of at jobs
$ at –r
to remove the list of jobs from the queue.
Other formats of AT are:
$ at 15
$ at 5pm
$ at 3:06pm
$ at noon
$ at now + 1 year
$ 3:08pm + 1 day
$ 15:08 December 18, 2008
$ at 9am tomorrow
BATCH commands are executed as soon as the
system load permits.
$ batch
……..
……..
control-d
job 10411856731.b at Sun Dec 29 13:14:33 2009
CRON is a system process (daemon) that
executes programs at regular intervals. It mostly
dormant, but every minute, it wakes up and
looks into a control file called the crontab file.
Creating a crontab file:
create a file cron.txt with the following 6 fields.
$ vi cron.txt
00-10 17 * 3,6,9 5 wc –c abc
field 1: 00-59 minutes field 4: month
field 2: 1-24 hours field 5: Friday
field 3: 1-31 day field 6: command
$ crontab cron.txt
$ crontab –l
display the contents of crontab file
$ crontab –r
removes the contents of crontab file
$ time sort emp.lst –o emp.lst
displays 3 times
real: time elapsed from the invocation of the
command until its termination.
user: time spent by the program in executing
itself.
sys: time spent by the kernel
TRAP traps the signals and executes the
commands. It is normally put at the beginning
of the shell script.
$ trap ‘command_list’ signal_list