Presentation is loading. Please wait.

Presentation is loading. Please wait.

Day 8,9,10 COP 3502 Introduction to Shell Scripts David A. Gaitros Department of Computer Science Florida State University.

Similar presentations


Presentation on theme: "Day 8,9,10 COP 3502 Introduction to Shell Scripts David A. Gaitros Department of Computer Science Florida State University."— Presentation transcript:

1 Day 8,9,10 COP 3502 Introduction to Shell Scripts David A. Gaitros Department of Computer Science Florida State University

2 What is a shell script? A group of UNIX commands along with a few programming constructs placed in a file that is executable. Must like a program.A group of UNIX commands along with a few programming constructs placed in a file that is executable. Must like a program. Used when a function needs to be completed over and over again.Used when a function needs to be completed over and over again. Your startup scripts are shell scripts.Your startup scripts are shell scripts.

3 Simple Shell Scripts The following text is placed in a file called simpleThe following text is placed in a file called simple # A Comment starts with an # caldatewho

4 How to run a shell script There are two waysThere are two ways –1. Use the (sh) command sh simple sh simple –2. Change the permissions to executable and just enter the file name chmod 755 simple chmod 755 simple chmod u+rwx,a+x,g+e simple chmod u+rwx,a+x,g+e simple simple simple

5 The Shell as a programming language variablesvariables –environment –user created –positional parameters input/output functionsinput/output functions arithmetic operationsarithmetic operations conditional expressionsconditional expressions selection structuresselection structures repetition structuresrepetition structures

6 Variables Environment - Special shell variables, keyword variables, predefined variables, or standard shell variables.Environment - Special shell variables, keyword variables, predefined variables, or standard shell variables. –Examples include TERM, HOME, MAIL, PATH, etc. –They exist and you can change them but you do not initially create them.

7 Unix Shell Stuff Standard Input and OutputStandard Input and Output –Standard input - stdin –Standard output - stdout –Standard error - stderr RedirectionRedirection –ls -al > directory.out –ls -al > directory.out –ls -al >> directory.out # Appends –mail gaitrosd –mail gaitrosd

8 Unix Shell Stuff Pipes - The output of one command is input to another.Pipes - The output of one command is input to another. ls -al | more ls -al | more # The output of the ls command is given to the more command with the pipe (|) symbol. You can also group commands on one line by separating them with a semicolon (;)You can also group commands on one line by separating them with a semicolon (;)

9 Unix Shell Stuff teetee A tee allows you to save the output of one command while also making it input to another:A tee allows you to save the output of one command while also making it input to another: ls -al | tee output.dat | more ls -al | tee output.dat | more

10 Unix Shell Stuff Wildcards - A single character that stands for a group of characters.Wildcards - A single character that stands for a group of characters. Let us say that you wanted to list all of the files that ended with.outLet us say that you wanted to list all of the files that ended with.out ls -al *.out ls -al *.out You can put wildcards anywhereYou can put wildcards anywhere ls -al *fall*.out ls -al *fall*.out This gives us all the files that have the word fall embedded in it that end in.out. The ? is also a wildcard but it only does one character at a time. For instance:The ? is also a wildcard but it only does one character at a time. For instance: ls -al ?ello.out ls -al ?ello.out This lists all the files starting with any character and ending in ello.out. This lists all the files starting with any character and ending in ello.out.

11 Unix Shell Stuff Special Characters that have special meaning:Special Characters that have special meaning: & * \ | [ ] { } $ ( ) # ? ' " / ; ^ ! ~ % `& * \ | [ ] { } $ ( ) # ? ' " / ; ^ ! ~ % ` To print these characters you must precede them each time with a backslash (\) usually found below the pipe(|) symbol on the keyboard.To print these characters you must precede them each time with a backslash (\) usually found below the pipe(|) symbol on the keyboard. echo "what time is it\?" echo "what time is it\?" It only works on a single character and you must put one in front of every special character you want to print.It only works on a single character and you must put one in front of every special character you want to print. Backquotes ( also called grave accent marks). The backquotes run a unix command inside the shell.Backquotes ( also called grave accent marks). The backquotes run a unix command inside the shell.

