Shell Script - PowerPoint PPT Presentation

About This Presentation
Title:

Shell Script

Description:

notice each line ends with a semi-colon ... 'words', 'separated', 'by', 'colons') Using Command-line arguments $ vi printfirstarg ... – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 52
Provided by: oitu
Category:
Tags: colons | script | shell

less

Transcript and Presenter's Notes

Title: Shell Script


1
Shell Script Perl Programming
2
Shell Programming
  • A shell script is a text file that contains Linux
    (UNIX) commands, which you enter using any
    standard editor.
  • You can then execute the commands in the file by
    using the filename as an argument to any dot (.)
    command.

3
Our first shell script
vi helloscript
i this is a simple script echo hello
world ltescgt w q
./helloscript hello world
4
Perl Variables
  • The primary distinction in Perl is not between
    data types (e.g. string, integer, real) but
    rather between singular and plural data.
  • Singular (scalar) strings and numbers
  • Plural (array) lists of strings or numbers
  • Variables do not need to be predefined

5
Variable example
  • welcome Hello world \n set a variable
  • print welcome print the variable
  • Notice that
  • - we dont predefine the variable
  • - the tells Perl that this is a scalar variable

6
Perl variable types
  • Type Character example Is a name for
  • Scalar cents An individual value
  • (number or string)
  • Array _at_ _at_large A list of values,
  • keyed by number
  • Hash interest A group of values,
  • keyed by string
  • Subroutine calcval A callable piece
  • of Perl code
  • Typeglob struck Everything named
  • struck

7
Another Variables Example
greetinghello echo greeting hello
echo greeting greeting
The set command will let you view all of your
currently defined variables.
8
Some interesting variables
  • Use the set command to see some of your defined
    variables (e.g. after you assign the value to
    greeting in previous slide).
  • Also check out some other important variables
    (type set more)
  • PATH
  • PS1

9
Starting to customize your settings
  • In your home directory (/home/ltusernamegt) create
    a file called .bash_profile
  • Insert into this two lines
  • PATH/usr/local/sbin/usr/binPATHHOME/bin.
  • PS1gt
  • The first tells the system where to look for
    programs (commands) to execute, the second sets
    your prompt
  • Default probably PS1\s-\v\ , use set to see
  • \s tells it to print what shell you are using
  • \v tells it to print what version
  • is the prompt you are telling it to use

10
Another variable example
gt schoolUTSA gt whereatyou are at school gt
echo whereat you are at UTSA gt
What would happen if gt echo whereat
whereat gt
11
Variables and scripts
gt vi greetvar echo Please enter a greeting read
greeting echo The greeting you entered was
greeting note optional quote gt
greetvar Please enter a greeting howdy The
greeting you entered was howdy gt
12
Arguments to scripts
gt vi greetargs echo The first argument of 0 is
1 echo the second argument is 2 echo the
third 3 echo and finally 4 gt
greetargs num1 number2 N3 simply N4 The
first argument of ./greetargs is num1 the second
argument is number2 the third N3 and finally
simply N4 gt
greetargs num1 num2 num3 The first argument
of ./greetargs is num1 the second argument is
num2 the third num3 and finally gt
13
Some simple math
gt vi add1 let num 1 quotes
important let num num 1 echo num gt
add1 2 3 gt
14
Some loops
gt vi loop3 again 1 while let again lt 3
you need the let do echo again howdy
let again again 1 done gt
loop3 1 howdy 2 howdy 3 howdy gt
15
Conditional statements
gt vi myls if 1 a watch spaces, need
after and before then ls -al
lists all files including hidden else ls
-l lists files in long format
fi gt
myls a ltlists files in long format including
hiddengt gt myls x ltlist files in long formatgt gt
myls ./myls unary operator expected ltlists
files in long formatgt gt
16
Reading from a file
gt vi list Line1 Line2 Line3 end gt
gt vi printlist read name while name ! end
do echo name read name done gt
printlist lt list Line1 Line2 Line3 gt printlist
list will have to c
17
Script for adding users
THE FILE 500 Abarshay Jbell Abelvis end
this is a script to create a bunch of
accounts read grpid read name let "userid grpid
1 while "name" ! end do echo name
userid grpid adduser -u userid -g grpid
-d /home/name -p "" -r name mkdir
/home/name chown name /home/name let
"userid userid 1" read name done
18
Perl
  • Practical Extraction and Report Language
  • Designed to provide convenience of shell scripts
    and power and flexibility of a programming
    language.
  • Perl programs are interpreted and executed
    directly like shell scripts.
  • Similar in many respects to the C programming
    language
  • Freeware, available from Free Software Foundation
    (comes with Linux)

