CS155

CS155: Introduction to Unix

Fall 2017

Shell Scripts

See this page as a slide show

Bash Shell Scripting

CS155 ShellScripts

What’s a shell script?

Shell Scripts

Shell Scripts

#! /bin/bash
mkdir 155tmp
touch 155tmp/helloWorld
echo "Lala it's Saturday" > 155tmp/satdaze
cat 155tmp/satdaze
date

A common pitfall

Shell Variables

#! /bin/bash
dir=155tmp
mkdir $dir
touch $dir/helloWorld
echo "Lala it's Saturday" > $dir/satdaze
cat $dir/satdaze
date

Examples of setting variables:

CommandResult
alpha=beta beta
gamma=2+2 2+2
let delta=2+2 4
epsilon=$(date | cut -c5-7)Nov
iota=alpha alpha
kappa=$alpha beta

An Annoying Special Case

Special Shell Variables

VariableMeaning
$#how many arguments were given to the script
$0the name of the program
$1the first argument
$2the second argument
$*All the arguments in a single string, like "$1 $2 $3 ..."
$@ All the arguments in separate strings, like "$1" "$2" "$3" ...
$?0 if the previous command succeeded

How do you tell if $2 exists?

More About Shell Variables

#! /bin/bash
if [[ $# -ne 1 ]]
then
    echo "Usage: $0 file"
    exit 1
fi
ls -l $1

Basic Math

% echo $UID
2543
% let x="UID*2"
echo $x
5086

All numeric values in the shell are integers. There is no fractional part.

Control Flow: Conditional Execution

Often, we will only want to execute a command if some condition is true (or false).

if [[ expr ]]
then
     command
     command 
fi
if [[ expr1 ]]
then
    command1
elif [[ expr2 ]]
then
    command2
else
    command3
fi
if [[ expr ]]
then
     command1a
     command1b
else
     command2a
     command2b
fi

Logical operations

if [[ $1 -le 0 || $2 -le 0 ]]
then
    echo "All arguments must be positive."
    exit 1
fi

let age=52
sex="M"
if [[ age -ge 21 && $sex = "M" ]]
then
    echo "Voter in 1916!"
fi
if [[ ! ($sex = "M") ]]
then
    echo "Hello, ma'am!"
fi

Conditional Execution: Examples

Conditional examples:

% cat scrpt
#! /bin/bash
if [[ $HOSTNAME = "denver" ]]
then
    echo "Mile high!"
elif [[ $HOSTNAME = "lansing" ]]
then
    echo "Welcome to the mitten state!"
else
    echo "Where am I?"
fi
% ls -l scrpt
-rwx------  1 cs155 class 193 Nov 23 05:58 scrpt
% ./scrpt
Where am I?

A $? example

#! /bin/bash
grep "foo" xyz >dummy
if [[ $? -eq 0 ]]
then
    echo "Hooray, we found foo in the file xyz!"
else
    echo "Sorry, foo was not found in the file xyz."
fi
rm dummy                        # be tidy

Why didn’t we rm dummy immediately after creating it? We don’t need its contents—why not get rid of it as soon as possible?

Comparison Operators

if [[ COLUMNS -lt 80 ]]
then
    echo "This terminal is too narrow."
    echo "It’s only $COLUMNS characters wide."
    echo "This program requires at least 80 characters."
fi

A $ is not required on variables when doing numeric comparisons. You still need a $ when comparing strings.

Comparing strings

Here’s how to compare strings (not numbers):

a = bdoes a equal b ?
a != bdoes a not equal b ?
a < bis a less than b ?
a > bis a greater than b ?

Surprisingly, there are no or operators.
Remember: spaces around operators!

Comparing numbers

Here’s how to compare numbers:

a -eq bdoes a equal b ?
a -ne bdoes a not equal b ?
a -lt bis a less than b ?
a -gt bis a greater than b ?
a -le bis ab ?
a -ge bis ab ?

Remember: spaces around operators!

Combining comparisons:

a && bare both a and b true?
a || bare either a or b true?
! ais a false?

Remember spaces around operators!

Another conditional example

#! /bin/bash

let hour=$(date "+%k")              # 0–23

if [[ hour -lt 12 ]]                # Is it 0–11?
then
    echo "Good morning!"
elif [[ hour -lt 18 ]]              # Is it 12–17?
then
    echo "Good afternoon!"
else                                # Is it 18–23?
    echo "Good evening!"
fi

Yet another conditional example

#! /bin/bash

# $1 is the first argument to this program.
# $2 is the second argument to this program.
# $3 is the third argument to this program.

if [[ $1 -eq $2 && $2 -eq $3 ]]
then
    echo "They are all the same!"
else
    echo "This is no good—they’re different."
fi

File Inquiry Operators

if [[ -d my_file ]]
then
    rmdir my_file
fi
if [[ -e my_file ]]
then
    echo "my_file still exists"
fi

Spaces

If the world were consistent, life would be much easier.
In the following examples, spaces look like this.

Spaces are nearly forbidden in a let statement: let a=b+c/2

Spaces are required in an if statement: if [[ $a -eq 42 ]]

Modified: 2017-02-11T20:41

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building