Professional Documents
Culture Documents
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.
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 $* $@
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.
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.
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.
#!/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"
1: 2:
Step
Run
back quotes, we can assign the output of a command to a variable: #! /bin/bash filelist=`ls` echo $filelist
Without
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
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
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)}