12 Unix Shell Stuff QuoteEffiect \ Cancels the special meaning of a single character inside a string 'string' Cancels the special meaning of any special characters inside the string "string" Cancels the special meaning of any special characters inside the string except $, ` `, and \ `string` Run any command in the string. Output replaces `string`. Note these ` are backquotes.

13 Variables User Created VariablesUser Created Variables –Creating user variables is easy. You just specify a name not already in use and assign a value to it. Use the set command. set stuff = ~gaitrosd/public_html set stuff = ~gaitrosd/public_html –To use the value inside the variable you place a $ in front of it. ls -al $stuff ls -al $stuff NOTE: typically, user defined variables are in lower case to distinguish them from standard shell variables.

14 Positional Parameters Positional parameters are also called read-only. They are numbered 1,2,3,….9. and correspond to the parameters entered as part of the shell command.Positional parameters are also called read-only. They are numbered 1,2,3,….9. and correspond to the parameters entered as part of the shell command. $0 is always the shell script command.$0 is always the shell script command. Each parameter is identified by a space separation at the UNIX prompt.Each parameter is identified by a space separation at the UNIX prompt. The $# is defined in the shell programming structure as containing the number of parameters. It starts counting with the actual first parameter.The $# is defined in the shell programming structure as containing the number of parameters. It starts counting with the actual first parameter. The $* is the string that contains all the arguments.The $* is the string that contains all the arguments.

15 Positional Parameters Here is an example program called echo.argsHere is an example program called echo.args # Comment Line echo "Welcome to the echo shell program" echo "This is an example of positional parameters" echo "This program can handle up to nine (9) parameters" echo "You have typed $# arguments" Demonstration of shell script

16 Input/Output Functions As you have seen, the echo command allows you to output items to standard output.As you have seen, the echo command allows you to output items to standard output. The read command allows you to input simple items into the shell script.The read command allows you to input simple items into the shell script.#!/bin/sh #Illustrates the use of positional parameters, user # defined variables # and the read command echo 'What is your name' read name echo "Well, $name, you typed in $# arguments" echo "They are: $*" Demonstration:Demonstration:

17 Set Command Assume we have the program as follows in a file called setdate. The set command executes the date command and automatically assigns the output to the positional parameters.Assume we have the program as follows in a file called setdate. The set command executes the date command and automatically assigns the output to the positional parameters. set `date` echo "Time: $4 $5" echo "Day: $1" echo "Date: $3 $2 $6" Demonstration:

18 Arithmetic Operations The shell is not intended to do any extensive mathematics. You should only perform simple integer arithmetic. (*) (+) (-) (/) (%)The shell is not intended to do any extensive mathematics. You should only perform simple integer arithmetic. (*) (+) (-) (/) (%) Use the expr UNIX utility.Use the expr UNIX utility. example:example: sum = ` expr $1 + $2` sum = ` expr $1 + $2` echo "$1 + $2 is equal to $sum" echo "$1 + $2 is equal to $sum" # Note the backquotes in the first line of the program.

19 Conditional Expressions if conditional expression then commands commandsfi Most of the time, the conditional expression will need the use of the test command.Most of the time, the conditional expression will need the use of the test command. Note also commands and the then must start on separate lines.Note also commands and the then must start on separate lines. if conditional expression then commands commands elif conditional expression then commands commandselse fi

20 Conditional Expressions Argument Test is True if: -d file file is a directory -f file file is an ordinary file -r file file is readable -s file file size is greater than zero -w file file is writable -x file file is executable n1 -eq n2 Integer n1 is equal to n2 n1 -ge n2 Integer n1 is > = n2 n1 -le n2 Integer n1 is <= n2 n1 -ne n2 Integer n1 is not equal to n2 n1 -lt n2 Integer n1 is less then n2 s1 = s2 String s1 is equal to string s2 s1 != s2 String s1 is not equal to string s2

21 If statement example - tgif set `date`set `date` if test $1 = Friif test $1 = Fri then then echo "TGIF" echo "TGIF" elif test $1 = Sat || test $1 = Sun elif test $1 = Sat || test $1 = Sun then then echo "Yeah it is the weekend" echo "Yeah it is the weekend" elif test $1 = Mon elif test $1 = Mon then then echo " Monday stinks " echo " Monday stinks " elif test $1 = Tue elif test $1 = Tue then then echo "Thank goodness it is not Monday" echo "Thank goodness it is not Monday" elif test $1 = Wed elif test $1 = Wed then then echo "Hump day" echo "Hump day" else echo "Survivor Party Night" else echo "Survivor Party Night" fifi echo "$*"echo "$*"

22 The Case Statement case word in pattern1) command(s) ;; pattern2) commands(s) ;;... patternN) command(s) ;; esac # Commands are seperated by semicolongs # The group of commands is ended by two semicolons. # The OR symbol is just one vertical line (|) # the astrik (*) marks the default case.

23 Case Statement Example set `date` case $1 in Mon)echo "I do not wake up on Monday";; Tue) echo "Nothing good on TV might as well study";; Wed) echo "Wednesdays are useless";; Thu) echo "Watch Alias tape, Love Jenny Garner";; Fri) echo "Pizza Night";; Sat) echo "Cartoon day";; *) echo "It is Sunday"; echo "I can sleep late and tape Alias";; echo "I can sleep late and tape Alias";;esac

24 For Loops for variable [in list] do do command(s) command(s)doneExample: for test in "one" "two" "three" do do echo "$test" echo "$test" done done

25 While loops while condition do command(s) command(s)done Note: condition is the same syntax and behavior as in an if statement.

26 Until Loops until condition do do command(s) command(s) done done Note: condition is the same syntax and behavior as in an if statement.

27 Some useful scripts Source: Just Enough Unix by Paul Anderson. Modified by David Gaitros # Change a files permissions to # executable # File name is chex. # Pass in the file as a parameter if test -f $1 then then chmod u+x $1 chmod u+x $1 echo "File $1 is executable" echo "File $1 is executable" ls -l $1 ls -l $1 else else echo "$1 is not a file" echo "$1 is not a file"fi

28 Some useful scripts Source: Just Enough Unix by Paul Anderson. Modified by David Gaitros #Removing files safely # File name is DELETE set filename = $1 if test ! -f $filename then echo "Not a valid file" then echo "Not a valid file"else echo "Do you really want to delete $filename (y/n)" echo "Do you really want to delete $filename (y/n)" read choice read choice if test $choice = y || test $choice = Y if test $choice = y || test $choice = Y then then rm $filename rm $filename echo "\"$filename\"deleted." echo "\"$filename\"deleted." else else echo "\"$filename\" not deleted." echo "\"$filename\" not deleted." fi fifi


Download ppt "Day 8,9,10 COP 3502 Introduction to Shell Scripts David A. Gaitros Department of Computer Science Florida State University."

Similar presentations


Ads by Google