Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write.

Similar presentations


Presentation on theme: "COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write."— Presentation transcript:

1 COMP075 OS2 Bash Scripting

2 Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write shell scripts But BASH scripting features make scripting in bash more like programming in a traditional language

3 Uses Most of the linux SysV INIT run time environment is established through BASH scripts –Service start/stop scripts for example Simple scripts can be written to run a command with preset parameters –du -x -h --max-depth=${1:-1} Fairly simple scripts could, for example, send a custom email to a file of addresses More complex scripts can perform perl like functions

4 Language Features A useful programming language has to have: –Variables to contain data –Literals Character Numeric Boolean –An if statement –A looping construct –Subroutines –A useful set of basic operations

5 BASH Variables Often called environment variables Many built in variables like $PATH are useful at the command line Some of the built in variables are particularly useful in scripts –$LINENO contains the line number of the script line being executed –Useful for debugging Scripts (or users) can create their own variables

6 Variable Names Best to stick to Perl rules –Letters numbers and underscore –Start with letter Note: variables names do not start with $ $ is an operator that substitutes the value of the variable into the command in place of the name X=1 echo $X displays the value 1 echo X displays the name X

7 Variable Types All BASH variables are character variables But if all of the contents are digits you can perform numeric operations If any of the characters are not digits BASH treats it as 0 (zero) in numeric operations