19
Our first Perl Program
vi sample !/usr/bin/perl inputline
ltSTDINgt input text print ("inputline")
output the text
./sample test line test line
- symbol indicates comment, except ! in first
line which indicates the location of the program
interpreter. - notice each line ends with a
semi-colon - variable treated in a similar manner
as in shell scripts
20
Quotes and Escape Sequences
!/usr/bin/perl x "a string" y "This is
x" becomes "This is a string z 'This is
x' remains 'This is x print
("x\n") print ("y\n") print ("z\n") print
('z\n') print ("\a\LFREDDIE \Umary\n") print
("a quote \"in a string\"\n")
./sample a string This is a string This is
x z\nfreddie MARY a quote "in a string
21
Some more escape sequences
\a Bell (beep) \b backspace \e escape \E cancel
effect of \L, \U, \Q \L all following letters are
lowercase \n newline \Q do not look for special
pattern characters \r carriage return \t tab \U al
l following letters are uppercase \v vertical tab
22
Performing Math
vi math !/usr/bin/perl a 15 print
("a\n") a 4 5.1 print ("a\n") a 17
- 6.2 print ("a\n") a 2.1 6 print
("a\n") a 48 / 1.5 print ("a\n") a 2
3 print ("a\n") a 21 5 print
("a\n") a - a print ("a\n")
./math 15 9.1 10.8 12.6 32 8 1 -1
23
Performing Comparisons
11.0 lt 16 less than 16 gt 11 greater than 15
15 equals 11.0 lt 16 less than or equal
to 16 gt 11 greater than or equal to 15 !
14 not equal to a b logical OR true
if either is non-zero a b logical AND
true only if both are non-zero ! a logical
NOT true if a is zero 4 ltgt 1 returns 1
4gt1 3 ltgt 3.0 returns 0 they are equal 1
ltgt 4.0 returns -1 1lt4
24
Performing Comparisons - strings
aaa lt bbb less than bbb gt aaa
greater than aaa eq aaa equals aaa le
bbb less than or equal to bbb ge aaa
greater than or equal to aaa ne bbb not
equal to aaa cmp bbb similar to ltgt,
returns 1 aaa lt bbb aaa cmp aaa
returns 0 bbb cmp aaa returns -1 bbb gt
aaa What would be returned for 40 lt 8
25
Some assignment operators
a 9 most common assignment operator a
1 equivalent to a a 1 a - 1 same
as a a - 1 a same as a
1 a same as a 1 --a same as a
- 1 a-- same as a - 1 be careful on
the last four of these a 7 b a a
and b are both set to 8 b a a is now
8, but b is 7
26
Some cool things to do with strings
The . operator joins the second operand to the
first operand a be . witched a is
now bewitched The x operator makes n copies of
a string a t x 5 a is now
ttttt The operator matches patterns and can
substitute with s x /jkl/ returns true
if jkl is in x val s/abc/def/ replaces
abc with def The character matches 0 or more,
? Matches zero or 1 copy /jkl/ matches jl,
jkl, jkkl, jkkkl, /jk?l/ matches jl or jkl
only
27
Lists and Arrays
A list is a collection of values enclosed in
parentheses. ( 1, 5.3, hello, 2) contains
four values () an empty list Perl allows
you to store lists in array variables _at_array
(1, 2, 3) _at_ character specifies var as an
array _at_x (11, my string, 27.44) _at_x
y _at_x is now a list of one element
the value of y Obtaining the length of a
list _at_x (string1, string2, string3) y
_at_a y contains the LENGTH of _at_a
28
More about lists and arrays
Array Slices _at_x (1, 2, 3) _at_y _at_x0,1 _at_y
gets slice of _at_x containing 2 values (1,
2). NOTE, position 0 _at_x (1, 2, 3) _at_x4, 5
(10, 20) _at_x now has 6 elements, the
fourth being a null string _at_x (10, 20,
30) y _at_x1 y now has the value 20 _at_x
(10, hello) y Say _at_x1 to your
friends
29
More fun with lists and arrays
Using the built-in function sort() _at_x (this,
is, a, test) _at_x sort (_at_x) _at_x is now
(a, is, test, this) Note that sort() is
an ALPHABETIC sort, thus _at_a (100, 50, 2) _at_a
sort (_at_a) _at_a is really (100, 2,
50) The function reverse() reverse the
order _at_a (backwards, is, array,
this) _at_a reverse (_at_a) _at_a is now
(this, array, is, backwards)
30
Some final list and array functions
To create a single string from a list or array
variable use join() _at_x join( , this, is,
a, sentence) The first element contains the
character to glue the rest together, _at_x is
thus this is a sentence Youd get the same
results with _at_a (this, is, a) _at_x
join ( , _at_a, sentence) To undo the effects
of join(), use split() _at_y wordsseparatedby
colons _at_x split(//, y) _at_x is now
(words, separated, by,
colons)
31
Using Command-line arguments
vi printfirstarg !/usr/bin/perl Print(the
first argument is ARGV0\n)
printfirstarg 1 2 3 The first argument is 1
Note that ARGV0, the first element of the
ARGV array variable, does NOT contain the name
of the program. This is a difference between
Perl and C.
32
Controlling program flow
if (x 14) print(\x is 14\n) Two
way branching if (x 14) print(\x is
14\n) else print(\x is not 14\n)

33
Files
To open a file, use the function
open() open(MYFILE, /home/gbwhite/testfile)
The second argument is the name of the file you
want to open. You can supply the full pathname or
just the filename itself. By default, Perl
assumes you will open a file for reading. If
you want to open one for writing, put a gt
character in front of your filename open(MYFILE,
gt/home/gbwhite/writefile) If you want to
append instead of overwriting, use gtgt
34
More about handling files
The open() function returns true if it succeeded,
false otherwise. You can use the (logical OR)
function to test it open(MYFILE,
/home/gbwhite/testfile) die(unable to
open /home/gbwhite/testfile\n) This works
because the right side of a logical OR only
is executed if the left side is false.
35
More about files
To read from a file, enclose the name in angle
brackets line ltMYFILEgt This statement
reads a line of input. To write to a file,
print MYFILE (this is a test\n, this
too\n) Closing a file close(MYFILE)
36
Subroutines
Subroutines can appear anywhere in the program
but by convention generally go at the
end. !/usr/bin/perl thecount
0 getwords while (words0 ne )
stop when line is empty for (index 0
wordsindex ne index 1) thecount
1 if wordsindex eq the above line
really ? if(wordsindex eq the) thecount
1 getwords print
(Total number of occurrences of the
thecount\n) sub getwords inputline
ltgt _at_words split(/\s/, inputline)
37
Reading from and Writing to Files
  • To access a file on a UNIX file system from
    within your Perl program, you must perform the
    following steps
  • Your program must open the file. This tells the
    system that your Perl program wants to access the
    file.
  • The program can either read from or write to the
    file, depending on how you have opened the file.
  • The program can close the file. This tells the
    system that your program no longer needs access
    to the file.

38
Looping Constructs
./loops x is now 1 x is now 2 x is now 3 x
is now 4 x is now 5 and x is now 6 and x is
now 5 and x is now 4 and x is now 3 and x is
now 2 and x is now 1
vi loops !/usr/bin/perl x 1 while (x lt
5) print ("\x is now x\n") x
until (x lt 0) print ("and \x is now
x\n") --x
39
Arrays
  • To assign a value (or list of values) to an
    array, simply group them together.

_at_weekdays (Mon, Tue, Wed, Thu, Fri)
  • Arrays are zero-based like C. You could also
    have accomplished the above by

_at_weekdays0 Mon _at_weekdays1
Tue _at_weekdays2 Wed _at_weekdays3
Thu _at_weekdays4 Fri
  • Another example

_at_home0 couch _at_home1 chair _at_home2
table _at_home3 stove
40
Hashes
  • An unordered set of scalars, accessed by some
    string value associated with each scalar

longday (Mon, Monday, Tue, Tuesday,
Wed, Wednesday, Thu, Thursday,
Fri, Friday)
  • Somewhat confusing to look at so Perl provides
    another way to initialize

longday (Mon gt Monday, Tue gt
Tuesday, Wed gt Wednesday,
Thu gt Thursday, Fri gt
Friday)
41
Arrays vs Hashes
42
Lets do a little Perl Review
  • Write a Perl program that will read in a set of
    scores from a file and calculate and print the
    average score.
  • The file is of the format

Fred 23 Mary 24 Terry 25
43
The code
  • !/usr/bin/perl
  • open(GRADES, "grades") or die "Can't open
    grades\n"
  • total 0
  • scores 0
  • while (line ltGRADESgt)
  • (student, grade) split(" ", line)
  • total grade
  • scores
  • close(GRADES)
  • average total / scores
  • print "Average average\n"

44
STDIN and STDOUT
  • !/usr/bin/perl
  • print STDOUT Enter a number
  • number ltSTDINgt
  • print STDOUT The number is number.\n

./printnum Enter a number 4 The number is
4 .
45
Angle Operator ltgt
  • Encloses the file handle that you want to read
    lines from.
  • The empty angle operator, ltgt, will read lines
    from all the files specified on the command line.
  • If no files were specified on the command line
    then ltgt reads from STDIN.

46
Chop
  • Remember the extra line at the end of our output
    for the previous example. This happens because
    the line-reading operation does not automatically
    remove the newline from the input line.
  • The chop command will indiscriminately remove
    (and return) the last character of the string.

47
chop example
  • !/usr/bin/perl
  • print STDOUT Enter a number
  • number ltSTDINgt
  • chop(number)
  • print STDOUT The number is number.\n

./printnum Enter a number 4 The number is 4.
48
Executing UNIX commands from Perl
  • The backtick( ) character is the command input
    operator. e.g.
  • info finger root
  • Another way is to use system
  • system(finger root)
  • These two are not exactly the same as the first
    example places the result in variable info.

49
pop and push
  • push (array, value-list) adds element(s) to the
    end of the array.
  • pop (array) removes the last element from the end
    of an array.

!/usr/bin/perl _at_stack (top, middle,
bottom) var pop(_at_stack) print var
var\n push(_at_stack, next) print stack
_at_stack\n)
./pop_push_example var bottom stack top
middle next
50
Some String Operations
  • substr(str, start-pos, length) returns a
    substring of the specified string, beginning at
    the start position for the length specified.
  • substr(str, start-pos, length) string replace
    the specified section of the string with the
    assigned string.
  • length(str) find the length of a string
  • index(str, pattern) find the position of a
    pattern in a string

51
String operations example
  • !/usr/bin/perl
  • var this is a long string
  • sub substr(var, 0, 4)
  • print subsub\n
  • substr(var, 10, 4) short
  • print varvar\n
  • lngth length(var)
  • print lengthlngth\n
  • loc index(var, short)
  • print locationloc\n

./str_operations_example subthis varthis is a
short string length22 location10
Write a Comment
User Comments (0)
About PowerShow.com