UNIX Commands and Scripting 6
UNIX Commands and Scripting 6
2
BASH SHELL PROGRAMMING
Input
prompting user
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’
if-then-else
case
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 IF…STATEMENT
if [ condition ]; then
statements
and, or
&& and must be enclosed within
|| or
[[ ]]
15
EXAMPLE: USING THE ! OPERATOR
#!/bin/bash
16
EXAMPLE: USING THE && OPERATOR
#!/bin/bash
Bonus=500
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
22
echo "You entered " $reply
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 ;;
User input
Control structures
if-then-else
case
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 as
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
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
44
SELECT EXAMPLE
#!/bin/bash
echo "script to make files private"
select FILENAME in *
do
echo "You picked $FILENAME ($REPLY)"
chmod go-rwx "$FILENAME"
echo "it is now private"
done
45
BREAK AND CONTINUE
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
if [ $index –le 3 ]; then
Repetition DONE !
do-while, repeat-until
for
select
Functions
Traps still to come
50
SHELL FUNCTIONS
A shell function is similar to a shell script
stores a series of commands for execution later
Remove a function
Use unset built-in
51
SHELL FUNCTIONS
must be defined before they can be referenced
usually placed at the beginning of the script
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 won’t 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 "."
66
sleep 1
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
Repetition DONE !
do-while, repeat-until
for
select
Functions
Traps
69