Create Shell Scripts.ppt
-
Upload
akdenizerdem -
Category
Documents
-
view
218 -
download
0
Transcript of Create Shell Scripts.ppt
-
8/16/2019 Create Shell Scripts.ppt
1/71
-
8/16/2019 Create Shell Scripts.ppt
2/71
Create Shell ScriptsOverview
− There are reasons to understand and createshell scripts
You can automate many daily system tasks which
reduces time and effort to manage the systemThey control the boot procedure and other systemfunctionsShell scripting is relatively easy to learnThey run on almost every !"#$like operatingsystem
− There are also some minor disadvantagesShell scripts are slow compared to other languagesThey can use a lot of %& power
-
8/16/2019 Create Shell Scripts.ppt
3/71
Create Shell Scripts• O!"ectives
'( nderstand the %ourse &ro)ect*( se Basic Script +lements,( se -ariables and %ommand Substitution.( se %ontrol Structures/( se Arithmetic 0perators1( 2ead ser "nput3( se Arrays4( 5inali6e the %ourse &ro)ect7( se Advanced Scripting Techni8ues'9( Learn About seful %ommands in ShellScripts
-
8/16/2019 Create Shell Scripts.ppt
4/71
-
8/16/2019 Create Shell Scripts.ppt
5/71
Use #asic Script $lementsThe #asic %ules of Shell Scriptin&
− You can also run the script without setting thepermissions using sh or !ash
*!in*sh is a symbolic link to *!in*!ash$xample+ sh script)sh or !ash script)sh
− The script directory must be in the user>s searchpath
0therwise must bes started using full path names A good way to do this is to create a *!in directory in theusers home directory and add it to the users ,'T-
− ?o this in the .)!ashrc− $xample @ export ,'T-/0,'T-+.*!in
− Always give script files a )sh extension so youknow that it is a shell script
-
8/16/2019 Create Shell Scripts.ppt
6/71
Use #asic Script $lementsThe %e1uest Tracker $ntries in the 2ail!ox3ile
− +ach mail message start with the line3rom wwwrun4linux5rwke)site )))
−
+ach mail message ends with 6Ticket which is theactual content of the mail'll other lines are headers which carry meta data andtransportation details of the mail
− The 6Ticket line has the information of the
re1uest su!mitted throu&h the we! interface andhas multiple fields separated !y the 7 89 character The fields have the followin& meanin&Su!"ect of %e1uest 8 $mail 'ddress of Sender 8%e1uest Cate&ory 8 :escription of %e1uest
-
8/16/2019 Create Shell Scripts.ppt
7/71
Use #asic Script $lementsThe %e1uest Tracker $ntries in the 2ail!ox3ile
− Start with a script that simply outputs themail!ox file to the terminal
6;*!in*!ash6!asic
-
8/16/2019 Create Shell Scripts.ppt
8/71
-
8/16/2019 Create Shell Scripts.ppt
9/71
Use #asic Script $lementsThe %e1uest Tracker $ntries in the2ail!ox 3ile
− 'nother improvement would !e to add a startmessa&e to the script
6;*!in*!ash6!asicelcome to the %e1uest tracker) nThe
followin& are open re1uests+ n9cat *var*spool*mail*&eeko 8 &rep @6@
−
The echo command is used to output text whichis enclosed in double 8uotes to the terminalThe option 5e lets echo interpret backslash se8uencesThe se8uence n is used to creates a line break
-
8/16/2019 Create Shell Scripts.ppt
10/71
Use #asic Script $lementsThe %e1uest Tracker $ntries in the2ail!ox 3ile
− #ackslash se1uences that can !e usedwith echo 5 Outputs a !ackslash a 5 Outputs an alert D!eep toneE ! 5 Outputs a !ackspace c 5 Outputs a suppress trainin& new5line
f 5 Outputs a form feed n 5 Outputs a new5line r 5 Outputs a carria&e return t 5 Outputs a horiFontal ta! v 5 Outputs a vertical ta!
-
8/16/2019 Create Shell Scripts.ppt
11/71
Garia!les and CommandSu!stitution
Garia!les− -ariables are important in all programming
languages− They are memory locations
-
8/16/2019 Create Shell Scripts.ppt
12/71
Garia!les and CommandSu!stitution
Garia!les− The string : &eeko ; is assigned to the variable a
which is then used in the echo command− The important thin&s are
>hen you assi&n a varia!le "ust use the name ofthe varia!le>hen you access the data of a varia!le use the 0 !efore the varia!le name>hen you assi&n data to a varia!le there can !e
no spaces !etween the varia!le name? the / andthe data− The output would look like this &eeko4linux5rwke +.*Shell
-
8/16/2019 Create Shell Scripts.ppt
13/71
Garia!les and CommandSu!stitution
Garia!les− -ariables can contain strings and numbers as
well− #y default a varia!le can hold any kind of
data !ut it is possi!le to limit a varia!le toa specific type usin& the declare command
− It is also possi!le to assi&n the output of acommand to a varia!le or to use acommand directly where the output isneeded
Called a command su!stitution
-
8/16/2019 Create Shell Scripts.ppt
14/71
Garia!les and CommandSu!stitution
Command Su!stitution− eans that the output of a command is used
in a shell command line or a shell script− +xample that uses the output of the command
date to generate the output of the current date6;*!in*!ash6command
-
8/16/2019 Create Shell Scripts.ppt
15/71
Garia!les and CommandSu!stitution
Command Su!stitution− "nstead of printing the output of a command to
the screen with echo it can also be assignedto a variable
6;*!in*!ash6command
-
8/16/2019 Create Shell Scripts.ppt
16/71
Garia!les and CommandSu!stitution
%e1uest Tracker − !ow improve the 2e8uest tracker by
including the number of open re8uests
6;*!in*!ash6!asic
-
8/16/2019 Create Shell Scripts.ppt
17/71
Use Control StructuresIntroduction
− So far we have only learned to writescripts that run se1uentially from!e&innin& to end
− >e now learn how to use controlstructures to make the execution ofparts of the script dependent on certinaconditions or to repeat script parts
− >e will learn how toCreate #ranchesCreate Loops
-
8/16/2019 Create Shell Scripts.ppt
18/71
Use Control StructuresCreate #ranches
− ' !ranch in a script means that a part ofthe script is only executed under a certaincondition
− ' common control structure for this usesthe if command
if conditionthen
commandsfi− If the condition is true then one or more
commands are executed
-
8/16/2019 Create Shell Scripts.ppt
19/71
Use Control StructuresCreate #ranches
− The if statement can !e extended withan optional else statement
if conditionthen command=else commandA
fi− CommandA is executed when the if
condition is false
-
8/16/2019 Create Shell Scripts.ppt
20/71
Use Control Structures%e1uest Tracker
− !ew we add an if structure to the 2e8uestTracker to modify the start message so thatif there are more than '9 open re8uests theuser sees a warning message
6;*!in*!ash6controlelcome to the %e1uest tracker) nThere are 0tinum open
re1uests) You have to do somethin&; a9elseecho 5e 7>elcome to the %e1uest tracker) nThere are 0tinum open
re1uests+ n9ficat *var*spool*mail*&eeko 8 &rep @6@
-
8/16/2019 Create Shell Scripts.ppt
21/71
Use Control StructuresCreate #ranches
− The program test is used to test the condition− Almost all command line tools have a return
value of 9 which means that everything is 0C− Dhen a value other than 9 is returned an
error is indicated− An if condition is true when the program uses
for testing returns 9− !ote that the lines after then and else are
indented which is not re8uired but makes thecode easier to read
-
8/16/2019 Create Shell Scripts.ppt
22/71
Use Control StructuresCreate #ranches
− The program test can !e used for manythin&s
test ST%INH= / ST%INHA M The strin&s are e1ual
test ST%INH= ;/ ST%INHA M The strin&s are not e1ualtest INT$H$%= 5e1 INT$%H$%A M The inte&ers are e1ualtest INT$H$%= 5lt INT$%H$%A M INT$H$%= is less than INT$H$%Atest 5e 3IL$ M 3IL$ exists
− You can also use a different syntax for
test ? where the expression is put ins1uare !rackets and the test commandis left out
=A 5e1 = P
-
8/16/2019 Create Shell Scripts.ppt
23/71
Use Control StructuresCreate #ranches
− Dith if more complex structures canalso !e used !y usin& elif to add moreconditions in case the one in theinitial if statement was not true
if test 0num!er 5e1 = then echo 7The value is = 9 elif test 0= 5e1 A then echo 7The value is A 9 else echo 7I don@t know9 fi
-
8/16/2019 Create Shell Scripts.ppt
24/71
Use Control StructuresCreate #ranches
− Another way to create multiple branchesis the case statement
The expression in a variable is comparedwith a number of expressions%ommands are executed for the firstexpression that matches
case 0num!er in = E echo 7The value is = 9QQ A E echo 7The value is A 9QQ RE echo 7I don@t know9 esac
-
8/16/2019 Create Shell Scripts.ppt
25/71
Use Control StructuresCreate Loops
− ' loop is often used when a task needs to !erepeated more than once since it is !etterthan repeatin& the same code multiple times
There are several options for implementin& a loop− Can use the for loop where the line with for
determines how many times to execute thecommands
>ith each pass of the loop the varia!le &ets the
next value in the list after in for varia!le in element= elementA elementB do commands done
-
8/16/2019 Create Shell Scripts.ppt
26/71
Use Control StructuresCreate Loops
− 'n example for loop 6;*!in*!ash 6for
-
8/16/2019 Create Shell Scripts.ppt
27/71
Use Control StructuresCreate Loops
− The list defined after in is not always static− The followin& example loops throu&h all
files in the current workin& directory andrenames files from upper to lower case
The R is expanded to a list of all the files !y theshell
6;*!in*!ash 6for
-
8/16/2019 Create Shell Scripts.ppt
28/71
Use Control StructuresCreate Loops
− Another way of creating a list usescommand substitution
− The followin& example uses find tocreate a list of all )mpB files in thecurrent directory and all su!directoriesand they are deleted
6;*!in*!ash 6for
-
8/16/2019 Create Shell Scripts.ppt
29/71
Use Control StructuresCreate Loops
− Another loop construct is the while loop wherethe commands are executed as long as thecondition is true
while condition do commands done
− 'nother loop construct is the until loopwhere the commands are executed until thecondition !ecomes true
until condition do commands done
-
8/16/2019 Create Shell Scripts.ppt
30/71
Use Control StructuresCreate Loops
− >e use the while loop to make theoutput of the %e1uest Tracker morereada!le
6;*!in*!ash6while
-
8/16/2019 Create Shell Scripts.ppt
31/71
Use Control StructuresCreate Loops
− $xplanation of while
-
8/16/2019 Create Shell Scripts.ppt
32/71
Use Control StructuresCreate Loops
− $xplanation of while
-
8/16/2019 Create Shell Scripts.ppt
33/71
Use 'rithmetic OperatorsOverview
− Shell scripts often use values assigned tovariables for calculations
− The Bash shell comes with built$in supportfor arithmetic operations but with thefollowing limitations
0nly operations with whole numbers All values are signed 1.$bit values
− Dhen using Bash you might need to useexternal commands
Always perform slower than built$in commands
-
8/16/2019 Create Shell Scripts.ppt
34/71
Use 'rithmetic Operators2ethods and 3ormats
− &ossible methods and formats for doing '/#(=− se the external command expr
'/Jexpr 0# ( = J− se the Bash built$in command letlet '/90# ( = 9− se arithmetic expressions inside parentheses or
brackets '/0DD# ( = EE or '/0 # ( = P− se the built$in command declare declare 5i ' Q declare 5i # '/#(=
-
8/16/2019 Create Shell Scripts.ppt
35/71
Use 'rithmetic Operators%e1uest Tracker
− se an arithmetic operator to assign anumber to each 2e8uest tracker re8uest
6;*!in*!ash
6arithmetic
-
8/16/2019 Create Shell Scripts.ppt
36/71
%ead User InputOverview
− 0ne way to read user input is to use the read command which stores the read input in avariable which can then be used to process
the user inputread G'%I'#L$− The script pauses at this point waiting for the
user input and then the $nter key−
To tell the user what is expected use the echocommandecho 7,lease enter a value for the varia!le+9
read G'%I'#L$
-
8/16/2019 Create Shell Scripts.ppt
37/71
%ead User Input%e1uest Tracker
− +xtend the 2e8uest Tracker to be moreinteractive by allowing the user to enter acommand after the open re8uests havebeen displayed
6;*!in*!ash6read
-
8/16/2019 Create Shell Scripts.ppt
38/71
Use 'rraysOverview
− Arrays are basically variables that holdmore than one value and use anumerical index in s8uare brackets toidentify the value in the arry
lines =P/9-ello >orld9− To access a value in an array specify the
index and put curly braces around thearray name
echo 0Dlines =PE
-
8/16/2019 Create Shell Scripts.ppt
39/71
Use 'rrays%e1uest Tracker
− +xtend the 2e8uest Tracker to usearrays
6array
-
8/16/2019 Create Shell Scripts.ppt
40/71
Use 'rrays%e1uest Tracker
− +xtend the 2e8uest Tracker to usearrays
6array
-
8/16/2019 Create Shell Scripts.ppt
41/71
Use 'rrays%e1uest Tracker
− In the while loop the re1uests are stored in the array 0lines and the value of the index 0re1nr is incremented in everycycle of the while loop
− In the for loop the content of the array is echoed to theterminal usin& a different syntax of the for loop
− 3or DDi/=Qi /0V6lines 4PWQi((EE means that the loop runs aslon& as the varia!le 0i is smaller or e1ual to the num!er ofelements in the 0lines array
0V6lines 4PW is a way to access the
num!er of elements in the array− 't the end of the while loop the array lines are deleted
usin& unset command
-
8/16/2019 Create Shell Scripts.ppt
42/71
Use 'dvanced Scriptin&Techni1ues
Introduction− "n this ob)ective we learn some
advanced scripting techni8ues to helpsolve common script developmentproblems
se Shell 5unctions2ead 0ptions with &etopts
-
8/16/2019 Create Shell Scripts.ppt
43/71
Use 'dvanced Scriptin&Techni1ues
Use Shell 3unctions− Sometimes you need to preform a task
multiple times in a shell script can this can bedone using functions
− Shell functions act like script modules andmake an entire script available under onename
− They are normally defined at the beginning of
a script− %an store several functions in a file and
include this file where the functions are needed
-
8/16/2019 Create Shell Scripts.ppt
44/71
Use 'dvanced Scriptin&Techni1ues
Use Shell 3unctions− Basic Syntax
functionname DE V commands commands
W− To generate a function using the function
command function functionname V
commands commandsW
− The function name can be any regular characterstring
-
8/16/2019 Create Shell Scripts.ppt
45/71
Use 'dvanced Scriptin&Techni1ues
Use Shell 3unctions− A simple function that creates a directory
and then changes to that directorymcd DE V mkdir 0= cd 0=W
− The function can be called in a shell
script ))) mcd *tmp*new
-
8/16/2019 Create Shell Scripts.ppt
46/71
Use 'dvanced Scriptin&Techni1ues
Use Shell 3unctions− The parameter *tmp*new
-
8/16/2019 Create Shell Scripts.ppt
47/71
-
8/16/2019 Create Shell Scripts.ppt
48/71
-
8/16/2019 Create Shell Scripts.ppt
49/71
Use 'dvanced Scriptin&Techni1ues
%ead Options with &etopts− If a parameter is expected the option (such
as -m maxvalue) t hen it must be followedby a “ :” in the string, such as getopts m:
− he optionstring is followed by a variable to which all of the command line optionsspecified are assigned to as a list
− he getopts command is mostly used in a
while loop together with a case statementto define which command to execute for agiven option
-
8/16/2019 Create Shell Scripts.ppt
50/71
Use 'dvanced Scriptin&Techni1ues
%ead Options with &etopts− he getopts command is mostly used in a
while loop together with a case statementto define which command to execute for a
given optionwhile getopts abc: variabledo
case !variable in a ) echo “ he option -a was used"” ##
b ) echo “ he option -b was used"” ##
c ) option$c%”!&' *” echo “ he option -c has been set"” ##
esac doneecho !option$c
-
8/16/2019 Create Shell Scripts.ppt
51/71
Useful Commands in ShellScripts
Introduction− This ob)ective gives an overview of useful
commands that can be used in shell scriptsand is intended as a reference
se the cat commandse the cut commandse the date commandse the &rep and e&rep commandse the sed commandse the test commandse the tr command
-
8/16/2019 Create Shell Scripts.ppt
52/71
Useful Commands in ShellScripts
The cat Command− Dhen combined with the here operator
< a good choice to output severallines of text from a script
− "nteractively it is mostly run with afilename as an argument and prints tostandard output
-
8/16/2019 Create Shell Scripts.ppt
53/71
Useful Commands in ShellScripts
The cut Command− se this command to cut out sections of
lines from a file so only the specified sectionis printed on standard output
%an specify single or several sections− The command is applied to each line of text
as available in a file or on standard output− se cut 5f to cut out text fields− se cut 5c to work with specified characters− se cut 5d to specify a field separator
' ta! is the default
-
8/16/2019 Create Shell Scripts.ppt
54/71
Useful Commands in ShellScripts
The cut Command− +xample to cut and print the first field to
standard outputcut 5d + 5f= *etc*passwdroot!indaemon)))
− $xample to take the output of ls and cut
out everythin& from the thirty5fifthcharacter? pipe it to sort and have is sorted!y file siFe
ls 5l somedir* 8 cut 5c B 5 8 sort 5n
-
8/16/2019 Create Shell Scripts.ppt
55/71
Useful Commands in ShellScripts
The date Command− Use whenever have a need to o!tain a
date or time strin&− Output with no options
date3ri Sep B = +=X+=A C$ST A
− The output can !e formatted usin&options
− The 5I option prints the date and timein ISO format
Same as (KY5Km5Kd
-
8/16/2019 Create Shell Scripts.ppt
56/71
Useful Commands in ShellScripts
The &rep and e&rep Commands− Used to search one or multiple files for
certain patterns− The syntax is &rep searchpattern filename )))− The output is all lines that contain the pattern− 2any options are availa!le to do thin&s like
Only print the line num!ers,rint the line alon& with leadin& and trailin&context lines
− Search patterns can !e supplied as re&ularexpressions
− The re&ular &rep has limited capa!ilities
f l d h ll
-
8/16/2019 Create Shell Scripts.ppt
57/71
Useful Commands in ShellScripts
The &rep and e&rep Commands− Can use e&rep or &rep 5$ for more complex
patterns− The re&ular expressions are in the standard
re&ex syntax− To avoid havin& special characters in the
seach patterns interpreted !y the shellenclose the pattern in 1uotation marks
tux4:'= +. e&rep D!8#Elur! fileR !ash+ syntax error near unexpected token 8
tux4:'= +. e&rep 7D!8#Elur!9 fileRfile=+!lur!fileA+#lur!
f l d h ll
mailto:tux@DA1mailto:tux@DA1mailto:tux@DA1mailto:tux@DA1
-
8/16/2019 Create Shell Scripts.ppt
58/71
Useful Commands in ShellScripts
The sed Command− The sed program is a stream editor used
from the command line not interactively− ?oes text transformations on a line$by$
line basis− %an specify sed commands directly on
the command line or in a special
command script loaded by the program− The syntax is sed editin&5command
filename
f l C d i Sh ll
-
8/16/2019 Create Shell Scripts.ppt
59/71
Useful Commands in ShellScripts
The sed Command− The available editing commands are single$
character arguments such asd+ ?eletes+ Substitute
-
8/16/2019 Create Shell Scripts.ppt
60/71
Useful Commands in ShellScripts
The sed Command− 0ptions can be specified to influence the
overall behavior of the program5n? 551uit? 55silent
−
#y default? prints all lines on standard output− This option suppresses the output so only printslines for which the p editin& option has !een &ivento re5ena!le printin&
5e command+ 5e command ))))− Necessary when specifyin& two or more editin&
commands− 2ust !e inserted !efore each additional editin&
command
U f l C d i Sh ll
-
8/16/2019 Create Shell Scripts.ppt
61/71
Useful Commands in ShellScripts
The sed Command− 0ptions can be specified to influence the
overall behavior of the program5f filename
− Can specify a script file from which to read its editin&commands
− Sometimes need to specify exact line orlines to !e processed and there arespecial characters you can use
0 stands for the last line− Command to print only lines = throu&h sed 5n @=? p@ somefile
U f l C d i Sh ll
-
8/16/2019 Create Shell Scripts.ppt
62/71
Useful Commands in ShellScripts
The sed Command− %ommand to delete everything from line '9 to the end
and print the first 7 linessed @= ?0d@ somefile
− 2egular expressions can be used to define addresses
for a command and must be enclosed in forwardslashes
− %ommand to print all lines that have the pattern2urphy)R in them
sed 5n @*2urphy)R*p@ somefile− To perform several editin& commands for the same
address need to enclose the commands in !races sed @=?= Vcommand+ Qcommand W@
U f l C d i Sh ll
-
8/16/2019 Create Shell Scripts.ppt
63/71
Useful Commands in ShellScripts
The sed Command− Important editin& commandsd sed = ?0d file elete linea sed .a/text/text. file Insert text before specified linei sed .i/text/text. file eplace specified lines with the text c sed . 000,!c/text. file eplace specified lines with the text s sed s1x1y1option 2earch and replace
'atterns are regular expressionsy sed y1abc1xy31 (yan4) eplace every character
from the set of source characters with the character in the same position in the destination characters
U f l C d i Sh ll
-
8/16/2019 Create Shell Scripts.ppt
64/71
Useful Commands in ShellScripts
The sed Command− Options can use with the s command
for search and replaceI o not distinguish between uppercase and lowercase
lettersg eplace globally n eplace the nth matching pattern only
p 'rint the line after replacing w 5rite the resulting text to the specified file rather than printing it on stdout
− eplace the first colon in each linewith a space
sed .s1:1 1. 1etc1passwd
U f l C d i Sh ll
-
8/16/2019 Create Shell Scripts.ppt
65/71
Useful Commands in ShellScripts
The sed Command− eplace all colons in all lines with a space sed .s1:1 1g. 1etc1passwd − eplace only the second colon in each line with a
spacesed .s1:1 1 . 1etc1passwd
− eplace all single vowels with double vowelsshowing how matched patterns can be referencedwith “/+” if the search pattern is given in
parenthesessed -n .s1/(6aeiou7/)1/+/+1Igp.
he I option ensures that the case is ignored he g option causes characters to be replaced globally he p option prints all lines processed in this way
U f l C d i Sh ll
-
8/16/2019 Create Shell Scripts.ppt
66/71
Useful Commands in ShellScripts
The test Command− 8xists as both a built-in command and as an
external command − 9sed to compare values and to chec4 for file and
their properties, such as if it exists, isexecutable, etc"
− If the tested condition is true then test returns anexit status of 0 and if not true returns an exitstatus of +
−
In shell scripts mainly used to declare conditionsto influence the operation of loops, branches andother statements
− he test syntax is test condition
U f l C d i Sh ll
-
8/16/2019 Create Shell Scripts.ppt
67/71
Useful Commands in ShellScripts
The test Command− est whether a file exists -e ile exists
-f ile exists and is a regular file-d ile exists and is a directory -x ile exists and is an executable file
− ;ompare files-nt
-
8/16/2019 Create Shell Scripts.ppt
68/71
Useful Commands in ShellScripts
The test Command− ;ompare integers -e= 8=ual
-ne ess than-ge *reater than or e=ual -le >ess than or e=ual
-
8/16/2019 Create Shell Scripts.ppt
69/71
U f l C d i Sh ll
-
8/16/2019 Create Shell Scripts.ppt
70/71
Useful Commands in ShellScripts
The tr Command− ;ommand translates (replaces) or deletes
characters− eads from standard input and prints to
standard output − can replace regular characters or se=uences of
regular characters and special characters, li4e /t or /r
− 2tandard syntax is tr set+ set where the
characters in set+ are replaced with thecharacters in set − To replace all lowercase characters with uppercase cat text5file 8 tr a5F '5
U f l C d i Sh ll
-
8/16/2019 Create Shell Scripts.ppt
71/71
Useful Commands in ShellScripts
The tr Command− To delete characters from set' and print
the rest to standard output tr 5d set=− To delete the percent si&n from the
ori&inal value of G'% and assi&n theresult as the new value of the varia!le
G'%/@echo 0G'% 8 tr 5d K@− %eplace a set of characters with a
sin&le character tr 5s set+ char