You are on page 1of 32

Shell Script and Awk Programming

by abhradita

UNIT II

INTRODUCTION TO SHELL
A shell is a program which reads and executes commands for the user. Shells also usually provide features such job control, input and output redirection and a command language for writing shell scripts. A shell script is simply an ordinary text file containing a series of commands in a shell command language

FUNCTIONS OF SHELLS
It can act as a command. It acts as a command interpreter. It can act as a programming language. It permits customization of users environment. It provides i/p and o/p redirection and piping facilities. It performs substitutions of variables , file name and commands.

TYPES OF SHELLS
Different people have implemented the interpreter function of shell in different ways. This gave rise to various types of shells. Bourne Shell:-Bourne shellBourne shell was created by steve bourne. It is most popular thus it is bundled with every unix system. It is known the programs as bash. C Shell:- This shell is used extensively used by unix programmers.It was created by Bill Joy.

CONTINUE
It has advantages over the bourne 1)Previously typed commands can be recalled ,since c shell keep track of all commands issued at the command line.(same as doskey in dos os). 2)It provides built in array support. It is known by the program csh.

CONTINUE
Korn shell:- It is the subset of Bourne shell.It was designed by David corn at AT & T bell labs. It provides string handling ,simplified menu generation.It is faster than bourne shell. Drawback of korn shell is that it uses the system resources heavily.

SHELL VARIABLES
Shell variable are an integral part of shell programming. They provide the ability to store and manipulate information within a shell program. The shell supports variables that are useful both in the command line and shell script. A variable assignment of the form variable=value(no space around =), but its evaluation requires the $ as prefix to the variable name - $ x=5 (no $ required for assignment) $ echo $ x(but needed for evaluation). 5

CONTINUE..
A variable can also be assigned the value of the another variable $ y=$x(assigning a value to the another variable) $ echo $y 5 When the shell reads the command line ,it interprets any word preceded by a $as a variable.

RULES FOR BUILDING SHELL VARIABLES


A variable name is any combination of alphabets ,digits and an underscore. No commas or blanks are allowed within a variable name. The first character of a variable name must either be an alphabet or an underscore. Variable names are case sensitive. i.e. Name and NAME are different variables.

META CHARACTERS OF SHELL


Meta characters are special symbols used in unix for creating and storing information in files. Classification of Meta characters.

TYPE FILE NAME SUBSTITUTION I/O REDIRECTION PROCESS EXECUTION QUOTING METACHARACTERS POSITIONAL PARAMETERS SPECIAL CHARACTERS METACHARACTERS ? * [.-.] [!.] > < >> ; ( ) & || && \ $1 $2 $3 $4 $5 $6 $7 $8 $9 $$ $! $? $- $# $0 $* $@

FILE NAME SUBSTITUTION META CHARACTERS


a) * :- a wild card character , it can represent any combination of any number of characters . A null or no character may also qualify for representation by a ( *.ls) gives a complete list of files in the current directory except hidden files that start with a .(.lst or ..txt etc). b) ? :- It is used to match files with a prefix of any one character and suffix of $ ls cha?ter(prefix of cha and suffix of ter(with any middle character)

CONTINUE..
c)[..]:- To match a file starting with any letter .. through .. $ ls [c..r]* to match a file starting with c through r . Or $ ls [a,b,c] to match a file starting with a,c and c. d)[!. ]:- To exclude a match with the letter .. $ ls [!a]* to exclude a match with the letter a

