Title: Day 8,9,10 COP 3502 Introduction to Shell Scripts
1Day 8,9,10COP 3502 Introduction to Shell Scripts
- David A. Gaitros
- Department of Computer Science
- Florida State University
2What 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. - Used when a function needs to be completed over
and over again. - Your startup scripts are shell scripts.
3Simple Shell Scripts
- The following text is placed in a file called
simple - A Comment starts with an
- cal
- date
- who
4How to run a shell script
- There are two ways
- 1. Use the (sh) command
- sh simple ltrgt
- 2. Change the permissions to executable and just
enter the file name - chmod 755 simple ltrgt
- chmod urwx,ax,ge simple ltrgt
- simple ltrgt
5The Shell as a programming language
- variables
- environment
- user created
- positional parameters
- input/output functions
- arithmetic operations
- conditional expressions
- selection structures
- repetition structures
6Variables
- 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.
7Unix Shell Stuff
- Standard Input and Output
- Standard input - stdin
- Standard output - stdout
- Standard error - stderr
- Redirection
- ls -al gt directory.outltrgt
- ls -al gtgt directory.out ltrgt Appends
- mail gaitrosd lt this.text ltrgt
8Unix Shell Stuff
- Pipes - The output of one command is input to
another. - ls -al more ltrgt
- 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 ()
9Unix Shell Stuff
- tee
- A tee allows you to save the output of one
command while also making it input to another - ls -al tee output.dat more ltrgt
10Unix Shell Stuff
- 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 .out - ls -al .out ltrgt
- You can put wildcards anywhere
- ls -al fall.out ltrgt
- 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 - ls -al ?ello.out ltrgt
- This lists all the files starting with any
character and ending in ello.out.
11Unix Shell Stuff
- Special Characters that have special meaning
- \ lt gt ( ) ? ' " / !
- 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\?" ltrgt
- 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.
12Unix Shell Stuff
Quote Effiect
\ 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.
13Variables
- User 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_htmlltrgt
- To use the value inside the variable you place a
in front of it. - ls -al stuff ltrgt
- NOTE typically, user defined variables are in
lower case to distinguish them from standard
shell variables.
14Positional 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. - 0 is always the shell script command.
- 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 the string that contains all the
arguments.
15Positional Parameters
- Here 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
16Input/Output Functions
- 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. - !/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
17Set 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. - set date
- echo "Time 4 5"
- echo "Day 1"
- echo "Date 3 2 6"
- Demonstration
18Arithmetic Operations
- The shell is not intended to do any extensive
mathematics. You should only perform simple
integer arithmetic. () () (-) (/) () - Use the expr UNIX utility.
- example
- sum expr 1 2 ltrgt
- echo "1 2 is equal to sum"ltrgt
- Note the backquotes in the first line of the
program.
19Conditional Expressions
- if conditional expression
- then
- commands
- fi
- 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. - if conditional expression
- then
- commands
- elif conditional expression
- then
- commands
- else
- commands
- fi
20Conditional 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 gt n2
n1 -le n2 Integer n1 is lt 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
21If statement example - tgif
- set date
- if test 1 Fri
- then
- echo "TGIF"
- elif test 1 Sat test 1 Sun
- then
- echo "Yeah it is the weekend"
- elif test 1 Mon
- then
- echo " Monday stinks "
- elif test 1 Tue
- then
- echo "Thank goodness it is not Monday"
- elif test 1 Wed
- then
- echo "Hump day"
- else echo "Survivor Party Night"
- fi
- echo ""
22The 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.
23Case 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"
- esac
24For Loops
- for variable in list
- do
- command(s)
- done
- Example
- for test in "one" "two" "three"
- do
- echo "test"
- done
25While loops
- while condition
- do
- command(s)
- done
- Note condition is the same syntax and behavior
as in an if statement.
26Until Loops
- until condition
- do
- command(s)
- done
- Note condition is the same syntax and behavior
as in an if statement.
27Some useful scriptsSource 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
- chmod ux 1
- echo "File 1 is executable"
- ls -l 1
- else
- echo "1 is not a file"
- fi
28Some useful scriptsSource 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"
- else
- echo "Do you really want to delete filename
(y/n)" - read choice
- if test choice y test choice Y
- then
- rm filename
- echo "\"filename\"deleted."
- else
- echo "\"filename\" not deleted."
- fi
- fi