Shell scripts - PowerPoint PPT Presentation

1 / 63
About This Presentation
Title:

Shell scripts

Description:

Chapter 14 Shell scripts Putting it all together The Concept of Scripts A script is nothing more than an executable file of shell commands If you can do it on the ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 64
Provided by: Dr1475
Category:

less

Transcript and Presenter's Notes

Title: Shell scripts


1
Chapter 14
  • Shell scripts
  • Putting it all together

2
The Concept of Scripts
  • A script is nothing more than an executable file
    of shell commands
  • If you can do it on the command line, you can do
    it in a script.
  • Best working model
  • Work in the C shell as your interface
  • Have your scripts run in the Bourne shell

3
Examination of Shells for Scripting
  • You can write scripts in any shell
  • The Bourne shell is best for scripting because
  • It is the standard shell for system
    administration
  • All flavors of Unix have a Bourne shell or the
    equivalent
  • The Bourne shell was the first shell

4
Command line execution vs. execution in a script
  • You dont need to write scripts.
  • You can keep reinventing the wheel over and over
    and over on the command line.
  • However, if you want to be efficient you will
    discover that if you have to do the same thing
    more than once, building a script to do it will
    save you lots of time.

5
Scripts are tools
  • This is a critical concept.
  • When you build a script, you craft a tool to
    perform a specific task
  • Once you have the tool, you can use it to easily
    perform the same task over and over.

6
Forcing Execution in a Specific Shell
  • The shebang character ! as the first character
    of a file, directs the shell to fork a process
    and run the command that follows the shebang
  • The rest of the file is used as input to the
    program run from the first line.
  • It is possible to use this construct to run other
    programs besides the shell.

7
Scripting standards
  • You should always write your scripts to a
    standard.
  • If your instructor has a standard use that
  • If not, use the standard in the book

8
Shell variables as they apply to scripting
  • The shell has a collection of variables available
    for your use
  • You can also create your own variables
  • If you want to use the contents of a shell
    variable, you must precede the variable name with
    a
  • In the Bourne shell, you may not put white space
    around the assignment operator

9
Shell variables an example
  • You simply assign a value to a variable with the
    sign
  • your variables should be lower case
  • unless you export them, shell variables are only
    known the the shell that created them

10
readonly
  • You can set a shell variable to be readonly
  • Once a variable has been marked as readonly, it
    cannot be changed
  • There is no un-readonly command

11
Useful shell variables
  • count of command line arguments
  • ? Return status of the most recently executed
    command
  • The process ID
  • ! Process ID of the last background process
  • All command line args in one long string
  • Each command line arg in its own string
  • 0 leftmost part of the command line, the command
    or script
  • n (1-9) the command line args. There are only 9
    addressable Bourne shell command line arguments

12
set
  • You can use set to change the values of the
    command line arguments
  • It changes , , _at_, as well as 1 -n
  • It is not used often but it is useful for testing

13
env
  • displays the environmental variables
  • The env command can change the value of the
    Bourne shell environmental variables

14
export
  • The export command will make the values of user
    variables available to shells forked from the
    current shell
  • It is the way you can pass variables from a
    parent process to a child process

15
Commands Used in Scripts
  • The commands that follow are use to create the
    skeleton upon which other commands are hung
  • You will use all of the commands you have learned
    in the text at one time or another in scripts.
  • Shell scripting is considered by some to be the
    best possible programming environment

16
echo
  • Used as the standard output command
  • Usually forces a newline at the end of the text
  • The -n option suppresses newlines in csh
  • The \c output specifier suppresses newlines in
    the Bourne shell
  • The \n and \t and others are also available for
    formatting

17
echo an example
18
sh
  • You can invoke the shell outside the script and
    use it as a debugging tool
  • -v (verbose) shows all of the lines of the script
  • -x (executed) prepends a to the front of every
    line that is executed
  • You can also imbed those options in the shebang
    line

19
sh an example
20
Simple if
  • Allows conditional execution
  • The required format is if test
    condition then command1
    command2 fi

21
Simple if example
22
Simple if alternate form
  • Allows conditional execution
  • The required format is if condition
    then command1
    command2 fi
  • This form is NOT preferred since it can be
    confusing. Please avoid this form

23
if then else
  • This form of the if will always take some action.
  • If the condition is true the code after the then
    is executed, (called the if or then block)
  • If the condition is false the code after the else
    is executed, (called the else block)

24
if then else syntax
  • The syntax of the if/then/else is if test
    condition then commands if condition
    is true else commands if condition is
    false fi

25
if then else example
26
Nested if
  • Adding one more verb allows us to create very
    complex and confusing constructs called nested
    ifs
  • Only one of the instances can be true
  • Be very careful when constructing nested ifs,
    consider a case structure instead

