Info 212
Info 212
Shell Scripting
• So far, we have used shells interactively.
#!/bin/bash
echo Name a fruit?
read FRUIT
echo Vegetable?
read VEGGIE
echo $FRUIT and $VEGGIE are healthy foods
4
io.sh
• echo statement will print multiple arguments.
7
Special Symbols
8
symbols.sh
#!/bin/bash
# This is a comment
# Special symbols include # $ \ ’ {}
HI=Hello
echo HI
echo $HI
echo \$HI
echo '$HI'
echo "$HI"
echo "$HIJ"
echo "${HI}J"
12
Variables
• Variables do not need to be explicitly declared before
being used.
14
Variables
• By convention, variables are often named using all
uppercase letters. This allows them to be seen more
easily and differentiated from other code.
#!/bin/bash
ROCKS=4
echo Price?
read PRICE
echo "$ROCKS rocks for sale, $PRICE each"
15
Automatically Declared Variables
#!/bin/bash
echo Run with $# arguments
echo First three command line arguments:
echo $0
echo $1
echo $2
echo $3
echo PID is $$
#!/bin/bash
18
Loops
• You can also loop through using range of numbers in the
for loop “in” using brace expansion.
• Increment is 1 by default.
20
loops2.sh
• while statement is used in shell scripting as it is used in
many languages, namely, for the processing of data of
unknown length.
#!/bin/bash
NAME=Unknown
while [ $NAME != "Fred" ]
do
echo Who are you?
read NAME
echo “Let me see ...”
done
21
echo Found you!
Let keyword
#!/bin/bash
a=2334 # Integer.
let "a += 1"
echo "a = $a " # a = 2335
22
Conditional
• if statement is the primary method of program flow
control in shell scripting.
if [ conditional expression ]
then
statement1
statement2
. 23
fi
conditional.sh
#!/bin/bash
NAME="Fred“
if [ $NAME = "Fred" ]
then
echo Matches
fi
24
conditional.sh
#!/bin/bash
if [ $1 = "Fred" ]
then
echo Matches
fi
>./conditional.sh Fred 25
Test
• if statement (and while statement) use the test program
to evaluate a condition.
if [ conditional expression ]
then
statement1
statement2
.
fi 26
test.sh
#!/bin/bash
NAME="Joe“
if test $NAME = "Fred"
then
echo Hello Fred
fi
if [ $NAME = "Joe" ]
then
echo Hello Joe
fi 27
Test
• The square brackets syntax is usually preferred because
it is shorter and somewhat easier to read.
28
Conditional
• if statement can contain else clauses, similar to most
programming languages.
if [ conditional expression ]
then
statement1
…
else
statement2
….
fi 29
conditional2.sh
#!/bin/bash
NAME="Joe“
if [ $NAME = "Fred" ]
then
echo Matches
else
echo No match
fi 30
Conditional
• Multiple if statements can be chained using the elif
statement.
if [ conditional expression ]
then
statement1
elif [ conditional expression2 ]
then
statement2
else
statement3
31
fi
conditional3.sh
#!/bin/bash
COUNT=99
if [ $COUNT -eq 100 ]
then
echo "Count is 100“
elif [ $COUNT -gt 100 ]
then
echo "Count is greater than 100“
else
echo "Count is less than 100“
fi 32
Conditional
• if statements can also be used to evaluate simple string
equivalencies, non-equality and other conditions.
33
conditional4.sh
#!/bin/bash
NAME="Fred”
if [ "$NAME" != "Joe" ]
then
echo Where\’s Joe?
fi
34
conditional5.sh
#!/bin/bash
GHOST="“
if [ -z "$GHOST" ]
then
echo Boo!
fi
if [ -n "$GHOST" ]
then
echo I’m not a ghost.
35
fi
Conditional
• The flag -lt tests if the first argument is less than the
second.
• The flag -gt tests if the first argument is greater than the
second.
• The flags -le and -ge also test for equality but include the
case where the arguments are equal in the evaluation.
36
conditional6.sh
#!/bin/bash
i=7
j=9
• The flags -r, -w and -x test whether or not the read, write,
and execute permissions for the file are set.
38
conditional7.sh
#!/bin/bash
FILE=conditional7.sh
if [ -f "$FILE" ]
then
echo FILE exists
fi
if [ -x "$FILE" ]
then
echo FILE is executable
39
fi