Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
NET212
1
11/26/2007
If-then statements
if [ myvar = “floppy”
floppy ]
then
echo Paperweight
fi
Variables
How does a variable help us and what are they?
Variables
` They names that are associated with places in memory
` They are placeholders
Memory (RAM)
--------------
| “floppy”
floppy | < --- myvar (address 10)
| |
| |
|_______|
` Variables can be used to store data and perform
computations.
` If a computer counts to some number, then how can it do so w/o
storing a variable?
2
11/26/2007
If-then statements
` How can if-then statements be helpful and what are they?
If-then statements
` They allow a script (or program) to be flexible
` Respond to situations differently depending on the context
Loops
` Loops will run a set of statements over and over again
until a condition is met
While loop
stoppoint=3
i=1
while [ $i –lt $stoppoint ]
do
echo i
i = $[$i + 1]
done
3
11/26/2007
Results:
1
2
Perl
` Like shell scripts Perl is interpreted
` An interpreted language is not compiled into binary code
` It is parsed through an interpreter
` Perl is a general purpose programming language, so it can
do just about anything
` What is the interpreter for BASH shell scripts and the
commands you type in text mode?
4
11/26/2007
Compilation example
` A simple c program:
#include <stdio.h>
main(){
printf( hello!\n );
printf(“hello!\n”);
return 0;
}
Compile it to produce a binary file Æ gcc –o helloprog
helloprog.c
gcc –o programname sourcecode
Perl variables
` Define a variable
#!/bin/perl
$myvar = “floppy”; # pound sign is for a comment
$num = 1; #notice the semicolons after each statement!
Run it:
./testperl.pl
I have 1 floppy
Output
123
tom
5
11/26/2007
print “$myresult”;
Perl if-statements
#!/bin/perl
$mynum = 4;
$mystr = “sally”;
Perl if-statements
Numerical conditional operators
==
!=
>
<
String conditional operators
eq
ne
6
11/26/2007
Perl loops
For loops
#!/bin/perl
for ( $i = 0; $i < 2 ; i++){
print “hey there!\n”;
}
@myarray = ( “sally”,”tom”);
foreach $person (@myarray){ #special for each loop; will do for each element in
myarray
print “I know $person! \n”; #$person is a way we can assign piece of array to use
}
Output
Hey there!
Hey there!
I know sally!
I know tom!
While loops
While loop
#!/bin/perl
$i = 3
while ( $i > 0 ){
print “$i”;
$i = $i + 1; # What’s wrong with this?
}
Output
3
4
5
mysql
` Mysql is a free database managements system
(DBMS)
` A DBMS allows you to create, modify, and read databases
` What are some other DBMSs?
` A database is basically table with columns and rows
` A column is an attribute of a row
` A row is a record in the table
7
11/26/2007
Example table
Student ID Paid ` Each record (row) has the
Tom 1 Y
following attributes
Sally 2 Y
` Student, ID, Paid
Frederick 3 N
` Each attribute has a data
type
` Student : TEXT
` ID : INT
` Paid : CHAR
USE net212;
212 Å Not
N use it!
i!
8
11/26/2007
Updating information
` I want to change the Penquin row, so he is now paid
Deleting records
` I want to delete the first record
9
11/26/2007
DROP students;
` Delete a database
DROP net212;
Give this user access to the net212 database and all its tables
10