27
Nested if syntax
  • The syntax of the nested if if test
    condition then done when condition
    1 is true elif test condition 2
    then done when condition 2 is true
    else done if neither condition true fi

28
Nested if example
29
test
  • The test command can evaluate 3 types of
    conditions
  • file conditions
  • string comparisons and conditions
  • numeric comparisons
  • The test command is worth focused study, the more
    you know about test the better your scripting
    will be

30
testing file conditions
  • The format is if test -cond file
  • Some examples of -cond
  • -d (the file is a directory)
  • -f (the file is a regular file)
  • -r (the file is readable)
  • -s (the file has a size greater than 0)
  • -w (the file may be written to)
  • -x (the file is executable)

31
testing string comparisons
  • The following are the string comparisons used
    with the test comand
  • -n string (true if string has contents)
  • -z string (true if the string has no contents)
  • s1 s2 (true if string 1 equals string2)
  • s1 ! s2 (true if string 1 doesnt equal string
    2)
  • string (true if string is not empty)

32
Testing numeric relationships
  • The numeric comparisons are what you would
    expect
  • n1 -eq n2 (the two numbers are equal)
  • n1 -le n2 (first number is less than or equal
    second)
  • n1 -lt n2 (first number is less than the second)
  • n1 -ge n2 (first number greater or equal the
    second)
  • n1 -gt n2 (first number is greater than the
    second)
  • n1 -ne n2 (the two numbers are not equal)

33
A common logic error
  • Many logic errors are caused when scriptors
    forget that there are three possible
    relationships between any two numbers
  • the first is greater than the second
  • the first is less than the second
  • the two numbers are equal
  • It is this last situation that many coders forget.

34
An example of a common error
35
case
  • The case structure adds structure to Bourne shell
    scripting
  • Use the case anytime you have more than 2 nested
    ifs
  • Do not use case to replace a series of ifs, only
    to replace nested ifs
  • Only one of the patterns in a case will match

36
Nested if vs. case
37
Complex case condition example
38
read
  • Besides taking input from the command line via
    the command line arguments, you can prompt the
    user and read data from the keyboard
  • The shell parses the input stream on the normal
    field separator
  • Each variable is given one string, with the last
    variable taking everything that is left

39
read example
40
exit
  • As you already know, the exit command will exit
    from the current shell
  • You can use the exit command to set a return code
    or return status
  • Good structure requires only one exit from any
    script

41
A sample exit script
42
A sleepy script
43
command substitution
  • Command substitution allows you to store the
    output of a command or pipe in a variable.
  • It is very useful for many applications
  • You can create huge values if you are not careful
    about what you store
  • Use the grave marks not apostrophes around
    the commands

44
command substitution example
45
expr
  • In the Bourne shell, all math is integer math
  • The tool you use to do math is expr
  • You must put whitespace between each part of the
    command
  • If you use variables with expr, those variables
    must have a value

46
expr examples
47
Other uses for expr
  • Expr can also perform
  • relational operations
  • , !, gt, lt, gt, and lt
  • logical operations
  • whether one of the arguments is empty
  • whether the arguments contain the same data
  • if the second argument contains the first
    argument
  • While expr can perform these tests, this type of
    usage is very uncommon

48
for
  • The for loop is a counted loop with the following
    format for var in list do
    code you want iterated done
  • The variable var will take on each of the values
    in the list

49
A simple sample for
50
A common use for for
51
A special form of for
52
A special form of for
  • Using the for variable form of for,
    you can access more than 9 command line
    arguments!
  • This is one of two tricks to get around the 9
    addressable arguments situation.

53
continue
  • The continue command stops processing the current
    value of the loop variable, and restarts the loop
    with the next value.
  • This is a way to skip processing a particular
    record

54
continue example
55
break
  • The break command exits from the loop, passing
    control to the command following the end of the
    loop
  • If there is no command following the loop, break
    causes the script to exit
  • If you want to exit from the script, use the exit
    command

56
break example
57
while
  • The while loop is the conditional loop of choice
    since it evaluates the condition before executing
  • The while loop has the following syntax while
    condition do what to iterate
    done

58
while example
59
More interesting while example
60
shift
  • The preceding while example showed you the second
    way to access by name more than 9 command line
    arguments
  • The shift command performs a left shift logical
    on the command line arguments, moving the value
    of 1 out, the value of 2 into 1, the value of
    3 into 2, and so on.
  • Notice that the previous script always addresses
    1, and that value changes

61
An elegant way to pass data
  • If you need to pass the value of a variable to a
    script, you can use it on the command line, or
    you can send the data down a pipe with the echo
    command.
  • Often the echo method is more efficient

62
A data passing example
63
Congratulations!
  • You have survived the entire course.
  • Go out and script
  • and most of all
  • Have Fun
Write a Comment
User Comments (0)
About PowerShow.com