Operating Systems Lecture 3 Shell Scripts - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Operating Systems Lecture 3 Shell Scripts

Description:

Shell Scripts. 2. Brief review of unix1.txt ... 3. All shell scripts begin with the interpreter you want to use: #!/bin/bash. Example: ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 26
Provided by: marily258
Category:

less

Transcript and Presenter's Notes

Title: Operating Systems Lecture 3 Shell Scripts


1
Operating SystemsLecture 3Shell Scripts
2
Brief review of unix1.txt
  • Glob Construct (metacharacters) and other special
    characters
  • ?, ,
  • Ex. ls prog?.c ls p.c ls ???.
  • ls prog123.c ls m-s ls 1-5A-Z
  • ls !ABC !a-z0-47
  • Other characters user -

3
More unix commands
  • grep (egrep)
  • find
  • head
  • tail
  • cut
  • paste
  • sed
  • tr
  • Tee
  • Jobs
  • fg 1
  • !! !-n !cmd str1str2

4
Shell scripts
  • Typing on command line
  • One time task
  • Shell scripts
  • Executable ASCII file containing shell command
  • Can run like a program
  • Deal with complex task
  • Repeat it later

5
Shell script contd
  • A shell script, in its most basic form, is simply
    a collection of operating system commands put
    into a text file in the order they are needed for
    execution.
  • !/bin/bash
  • rm -f /tmp/listing.tmp gt /dev/null 2gt1
  • touch /tmp/listing.tmp
  • ls -l a-z.doc sort gt /tmp/listing.tmp
  • lpr -Ppostscript_1 /tmp/listing.tmp
  • rm -f /tmp/listing.tmp

6
Shell Programming
  • Shell scripts must be marked as executable
  • chmod ax myScript
  • 2. Use to start a comment.
  • Comments run from to the end of the line.
  • 3. All shell scripts begin with the interpreter
    you want to use
  • !/bin/bash
  • Example
  • !/bin/bash
  • who grep ycjiang
  • exit 0

7
Running a shell script
  • To run a UNIX script
  • Type the script into a file.
  • Change the file permission to executable.
  • Execute it (by typing the filename at the
    prompt).

8
Shell Variables
Shell variables are stored as strings Example
!/bin/bash x1 Note No spaces in
assignment. If space after x, thinks x is a
command echo The value of x is x x prints
the value of variable x echo The home directory
is HOME echo The current shell is SHELL (Note
to debug, use -x sh -x scriptFileName This
will list the commands as they are executed.)
9
Using Quotes
Single quote Groups together characters until
end quote. is not processed. Example
!/bin/sh grep Constance John
/etc/passwd Tries to open John as a file
grep 'Constance John' /etc/passwd Searches
for Constance John in passwd file x1
echo x echos 1 echo 'x' echos x
exit 0
10
Double Quotes
Double quotes act like single quotes, except the
is processed !/bin/bash x1 echo x echos
the value of x echo "x" echos the value of
x address"College of the Engineering and
Science" echo address echos College of the
Engineering and Science echo "address" ditto exi
t 0
11
More Quotes
Backslash (\) Places a single quote around a
character \gt is the same as 'gt' Back quote
() Tells shell to execute the enclosed command
and insert the output here !/bin/bash echo
There are who wc -l users logged on exit
0 Try these examples out for yourself!
12
Evaluating Expressions
Use expr to evaluate arithmetic
expressions Example !/bin/bash i1 iexpr
i 1 use back quote echo i
prints out 2 exit 0
13
Common Mistakes
Mistake 1 Leaving out expr !/bin/bash i1 i
i1 echo i prints 11 exit
0 Mistake 2 Leaving out spaces around
!/bin/bash i1 iexpr i1 echo i
prints out 11 exit 0
14
Using Arguments with a Shell script
Example command gtmyShell arg1 arg2 arg3 arg1
is accessed by 1, arg2 is accessed by 2,
etc. Question What is accessed by 0? The
number of arguments (not including the command)
is given by for the above command line is
3.
15
example
  • !/bin/bash
  • source prnargs
  • shell pgm to print out its first 2 args
  • echo The first argument is 1
  • echo The second argument is 2
  • exit 0

16
To run
  • First, make sure it is executable (has execute
    permissions for user),
  • /home/ycjianggt prnargs "Hello there" world
  • The first argument is Hello there
  • The second argument is world
  • Note spacing is correct, it ignores fact that I
    lined up the 2 args in the echo
  • --could have used quotes in echo,
  • echo "The first argument is " 1
  • echo "The second argument is" 2
  • --or could have used echo backslash
  • -escape TAB char, \t (see man echo)
  • echo -e "The first argument is\t" 1
  • echo -e "The second argument is\t" 2

17
The shift command
The shift command shifts each variable down
1. 2 becomes 1, etc. 0 is not shifted. It
always refers to the command name. Example
command line gtcmd x y z Example
script !/bin/sh echo 0 1 2 prints 3
cmd x y shift echo 0 1 2 prints 2 cmd
y z shift echo 0 1 2 prints 1 cmd
z exit 0
18
The read command
Use the read command to read in from the
keyboard Example !/bin/bash read x y z echo
"x " x echo "y " y echo "z " z read
text echo "text " text exit 0 If type "I
love shell programming!" at first read x is
"I", y is "love" and z is "shell
programming!" If type "I love shell
programming!" at second read text is "I love
shell programming" Use lt to read in from a
file read x y z lt myFile
19
Conditionals
Example of a conditional statement !/bin/bash
read name if "name" Joe then echo It
is Joe else echo It is "name", not Joe fi
fi marks the end of the statement exit 0
20
Integer comparisons
-eq (or )is equal to -ge greater than or equal
to -gt greater than -le less than or equal
to -lt less than -ne not equal to Example if
num1 -lt num2 then ... fi
21
for loops
Example !/bin/bash for i in 1 2 3 4
5 do for j in .1 .2 .3 .4 .5 do echo
ij done done exit 0 Note break command
exits loop continue loops back to top, skipping
the rest of the current iteration.
22
while loops
Example !/bin/bash i1 num5 while i -le
num do echo i gtgt file_test iexpr i
1 done exit 0 What does this do? Note while
read x loops until zero exit status not returned
from read.
23
Defining Functions
Example !/bin/bash nu() who wc
-l echo Number of users is nu echo Number
of users is now nu exit 0
24
Writing Good Shell Scripts
  • Verify that the correct number of arguments is
    used
  • !/bin/bash
  • if -ne 3
  • then
  • echo "only parameters entered."
  • echo "Usage 0 arg1 arg2 arg3"
  • exit 127
  • fi
  • exit 0
  • Use good design and style (e.g. use functions and
    comments.
  • Return an exit status
  • exit 0 things worked OK
  • exit 127 non-zero error occurred.
  • See /usr/include/sysexits.h for sample exit codes.

25
Tutorials on shell scripting
  • http//www.injunea.demon.co.uk/
Write a Comment
User Comments (0)
About PowerShow.com