Sunteți pe pagina 1din 17

Unit 5 Scripting

Copyright 2010, ITT ESI

Scripts are used heavily in linux environments


Scripting is a type of programming language Allow you to automate many tasks Scripts are interpreted in the fly
There is no need to compile the code

Scripts can change their behavior under different conditions


By By By By day Time what files exist User Input
Copyright 2010, ITT ESI 2

A basic script file will be read by the shell line by line Files are read from the top down
A script like this will likely be a list of commands, one after another When executed, the shell acts just like it would if you typed these commands in one at a time
Copyright 2010, ITT ESI 3

Many times user input is required to complete a task


Name of a file Select options

Variables are needed to store information


Variables in Scripts act a little different than in other languages
There is no need to formally declare a variable There is no need to specify a variable type To use, type a $ followed by the variable name Example: $inFileName
Copyright 2010, ITT ESI 4

To simply set a variable to a value:


variableName=value for variable

Use the read command


read variableName
This command reads input from the user and places the value into the variable
Notice while using this command, there is no need to use a $ in front of the variable name variableName can be any name, even one not yet declared in the script

Copyright 2010, ITT ESI

Control commands are those that affect the normal top to bottom flow of a script There are many types of control commands
If statements Loops
For While Until Case

Copyright 2010, ITT ESI

If statements are the basic decision makers of scripts


They evaluate an expression:
if it is true, the commands in the then section are executed If it is false, the commands are skipped

Syntax:

If [ statement ] then

fi

commands

Copyright 2010, ITT ESI

If statements can contain optional else statements

The else code is only run in the case that the original expression is false

Syntax:

if [ statement ] then else

commands commands

fi
Copyright 2010, ITT ESI 8

Instead of using else statements that are followed by another if you can use a single command
elif

Syntax:

if [ statement ] elif [ statement2 ]

Copyright 2010, ITT ESI

For loops do a set of commands a set number of times Each time the loop is run, the value of the starting variable is changed
This means we can get different results each time we go through the loop

Copyright 2010, ITT ESI

10

Syntax:

for loopVar in value1 value2 value3 do

done

commands

Note:
The values can be a list of words or numbers
Each iteration through the loop, the next value will be placed into loopVar

When the loop runs out of values, the script continues beyond done
Copyright 2010, ITT ESI 11

While loops will do a set of commands continuously until its test case turns false Syntax:

while [ expression ] do

done

commands

Copyright 2010, ITT ESI

12

Number Comparison Expressions:


-gt -ge -lt -le -eq Greater Than Greater Than or Equal To Less Than Less Than or Equal To Equal To

Copyright 2010, ITT ESI

13

Much like while loops, except:


Until loops do something until their test expression turns true Wash the car until it is clean

Syntax

until [ expression ] do done

commands

Copyright 2010, ITT ESI

14

Case Structures allow your script to choose one set of commands to run from an entire list Syntax
case selectionVar in case1)

case2)

commands commands

esac

case3) commands

Copyright 2010, ITT ESI

15

When the value in selectionVar exactly matches the value of a case, the commands directly below the case are executed
When the end of that case is reached, the script jumps to esac (case spelled backwards)
If no case is matched, then the script continues on without running any of the cases

Copyright 2010, ITT ESI

16

The cases can use special characters to catch more possibilities for one case
* - matches any string of characters | - used between two strings to allow the case to run when EITHER string is matched ? matches any SINGLE character

Example:
cat|dog) echo This matches the text cat or dog
Copyright 2010, ITT ESI 17

S-ar putea să vă placă și