Linux Programming - Lecture Notes On UNIT 2
Linux Programming - Lecture Notes On UNIT 2
Linux Programming - Lecture Notes On UNIT 2
#! /bin/bash
#! /bin/sh
to run:
make executable: % chmod +x script
invoke via:
% ./script
2
User input
shell allows to prompt for user input
Syntax:
read varname [more vars]
or
read p "prompt" varname [more vars]
Meaning
Name of the current shell script
Positional parameters 1 through 9
$#
$*
$@
$?
$$
The set
command can
be used to
assign values to
positional
parameters
if statement
if command
then
statements
fi
test command
Syntax:
test expression
[ expression ]
evaluates expression and returns true or false
Example:
if test w "$1"
then
echo "file $1 is write-able"
fi
10
11
12
The ifstate e t
if [ condition ]; then
statements
elif [ condition ]; then
statement
else
statements
fi
itself
13
Relational Operators
Meaning
Numeric
String
Greater than
-gt
-ge
Less than
-lt
-le
Equal
-eg
= or ==
Not equal
-ne
!=
-n str
-z str
14
not
and, or
must be enclosed within
&&
||
and
or
[[
]]
15
16
File Testing
-d file
-f file
-r file
-w file
-x file
-s file
Meaning
True if file is a directory
True if file is an ord. file
True if file is readable
True if file is writable
True if file is executable
True if length of file is nonzero
19
20
let Net=$Income-$Expense
if [ "$Net" -eq "0" ]; then
echo "Income and Expenses are equal - breakeven."
elif [ "$Net" -gt "0" ]; then
echo "Profit of: " $Net
else
echo "Loss of: " $Net
fi
23
multiple choices
Syntax:
case word in
pattern1) command-list1
;;
pattern2) command-list2
;;
patternN) command-listN
;;
esac
24
case pattern
checked against word for match
may also contain:
*
?
[ ]
[:class:]
multiple patterns can be listed via:
|
25
26
28
do-while, repeat-until
for
select
Functions
Trapping signals
29
Repetition Constructs
30
32
33
34
36
37
39
arguments:
#! /bin/bash
for parm
do
echo $parm
done
41
Select command
Constructs simple menu from word list
Allows user to enter a number instead of a word
User enters sequence number corresponding to the
word
Syntax:
select WORD in LIST
do
RESPECTIVE-COMMANDS
done
Loops until end of input, i.e. ^d (or ^c)
42
Select example
#! /bin/bash
select var in alpha beta gamma
do
1) alpha
2) beta
echo $var
3) gamma
done
Prints:
#? 2
beta
#? 4
#? 1
alpha
43
Select detail
Output:
PS3 is select sub-prompt
select ...
$REPLY is user input (the number)
1) alpha
2) beta
? 2
#! /bin/bash
2 = beta
PS3="select entry or ^D: " ? 1
1 = alpha
select var in alpha beta
do
echo "$REPLY = $var"
done
44
Select example
#!/bin/bash
echo "script to make files private"
echo "Select file to protect:"
select FILENAME in *
do
echo "You picked $FILENAME ($REPLY)"
chmod go-rwx "$FILENAME"
echo "it is now private"
done
45
47
This iteration is
over; do the next
iteration
48
Example:
for index in 1 2 3 4 5 6 7 8 9 10
do
if [ $index le 3 ]; then
echo "continue"
continue
fi
echo $index
if [ $index ge 8 ]; then
echo "break"
break
fi
done
49
DONE !
still to come
50
Shell Functions
A shell function is similar to a shell script
stores a series of commands for execution later
shell stores functions in memory
shell executes a shell function in the same shell that
called it
Where to define
In .profile
In your script
Or on the command line
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
Syntax:
function-name () {
statements
}
52
Example: function
#!/bin/bash
funky () {
# This is a simple function
echo "This is a funky function."
echo "Now exiting funky function."
}
# declaration must precede call:
funky
53
Example: function
#!/bin/bash
fun () { # A somewhat more complex function.
JUST_A_SECOND=1
let i=0
REPEATS=30
echo "And now the fun really begins."
while [ $i -lt $REPEATS ]
do
echo "-------FUNCTIONS are fun-------->"
sleep $JUST_A_SECOND
let i+=1
done
}
fun
54
Function parameters
Need not be declared
Arguments provided via function call are
$#
$0
55
58
Example: function
#! /bin/bash
global="pretty good variable"
foo () {
local inside="not so good variable"
echo $global
echo $inside
global="better variable"
}
echo $global
foo
echo $global
echo $inside
59
Handling signals
Unix allows you to send a signal to any process
-1 = hangup
-2 = interrupt with ^C
no argument = terminate
-9 = kill
kill
-9 cannot be blocked
Signals on Linux
% kill -l
1) SIGHUP
5) SIGTRAP
9) SIGKILL
13) SIGPIPE
17) SIGCHLD
21) SIGTTIN
25) SIGXFSZ
29) SIGIO
35) SIGRTMIN+1
39) SIGRTMIN+5
43) SIGRTMIN+9
47) SIGRTMIN+13
51) SIGRTMAX-13
55) SIGRTMAX-9
59) SIGRTMAX-5
63) SIGRTMAX-1
2)
6)
10)
14)
18)
22)
26)
30)
36)
40)
44)
48)
52)
56)
60)
64)
SIGINT
SIGABRT
SIGUSR1
SIGALRM
SIGCONT
SIGTTOU
SIGVTALRM
SIGPWR
SIGRTMIN+2
SIGRTMIN+6
SIGRTMIN+10
SIGRTMIN+14
SIGRTMAX-12
SIGRTMAX-8
SIGRTMAX-4
SIGRTMAX
3)
7)
11)
15)
19)
23)
27)
31)
37)
41)
45)
49)
53)
57)
61)
SIGQUIT
SIGBUS
SIGSEGV
SIGTERM
SIGSTOP
SIGURG
SIGPROF
SIGSYS
SIGRTMIN+3
SIGRTMIN+7
SIGRTMIN+11
SIGRTMIN+15
SIGRTMAX-11
SIGRTMAX-7
SIGRTMAX-3
4)
8)
12)
16)
20)
24)
28)
34)
38)
42)
46)
50)
54)
58)
62)
SIGILL
SIGFPE
SIGUSR2
SIGSTKFLT
SIGTSTP
SIGXCPU
SIGWINCH
SIGRTMIN
SIGRTMIN+4
SIGRTMIN+8
SIGRTMIN+12
SIGRTMAX-14
SIGRTMAX-10
SIGRTMAX-6
SIGRTMAX-2
^C is 2 - SIGINT
61
Handling signals
Default action for most signals is to end process
term: signal handler
Bash allows to install custom signal handler
Syntax:
trap 'handler commands' signals
Example:
trap 'echo do not hangup'
1 2
62
#! /bin/sh
trap 'justonce' 2
justonce() {
echo "not yet"
trap 2
}
# now reset it
while true; do
echo -n "."
sleep 1
done
66
67
#! /bin/bash -xv
68
DONE !
69