I/O REDIRECTIONS
I/O redirections are the special characters specify from where , i/p is to be picked up and where the o/p is to be sent. Example 1.cat abc > xyz(if xyz exists the shell overwrites the content of abc to xyz Example 2.cat abc >> xyz(if xyz exists the shell copies the content of the abc after the contents of xyz.

CONTINUE..
Example3.$wc <sample.txt (shell reads sample.txt and count the no of lines ,no of words and no of characters) Example4.$cat < abc>>sample.txt (shell copies the contents of abc after the contents of sample.txt.

PROCESS EXECUTION
It provides various different ways of execution of commands. By process execution we can run more than one command at the $ prompt in one stroke with a semicolon. Example1.$ls;who;cat abc The result in the execution of ls first, then who and at last cat command displays the contents of abc.

QUOTING META CHARACTERS


1.single quotes :- $echo today is date Today is mon jan 09 10:20:17 2012 2.double quotes:- $x=5 $echo your birth date is $x Your birth date is 5

POSITIONAL PARAMETERS

Positional parameters:- $1 through $9 are ten shell variable called positional parameters , which automatically collect the arguments passed at the command line.

SPECIAL PARAMETERS
$$:- PID of current shell. $!:- PID of last background process $?:- Exit status of last executed command. $-:- Current shell settings. $#:-Total number of positional parameters. $0:-Name of the command being executed, $*:-List of all shell arguments. $@:-all arguments individually with double quotes.

SHELL ENVIRONMENT

Unix system is controlled by a number of shell variables which are separately set by the system , some during the boot process and some after logging in. These variable are called system variables or environment variables. We can never alter their values to our liking. The set statement displays a complete list of these variables.

SOME OF THE IMPORTANT SYSTEM VARIABLES


HOME variable:- When we log in , unix normally places us in the directory named after our login name .This directory is called the home or login directory , and is available in the variable Home $ echo $HOME Result :- /home/abhra PATH variable:- path is a variable which instruct the shell about the route it should follow to locate any executable command.(dos uses a path variable name(AUTOEXEC.BAT). If we ant to include the directory /home/abhra in our search list we need to redefine this variable PATH=$PATH:/home/abhra

CONTINUE

MAIL variable:- MAIL determines the location of all incoming mail addresses. This directory is searched by the shell every time a user logs in. If any information is found , the shell informs the user with a message you have mail. TERM variable:- Term indicates the terminal type being used. There are some utilities that are terminal dependent, and they require to know the type of terminal we are using. Example is VI editor.

CONTINUE..

LOGNAME variable:- This variable shows our username. IFS variable:-IFS contains a string of that are used as word separators in the command line. The string generally consists of the space,tab and the new line characters. All these characters are invisible but the blank line shows that the newline character is a part of this string. (octal values). We cant alter the IFS variable but advance shell programmers can make temporary changes to IFS variables.

SHELL SCRIPTS
A shell script is a text file with Unix commands in it. Shell scripts usually begin with a #! and a shell name (complete pathname of shell).

Pathname

command. The shell name is the shell that will execute this script. E.g.#!/bin/bash If no shell is specified in the script file, the default is chosen to be the currently executing shell.

of shell be found using the which

SIMPLE SHELL SCRIPTING

#!/bin/sh # this is a comment echo "The number of arguments is $#" echo "The arguments are $*" echo "The first is $1" echo "My process number is $$" echo "Enter a number from the keyboard: " read number echo "The number you entered was $number"

SAMPLE EXAMPLE (CONTD.)

To run the script:


Step
$

1: 2:

chmod u+x myscript(name of the file)

Step

Run

the script: $ ./myscript

Each line of the script is processed in order.

SHELL SCRIPTS (CONTD.)

Assigning the output of a command to a variable:


Using

back quotes, we can assign the output of a command to a variable: #! /bin/bash filelist=`ls` echo $filelist

Without

using shell script $ filelist=ls $echo $filelist

SCRIPT2
Write a shell script that prints the current date and your user name. #!/bin.bash echo current date is:date echo self login user name:who am I

REMAINING PART OF AWK PROGRAMMING


Looping with for :- For loop has two forms First form :- for (k=1 ; k<=count ; k++) It consists of 3 components-(a) Initializes the value of any variable (b) Checks the condition with every iteration. (c ) Sets the increment or decrement for every iteration. Example:$ awk F| { count++ > for (k=1 ; k<=count ; k++) > print k , count , $6 } emp.lst

CONTINUE

Second Form :- This form is different from first form generally used in other programming languages . for(k in array) > commands |k is a subscript , it is not restricted to use integer values as subscript but subscript can even be a string. Example :$ awk F| { kount[$3]++} > END { for( desig in kount) > print desig , kount[desig] emp.lst Result :- Chairman 1 D.g.m 2 G.m 2 Director 2

The while loop is similar to for loop in execution point of view ,as used in other programming languages. syntax:K=0 while(k<condition) { commands; K++ } Example:$ awk F | {kount++ K=o while(k<=kount) { print ~ K++ } print $2 } emp.lst

WHILE LOOP

BUILT IN FUNCTIONS IN AWK PROGRAMMING


Built in Functions int (x) sqrt(x) length(x) substr (stg , m , n) index(s1,s2) system( cmd) Description Returns integer value of x Returns square root of x Returns length of x Returns portion of string of length n ,starting from position m in string stg Returns position of string s2 in s1 Runs unix command cmd and returns its exit status

EXAMPLES OF SOME BUILT IN FUNCTIONS


Length:$awk F length($4)<=5 emp.lst Result :- sales Admin Sales Substr(stg,m,n):$ awk F| substr($6,1,2) Result :- 35 45 44 56 etc System(cmd):$ awk BEGIN { system(tput clear)}

You might also like