CSCI 330 The Unix System: Bash Programming
CSCI 330 The Unix System: Bash Programming
CSCI 330 The Unix System: Bash Programming
2
BASH SHELL PROGRAMMING
Input
promptinguser
command line arguments
or
5
SPECIAL SHELL VARIABLES
Parameter Meaning
$0 Name of the current shell script
6
EXAMPLES: COMMAND LINE
ARGUMENTS
% set tim bill ann fred
$1 $2 $3 $4
The set
8
IF STATEMENT
if command
then
9
TEST COMMAND
Syntax:
test expression
Example:
if test w "$1"
then
echo "file $1 is write-able"
fi 10
THE SIMPLE IF STATEMENT
if [ condition ]; then
statements
11
THE IF-THEN-ELSE STATEMENT
if [ condition ]; then
statements-1
12
THE IFSTATEMENT
if [ condition ]; then
statements
15
EXAMPLE: USING THE ! OPERATOR
#!/bin/bash
16
EXAMPLE: USING THE &&
OPERATOR
#!/bin/bash
19
EXAMPLE: FILE TESTING
#!/bin/bash
echo "Enter a filename: "
20
EXAMPLE: FILE TESTING
#! /bin/bash
* "TEST" COMMAND
read -p "Do you want to continue?" reply
if test $reply = "y"; then
echo "You entered " $reply 22
fi
EXAMPLE: IF..ELIF... STATEMENT
#!/bin/bash
let Net=$Income-$Expense
25
EXAMPLE 1: THE CASE STATEMENT
#!/bin/bash
echo "Enter Y to see all files including hidden files"
echo "Enter N to see all non-hidden files"
case $reply in
Y|YES) echo "Displaying all (really) files"
ls -a ;;
N|NO) echo "Display all non-hidden files..."
ls ;;
Q) exit 0 ;;
28
BASH PROGRAMMING: STILL TO
COME
Control structures
select
Functions
Trapping signals
29
CSCI 330 - The Unix System
30
REPETITION CONSTRUCTS
THE WHILE LOOP
Purpose:
To execute commands in command-list as long
Syntax:
while [ expression ]
do
command-list
done
31
EXAMPLE: USING THE WHILE LOOP
#!/bin/bash
COUNTER=0
32
EXAMPLE: USING THE WHILE LOOP
#!/bin/bash
33
EXAMPLE: USING THE WHILE LOOP
#!/bin/bash
# copies files from home- into the webserver- directory
# A new directory is created every hour
Syntax:
until [ expression ]
do
command-list
done
35
EXAMPLE: USING THE UNTIL LOOP
#!/bin/bash
36
EXAMPLE: USING THE UNTIL LOOP
#!/bin/bash
37
THE FOR LOOP
Purpose:
To execute commands as many times as the
Syntax:
for variable in argument-list
do
commands
done
38
EXAMPLE 1: THE FOR LOOP
#!/bin/bash
39
EXAMPLE 2: USING THE FOR LOOP
#!/bin/bash
# compute the average weekly temperature
let AvgTemp=$TempTotal/7
echo "Average temperature: " $AvgTemp 40
LOOPING OVER ARGUMENTS
simplest form will iterate over all command line
arguments:
41
SELECT COMMAND
Constructs simple menu from word list
Allows user to enter a number instead of a word
Syntax:
select WORD in LIST
do
RESPECTIVE-COMMANDS
done
42
Loops until end of input, i.e. ^d (or ^c)
SELECT EXAMPLE
#! /bin/bash
select var in alpha beta gamma
select FILENAME in *
do
echo "You picked $FILENAME ($REPLY)"
chmod go-rwx "$FILENAME"
echo "it is now private"
done 45
BREAK AND CONTINUE
Interrupt for, while or until loop
The break statement
46
THE BREAK COMMAND
while [ condition ]
do
47
THE CONTINUE COMMAND
while [ condition ]
do
48
EXAMPLE:
for index in 1 2 3 4 5 6 7 8 9 10
do
function-name () {
statements
}
52
EXAMPLE: FUNCTION
#!/bin/bash
funky 53
EXAMPLE: FUNCTION
#!/bin/bash
fun () { # A somewhat more complex function.
JUST_A_SECOND=1
55
EXAMPLE: FUNCTION WITH
PARAMETER
#! /bin/sh
testfile() {
if [ $# -gt 0 ]; then
testfile .
testfile funtest 56
EXAMPLE: FUNCTION WITH
PARAMETERS
#! /bin/bash
checkfile() {
for file
58
EXAMPLE: FUNCTION
#! /bin/bash
echo $global
foo
echo $global 59
echo $inside
HANDLING SIGNALS
Unix allows you to send a signal to any process
^C is 2 - SIGINT 61
HANDLING SIGNALS
Default action for most signals is to end process
term: signal handler
Example:
trap 'echo do not hangup' 1 2
62
EXAMPLE: TRAP HANGUP
#! /bin/bash
# kill -1 wont kill this process
while true
do
echo "try to hang up"
sleep 1
63
done
EXAMPLE: TRAP MULTIPLE
SIGNALS
#! /bin/sh
# plain kill or kill -9 will kill this
while true; do
echo -n .
sleep 1
done
64
EXAMPLE: REMOVING TEMP FILES
#! /bin/bash
trap 'cleanup; exit' 2
for i in 1 2 3 4 5 6 7 8
do
echo "$i.iteration"
touch /tmp/tempfile.$$.$i
sleep 1
done 65
cleanup
RESTORING DEFAULT HANDLERS
trap without a command list will remove a signal
handler
Use this to run a signal handler once only
while true; do
echo -n "."
sleep 1 66
done
DEBUG SHELL PROGRAMS
Debugging is troubleshooting errors that may
occur during the execution of a program/script
67
DEBUGGING USING SET
The set command is a shell built-in command
has options to allow flow of execution
69