8 Variable Scope Every process has an Environment (a memory area) containing environment variables, and these variables are also available to the process's child processes Variables created in a script are in the script's memory space (not the process's environment) The export command exports the variable to the script's environment making it available to sub-processes created by the script Variables created in a code block or function may be local to the code block or function –Like perl private variables (my $variables)

9 Creating Variables name=value –Creates variable called “name” –Assigns the value “value” let a=$b+1 –Creates “a” –Evaluates numerical expression and assigns to “a” export a=value –Creates and exports “a”

10 Using Variables Usually we want the value of the variable inserted in the command in place of the name a=“Hello World” echo $a –Same as echo “Hello World” $a is a short form of ${a} Variables occasionally appear inside braces, with the $ prefix before the opening brace

11 Parameters Parameters passed to a script on the command line appear as variables inside the script $1 provides the value of the first parameter $2 … $9 provide the next 8 parameters $0 contains the name of the script ${11} is the eleventh parameter $# is the number of parameters $? = return value of last command $$ = PID of the script

12 Literals An unquoted string with no white space is a string literal h=hello w=world echo $h “ “ $w Strings containing white space must be quoted phrase=”hello world” echo $phrase

13 Quotes Variables within double quotes are interpolated –As in perl Variables within single quotes are not Strings in backticks are evaluated as a bash command and the output returned dir=`pwd` echo $dir –Prints current directory

14 Numeric Literals Numeric literals in a numeric expression are treated as numbers a=$((20 + 5)) echo $a –Displays 25 declare -i a –Makes a an integer –Values assigned to it are treated as numbers –Non-numeric strings are evaluated as 0

15 Boolean Values There is no special boolean data type In if statements and elsewhere 0 is treated as true if [ $? ] ; then echo 'last command successful' ; fi Note –“[“ is a command meaning “test” –Could have written if test $? –“;” is a statement separator –So is newline

16 If Statement General syntax: if command1 then command list else command list fi In this form –Newline terminates command1 –else and fi terminate the command lists

17 Another Form of if if command ; then command ; fi ';' teminates commands instead of newline Useful to run an if statement on the command line

18 If condition In the general format the condition of the if statement is a command If the return from the command is 0 then the condition is true “test” statement, or its synonym [ can be used to evaluate a boolean expression if [ $? ]; then echo “last command worked”; fi Note: whitespace is required after [

19 Some Boolean Constructs if [ $a = $b ] –Also == != \ For strings –Or -eq -ne -lt -gt -le -ge For numeric comparison if (( $a > $b )) –Or >= < <= For numeric comparison Note: (( 0 )) is false, (( 1 )) is true

20 File Tests if [ -e filename ] –File exists Also -d –File is a directory -s –Not size zero -r (or w or x) –Has read permission (or write or execute)

21 Some More Tests if [ -z “$a” ] –True if $a is null If [ -n “$a” ] –True if $a is not null Why the “ around $a ? –$a inserts the value of the variable a into the command –If $a is null then nothing is inserted and the command without quotes becomes: if [ -z ] –Rather than if [ -z “” ]

22 Boolean Operators Between tests: ! = not && = and (also -a) || = or (also -o) If [ ! -e file1 ] && [ -e file2] Can also be written If [ ! -e file1 -a -e file2]

23 [ and [[ [ is a built in command –Runs as part of the BASH executable [[ is an external command –Runs as a separate process [[ is more sophisticated (larger program) Sometimes behaves differently For example –if [[ -e file1 && -e file2 ]] –Is OK

24 Looping with for for argument in list do commands done Argument is a variable name Takes on value of each item in the list and then commands are executed List can be literals, variables, or an operator that generates a list for file in * ; do echo $file; done

25 Another for loop for (( expr1 ; expr2 ; expr3 )) do commands done Numeric expr1 is evaluated Expr2 is evaluated and if non-zero commands are executed Then expr3 is evaluated Like the equivalent perl loop

26 Looping with While and Until while [ condition ] do commands done [ is optional Can use [[ for fancier test Often used to read through a file

27 Reading a File with While read x –takes the next value from standard input assigns it to the variable x and returns 0 (true) as long as there is a value cat filename | script.sh –Pipes the records from filename into standard input of script.sh The following script would just print the records cat filename | ( while read record; do echo $record done )

28 User Input In the previous example, the contents of a file was piped to standard input of a sub-process Usually standard input is the keyboard, so read reads input from the user echo -n “Enter your selection: “ read choice echo “You entered $choice”

29 Loop Control break –Breaks out of a loop –Like the “last” statement in perl continue –Jumps to the next iteration of the loop –Like the “next” statement in perl Useful in forever loop while [ 0 ]

30 Case Statement case expression in pattern-list ) command ;; pattern-list) … ;; esac

31 Case (cont.) Expression can be any expression, usually a variable Value of the expression is matched to the patterns If it matches the commands are run “)” terminates the pattern list –Patterns separated by | “;;” terminates the command list esac terminates the entire structure and last command list

32 Patterns Are the same patterns used as wild cards in file names and elsewhere E matches the character E [Ee] matches upper or lower e [0-9] matches a range [digit] matches a digit * matches any string ? matches any single character

33 Parameter Expansion ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion Expansion means inserting the value of an expression into the command line ${VarName} inserts the value of VarName into the command line Can be written $VarName $(Command) inserts the output of Command into the command line Same as `Command`

34 Special Expansions ${var:-expression} –If var unset or null, return expression, else var ${var:=expression} –Also sets var = expression ${var:offset:length} returns substring ${#var} returns length of var Lots more

35 Comments The # character indicates that all following text is treated as a comment Must be the first character of a word –Basically means that it follows whitespace ls * # lists filenames in current directory –# starts a comment echo ${#Var} –Echos length of Var –# doesn't start a comment

36 Command Groups ( command list ) and { command list; } both treat the command list as a single command for the sake of redirection ( ) uses a separate subshell so variables in the group are local Example: ( command1; command2; command3 ) > filename – Sends output of all three commands to filename

37 Shell Functions Like perl functions, shell functions allow you to break a large program down into smaller modules When invoked, function runs in the same environment as the main program –Variables are global unless declared local Returns exit status of last command executed, or an integer specified in a return statement –Accessed through $? Must be defined before being called Can be passed parameters –$1 $2 etc

38 Shell Function Syntax function func_name { commands } Or func_name () { commands }

39 Invoking the Function Shell functions are invoked just like any other BASH command my_function string1 string2 if [ $? ] ; then –Function was successful string1 becomes the value of $1 inside function Can use $# etc inside function

40 Getting Help The man command prints a manual page for a command and is the recommended way of getting help Works only for external commands –Commands implemented via a separate executable file Built in commands are part of the bash executable and don't always have separate man pages man BASH has help for built ins but is tedious

41 Other Sources of Help The info command produces structured help that functions a little like HTML –OK if you learn to use it –Has been in unix since before HTML It is useful to find an online source of man pages that is authoritative A list is available here: http://www.tldp.org/manpages/man.html http://www.tldp.org/manpages/man.html Google results can be good sometimes, not always. Find a source you trust and bookmark it.

42 Running a BASH Script Save your commands in a text file –Name can be anything, but avoid using the name of an existing shell command –Extension can be anything but.sh is conventional –Some editors syntax highlight based on that extension First line should be: #!/bin/sh File needs to be executable –chmod +x file.sh Put in path, or provide fully qualified name./file.sh to run from current directory

43 References http://www.tldp.org/manpages/man.html –On-line man pages http://www.gnu.org/software/bash/manual/bashr ef.html#Bash-Builtinshttp://www.gnu.org/software/bash/manual/bashr ef.html#Bash-Builtins –Authoritative reference for BASH http://www.tldp.org/guides.html –Various guides, HTML on-line or dl, PDF –Advanced Bash-Scripting Guide is my favorite Appendix B is really useful –Bash Guide for Beginners seems good


Download ppt "COMP075 OS2 Bash Scripting. Scripting? BASH provides access to OS functions, like any OS shell Also like any OS shell, BASH includes the ability to write."

Similar presentations


Ads by Google