Programming Concepts
Define the problem Outline the solution Develop the outline into an algorithm Test the algorithm for correctness Code the algorithm into a specific language Run the program on the computer Document and maintain the program
R.N. Sangwan www.allianceit.in/unix
1
Define the Problem
Examine the problem until you thoroughly understand it
May
involve writing a problem description yourself
Divide the problem into three separate components
Inputs
Outputs
Processing
steps needed to produce the outputs
R.N. Sangwan www.allianceit.in/unix
2
Outline the Solution
Once the problem is understood, break the problem into smaller tasks or steps and outline a solution Outline may include:
Major
processing steps Major subtasks (if any) Major control structures (loops) Major variables and data structures Mainline logic
R.N. Sangwan www.allianceit.in/unix
3
Develop Outline into an Algorithm
Expand the outline that was developed into an algorithm So what's an algorithm?
A
set of precise steps which describe exactly the tasks to be performed and the order in which they are to be carried out An algorithm must: Be lucid, precise, and unambiguous Give the correct solution in all cases Eventually end Algorithm may be developed in pseudo-code, an English-like language
R.N. Sangwan www.allianceit.in/unix
4
Pseudo-Code
Statements are written in simple English without regard to the final programming language Each instruction is written on a separate line Keywords and indentation are used to signify control structures (if-then-else, do-while, while, until) Each set of instructions is written from top to bottom with only one entry and exit Groups of statements may be combined in modules and given a name
R.N. Sangwan www.allianceit.in/unix
5
Test the Algorithm for Correctness
The most critical step, and the one most often forgotten (although documenting is a close second :) Desk check the algorithm to find logic errors early
Errors
found now are (relatively) easy to correct After your code is written, it becomes much more difficult
To desk check, you play computer, walking through you algorithm with test data just as a computer would, keeping track of major variables on a sheet of paper
6
R.N. Sangwan www.allianceit.in/unix
Code the Algorithm
After the previous steps have been successfully completed, code the resultant algorithm in the desired implementation language
R.N. Sangwan www.allianceit.in/unix
7
Run the Program
Execute the program on the computer
After
any syntax errors are removed, logiv testing is performed Using test data and test cases, verify proper operation If errors are found, debug, fix, and re-execute the test
This is the usually the most rewarding step of the entire process
R.N. Sangwan www.allianceit.in/unix
8
Document and Maintain
A vital step in the development process, although often overlooked (or avoided!) Even the best programmer forgets what he wrote and how it works after time Documentation includes:
Internal
documentation (module headers and code comments) External documentation (test data, design documentation, user documentation)
R.N. Sangwan www.allianceit.in/unix
9
Scripting
The Bourne shell is what we will use for scripting
It's
faster (remember? Why?) It's portable Virtually every Unix system has sh installed It has a rich set of programming constructs
R.N. Sangwan www.allianceit.in/unix
10
Comments
# is the comment symbol in the shell # can occur anywhere on a line
The
shell ignores anything following a # until the endof-line
One special exception
#!
on the first line is used to tell the shell what program to use to interpret a script file Examples: #!/bin/sh - tells shell to use the Bourne shell to execute the script #!/usr/bin/perl - tells shell to use Perl to execute the script
R.N. Sangwan www.allianceit.in/unix
11
Shell Output
echo is the primary way to perform output from the shell Syntax: echo [-n] arguments
-n
- do not append a NEWLINE arguments - may be variables or explicit strings
Examples
echo
"Hi there, I'm a Unix whiz kid!" echo "My home directory is $HOME" echo $PATH echo
To suppress output from shell commands in a script, redirect the output to /dev/null
12
R.N. Sangwan www.allianceit.in/unix
Shell Variables
To store values in a shell variable, write the name of the variable followed by an = followed by the value
count=1 my_dir=/home/clyde/krf
Note that spaces are NOT allowed on either side of the = Also, the shell has no concept of data types No matter what assignment you make, the shell considers the value as a string of characters Variables don't need to be declared, they're simply assigned values when you want to use then
R.N. Sangwan www.allianceit.in/unix
13
Referring to Variables
In order to refer to the value of a variable, preface the variable name with a $
echo
$count - displays 1 echo count - displays count echo $my_dir - displays /home/clyde/krf echo my_dir - displays my_dir
If you want to ensure a variable is not accidentally modified, specify it as readonly. Further attempts to modify it will result in an error from the shell.
my_dir=/home/clyde/krf readonly
my_dir
14
R.N. Sangwan www.allianceit.in/unix
File Name Substitution & Variables
If you define a variable as x=*, ls $x will produce a directory list Did the shell store the * in x or the list of files in your current directory?
R.N. Sangwan www.allianceit.in/unix
15
File Name Substitution & Variables
If you define a variable as x=*, ls $x will produce a directory list Did the shell store the * in x or the list of files in your current directory?
The shell stored the * in x, the shell does not perform filename substitution when assigning values to variables What actually happens is:
The
shell scans ls $x, substituting * for $x It then rescans the line and substitutes all the files in the current directory for the *
R.N. Sangwan www.allianceit.in/unix
16
Environment
Create a file called vartest.scr that contains:
echo :$x:
Save it and make it executable (chmod 744 vartest.scr) Now, assign 100 to x
x=100
Then execute vartest.scr - what happens?
R.N. Sangwan www.allianceit.in/unix
17
Variables in the Environment
When variables are assigned, they are local to the current shell Since scripts are executed in a sub-shell, these local variables aren't visible to the script To make them visible (inherited by) subsequent sub-shells, they must be exported
export
my_dir
The env command lists all currently defined exported variables
R.N. Sangwan www.allianceit.in/unix
18
Pre-defined Variables
$! - PID of last command executed in background $# - number of arguments on command line $* - collectively references all of the positional parameters ($1, $2, $3,…) $0 - name of program being executed $$ - PID of current process $? - exit status of last command not executed in background "$@" - just like $* except it returns arguments as though they were individually quoted, ie; "$1 "$2" "$3"…"$9"
R.N. Sangwan www.allianceit.in/unix
19
Debugging Scripts with -x
When developing scripts, it is often dificult to debug them In order to get a trace of what is happening, you can invoke your script by using the shell's -x option
sh
-x case.scr This will trace the statements in the script as they are being executed
R.N. Sangwan www.allianceit.in/unix
20
Quoting
Quoting is a necessity in shell scripting There are four types of quoting '…' removes the special meaning of all enclosed characters "…" removes the special meaning of all enclosed characters EXCEPT $, `, and \ \c removes the special meaning of the character that follows the \; inside double quotes it removes the special meaning of $, `, ", NEWLINE, and \ but is otherwise not interpreted `command` executes command and inserts standard output at that point
R.N. Sangwan www.allianceit.in/unix
21
Back Quoting
echo Your current directory is `pwd`
Outputs
"Your current directory is /home/clyde/krf" "There are 13 users logged on"
echo There are `who | wc -l` users logged on
Outputs
Since single quotes protect everything, the following output should make sense:
echo
'`who | wc -l` tells how many users are logged on' Outputs "who | wc -l` tells how many users are logged on"
But back quotes are interpreted inside "
echo
"You have `ls | wc -l` files in your directory" Outputs "You have 24 files in your directory" Why?
R.N. Sangwan www.allianceit.in/unix
22
Passing Arguments
When the shell invokes your script, it assigns the command line arguments to a set of variables
$0,
$1, $2,…$9 $0 is the script name $1 is the first argument, $2 the second, up through $9 You can then refer to these variables in your script
If more than 9 arguments are used, you must access them using the shift command
shift
simply does a left shift on all the arguments, discarding $1 and making $1 = $2, $2 = $3, etc Note, this means that if you still need $1 you must save it in another variable BEFORE performing the shift
R.N. Sangwan www.allianceit.in/unix
23
Interactive Input
Data is obtained from the user by using read
Syntax:
read variables Example: read x y read text read gets input from STDIN so it can be redirected read text < data
This doesn't work in all version of sh so an alternative is the line command line reads an entire line from STDIN and writes it to STDOUT
text=`line
< data`
24
R.N. Sangwan www.allianceit.in/unix
set
There is no way to do a direct assignment to the positional variables
1=100
or 3=filename doesn't work
set gets around this by indirectly assigning values to positional variables ($1, $2,..) set and baker charlie produces the following results
echo
$1 $2 $3 and baker charlie
When using set, $# and $* also work properly
R.N. Sangwan www.allianceit.in/unix
25
Looping
The shell has three built-in looping constructs
for while until
These let you execute a set of commands either a specific number of times or until some condition is met
R.N. Sangwan www.allianceit.in/unix
26
for
General format of the for loop is:
for var in word1 word2 … wordn do command command … done
When the loop is executed, first word1 is assigned to var and the body of the loop is executed Then word2 is assigned to var, followed by word3 until all the words have been processed
R.N. Sangwan www.allianceit.in/unix
27
More for
for i in 1 2 3 do
echo $i
done for i in $* do
echo $i
done
R.N. Sangwan www.allianceit.in/unix
28
Special Form of for
A special notation is recognized by the shell If you write
for var do command command …. done
The shell will automatically sequence through all the arguments typed on the command line
R.N. Sangwan www.allianceit.in/unix
29
testing for Decision Making
test gives the shell the ability to make TRUE or FALSE decisions, returning a zero exit status if the condition evaluates to TRUE test is often used to test conditions in an if, while, or until command The test command has two possible formats
test
R.N. Sangwan www.allianceit.in/unix
condition [ condition ] where condition is an operator or a set of operators ANDed an/or ORed together NOTE: when using the [ condition ] format, there must be a space separating the [ and ] symbols from the conditon 30
File Operators
Operator -d file -f file -r file -s file -w file -x file
R.N. Sangwan www.allianceit.in/unix
Returns TRUE (zero exit status) if file is a directory file is an ordinary file file is readable by the process file has non-zero length file is writable by the process file is executable
31
String Operators
Operator string -n string Returns TRUE (zero exit status) if string is not null string is not null ( and string must be seen by test) string is null (and string must be seen by test) string1 is identical to string2 string1 is not identical to string2
32
-z string string1 = string2 string1 != string2
R.N. Sangwan www.allianceit.in/unix
Integer Comparison Operators
Operator int1 -eq int2 int1 -ge int2 int1 -gt int2 int1 -le int2 int1 -lt int2 int1 -ne int2
R.N. Sangwan www.allianceit.in/unix
Returns TRUE (zero exit status) if int1 is equal to int2 int1 is greater than or equal to int2 int1 is greater than int2 int1 is less than or equal to int2 int1 is less than int2 int1 is not equal to int2
33
Boolean Operators
Returns TRUE (zero exit status) if expr is FALSE; otherwise ! expr return TRUE expr1 -a expr2 expr1 is TRUE and expr2 is TRUE expr1 -o expr2 expr1 is TRUE or expr2 is TRUE Note: the -a operator has a higher precedence than - o. This means that:
expr1 -o expr2 -a expr3 is interpreted as: expr1 -o (expr2 -a expr3) not (expr1 -o expr2) -a expr3
R.N. Sangwan www.allianceit.in/unix
Operator
34
Boolean Truth Table
Var A FALSE TRUE FALSE TRUE
Var B FALSE FALSE TRUE TRUE
AND FALSE FALSE FALSE TRUE
OR FALSE TRUE TRUE TRUE
R.N. Sangwan www.allianceit.in/unix
35
clyde% ls -l -rw-r--r-- 1 krf 0 Nov 24 -rw-r--r-- 1 krf 38 Nov 24 drwxr-xr-x 2 krf 512 Nov 24 -rw-r--r-- 1 krf 0 Nov 24 -rw-r--r-- 1 krf 31 Nov 24 clyde% count=22 lines=7 score=15 answer=`expr $lines + 15` name="Holly Dunn" my_name= name1=$name dog1=Bear dog2=Nitro
R.N. Sangwan www.allianceit.in/unix
13:30 13:30 13:29 13:29 13:29
Terri martina reba sara trisha
36
while
while executes the commands between the do and done while the return status from commandt is TRUE (zero) while commandt do
command command …
done Often test is the command used in the while loop as commandt
R.N. Sangwan www.allianceit.in/unix
37
until
until executes the commands between the do and done until commandt returns a TRUE status until commandt do
command command ….
done Again, the test command is often used for commandt in until loops although other commands may certainly be used instead
R.N. Sangwan www.allianceit.in/unix
38
More On Loops
Breaking out of loops
Sometimes
you may want to exit immediately from a
loop break allows you to do this
Skipping the remaining commands in a loop
Sometimes
you are in the middle of executing a pass through a loop and, rather than breaking out of the loop just want to skip the rest of the commands in this pass continue will let you do that
R.N. Sangwan www.allianceit.in/unix
39
Shell Arithmetic
The Bourne shell has no idea how to do arithmetic For example
number=2 number=$number + 4 echo $number 2+4
That makes shell scripting pretty useless as a programming language doesn't it?
R.N. Sangwan www.allianceit.in/unix
40
expr
Fortunately, there is a Unix command that will allow us to perform arithmetic within a script The expr command "evaluates" it's arguments and writes it's output on STDOUT Example:
expr 1 + 2 3 expr 6 / 2 + 5 8
R.N. Sangwan www.allianceit.in/unix
Note, since it is evaluating arguments, they must be separated by spaces Also, expr only works with integer arithmetic expressions 41
Decision Making
The Bourne shell provides a variety of decision making tools for your use
test if-else case &&
and ||
R.N. Sangwan www.allianceit.in/unix
42
if-else
if-else allows you to execute a series of commands if some test is TRUE, if not you can execute a different set of commands if commandt then
command command ….
fi if commandt returns a TRUE (zero status) then the following commands are executed
R.N. Sangwan www.allianceit.in/unix
43
else
Another optional form adds an else clause
if commandt then command command …. else command command …. fi
R.N. Sangwan www.allianceit.in/unix
44
elif
One other form combines multiple ifs and elses
if commandt then command …. elif then command … else command fi
R.N. Sangwan www.allianceit.in/unix
45
case
The case command allows you to compare a single value against multiple values and execute one or more commands when a match is found
case value in pat1 ) command …. command;; pat2 ) command …. command;; *) command;; esac
R.N. Sangwan www.allianceit.in/unix
46
Patterns in case Statements
You can use the same special characters in case statement pattern specifiers as you do in shell file name substitutions
?
matches any single character * matches zero or more occurrences of any character […] matches any characters enclosed in the brackests
R.N. Sangwan www.allianceit.in/unix
47
&& and ||
&& and II allow you to conditionally execute commands command1 && command2
command2
executes only if command1 returns a zero status (executed successfully)
command1 || command2
command2
executes only if command1 returns a nonzero status (did not execute successfully)
Examples
who | grep "krf" > /dev/null && echo "Ken is logged on" who | grep "joe" > /dev/null || echo "Joe is not logged on"
R.N. Sangwan www.allianceit.in/unix
48
Hold on a Minute!
Sometimes, you want your shell script to stop processing until either something happens or for some amount of time The wait and sleep commands provide these functions wait will cause the script to suspend execution until a specific process finishes
It's
syntax is wait n where n is the PID of the process you want to wait for Recall that $! can be used to get the PID of the last process sent to the background
R.N. Sangwan www.allianceit.in/unix
49
An example of wait might be
sort big_file > sorted_file & pid=$! command command …. wait $pid plot sorted_file
Here, we've started a large sort in the background. We want to do some other things while it is sorting but we can't plot the data until the sort has finished
50
R.N. Sangwan www.allianceit.in/unix
Sweet Dreams
We've seen sleep before when we looked a job control. It suspends execution for a specified time. Syntax: sleep n
where
n is the number of seconds to sleep
It's used to execute a command after a certain amount of time as in:
(sleep 105; command)&
or to execute a command every so often, as in:
while true do command sleep 37 done R.N. Sangwan www.allianceit.in/unix
51
I'm Outta' Here
The exit command causes the current shell script to exit immediately and return the status specified by the exit command Syntax: exit n
where
n is the desired exit status if n is not given, exit returns the exit status of the last command that was executed by the script
exit can be used to return diagnostic error codes when something goes wrong with your script
This
is usually most interesting when your script calls other scripts, by using exit codes, you can incorporate error handling in you upper level scripts
52
R.N. Sangwan www.allianceit.in/unix