360 likes | 469 Views
Introduction to UNIX and Perl. Todd Scheetz. UI Summer Bioinformatics Workshop. July 16, 2001. Definitions. Operating System provides a uniform interface between a computer’s hardware and user-level programs. Manages the low-level functionality of the hardware automatically.
E N D
Introduction to UNIX and Perl Todd Scheetz UI Summer Bioinformatics Workshop July 16, 2001
Definitions • Operating System • provides a uniform interface between a computer’s hardware and user-level programs. • Manages the low-level functionality of the hardware automatically. • Programming Language • provides a formal structure/syntax for implementing algorithmic procedures.
What is UNIX? • Operating system developed at Bell Labs. • originally written in assembly code • the C programming language was designed to implement a more portable version of UNIX • Multi-user • Multi-tasking
What is UNIX? (part 2) • Made available with source code at no cost • could fix bugs, add features or just test alternative methods • EXCELLENT for learning or teaching • Adopted by Berkeley to make BSD • virtual memory • paging • networking (TCP/IP)
What is UNIX? (part 3) • By programmers, for programmers • extensive facilities to allow people to work together and share information in controlled ways • time sharing system • Basic Guidelines • Principle of least surprise • every program should do one thing and do it well
UNIX hierarchy User i/f Users Library i/f Std. Utility Programs (shell, editor, compiler) System call i/f (open, close, fork, read, print, etc.) Standard Libr. (process mgmt, memory mgmt, file system, I/O, etc.) UNIX O/S Hardware (CPU, memory, disks, keyboard, etc.) Adapted from Tanenbaum, p. 273
UNIX Basics User Accounts - required to login in the computer with username and password. Groups - entity made up of one or more users. Sharing... Diane Bob Bill Mike Stacie group1 group2
UNIX Basics • File Sharing - Regulated by three sets of permissions. • Permissions: read, write, execute • Subjects: owner, group, all -rwxr-xr-x foo.pl -r-xr-xr-x bar.pl -rw------- secret -rw-r--r-- public
UNIX Basics • Super-user account • complete access to all files • Required for system administration tasks • add accounts/groups • change permissions/owners of any file • change password of any account • shutdown a machine
UNIX Basics UNIX Filesystem Hierarchy / bin dev etc lib tmp usr var bin doc lib local • Two shortcuts • . - the current directory • .. - the directory one level “up”
What is UNIX? Processes Each program executes as a process A process provides encapsulation for the program Under UNIX, multiple processes can be running at the same time! How to control processes: ^C -- break ^Z -- stop & -- start in background ps -- show which processes are running kill -- kill a process
What is UNIX? • grep - show every line from a file that matches a supplied pattern • Ex. grep sub my_program.pl • (would return every line in the file that contained the string ‘sub’) • ls - list files • Ex. ls *.pl • (would list all files in the current directory that end in ‘.pl’) • head - list the first lines in a file • Ex. head -20 my_program.pl • (would show the first 20 lines from my_program.pl) • sort - performs a lexical sorting of a file • Ex. sort my_program.pl
pipes What is UNIX? • UNIX also provides a method for stringing multiple programs together • Pipes… • Ex. • head -20 *.pl | grep File | sort
UNIX Basics UNIX Command Summary pwd - print working directory cd - change directory ls - list files mv - move a file (relocate/rename) rm - remove a file mkdir - make a new directory rmdir - remove a directory more - display the contents of a file (one screen as a time) chmod - change the permissions on a file chgrp - change the group associated with a file
UNIX Shell Shells a.k.a. command interpreter the primary user interface to UNIX interpret and execute commands 1. Interactive use 2. Customization of UNIX session (environment) 3. programmability /bin/sh - Bourne shell /bin/csh - C shell /bin/bash - Bourne again shell /bin/tcsh - modified, updated C shell
UNIX Shell • bash • prompt -- by default shows who you are, what machine the shell is running on, and what directory you are in. • PATH -- environment variable that defines where the shell should look for the programs you are running. • /bin • /usr/bin • /usr/local/bin • /usr/X11R6/bin • /usr/sbin • .
Installing Software • Pre-built vs. source • RPM vs. “raw” binaries • Process • downloading • extracting • compiling • installation • configuration • Now we’re going to install some software!! • http://huge.eng.uiowa.edu/~tscheetz/Workshop/
Programming Languages • Basics of a Perl program under UNIX • Perl is an interpreted language • The first line of a Perl program (in UNIX) is... • #!/usr/bin/perl • The # character is the comment character. • All single-expression statements must end in a semi-colon. • $area = 0.5 * $pi * $radius * $radius; • while (CONDITION) { • # some stuff • }
Programming Languages • Input/Output in Perl • Reading in from the keyboard... • $line = <STDIN>; • Filehandles... • File: • open(FH,”filename”); • open(FH,”>filename”); • ... • $line = <FH>; • ... • close(FH); • DO HELLO WORLD WALK-THROUGH.
Programming Languages • Data Types • Integer - 0, 1, 2, …, 1000, 1001, … • Floating Point - 0.0, 0.001, 0.0003, 3.14159265, … • Character - a, b, c, d, …, 0, 1, 2, :, !, … • Different languages use different conventions. In Perl, a string is also a basic data type. A string is a sequence of 0 or more characters.
Programming Languages • Variables - Pieces of data stored within a program. • (similar to variables in arithmetic) • scalar variables are distinguished by the ‘$’ at their front. • Any name beginning with a letter is allowed • $a • $a1 • $alphabet_soup_is_OK_to_me
Programming Languages Arithmetic Operations
Programming Languages Arithmetic Operations
Programming Languages Statements • A program can be broken down into basic structures called statements. Statements are terminated by a semi-colon. • print “Hello, world!\n”; • Assignment statements use a single ‘=‘ rather than the ‘==‘ of the equality operation. • $pi = 3.1415926; • $area = 0.5 * $pi * $radius * $radius; • $line = <STDIN>;
. . . . . . . . . . . . Programming Languages • Variable Types • Scalar - a single value • Array - a list of values (indexed by sequential number) • Hash - a set of key,value pairs • Prime Numbers = (1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, …) 0 1 1 2 2 3 3 5 First 1 Second 2 Third 3 Fourth 5
0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 Programming Languages Arrays are good when the data is dense, and the algorithm uses a linear access pattern. Prime Numbers = (1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, …) 1 1 1 0 1 0 1 0 0 0 1 1 2 3 5 7 11 13 17 19 23 29 31
0 1 2 3 4 5 6 7 8 9 10 11 Programming Languages • Hash - “associative array” • array indices can be any unique set of “keys” • excellent for accessing in random patterns (in sparse data) • (Ex. “is 19 a prime number?”) 1 2 3 5 7 11 13 17 19 23 29 31 1 2 3 5 7 11 13 17 19 23 29 31 1 1 1 1 1 1 1 1 1 1 1 1
Programming Languages • Scalar -- • $foo, $a1, $a2000 • Array -- • @array, @ii • to access the element at index $i • $array[$i] • the last index of an array is $#array • the number of elements in an array is • $num_elements = $#array + 1; • OR • $num_elements = @array;
Programming Languages • Hash -- • %hash, %env • to access the element with index of $i • $hash{$i} • to get a list of keys used in a hash • @key_list = keys(%hash); • to determine how many keys are in a hash • $num_elements = @key_list; • OR • $num_elements = keys(%hash);
Programming Languages Control of Program Execution if -- executes a block of code, if the condition evaluates to TRUE if($light eq “green”) { continue_driving(); } if( ($light eq “green”) && ($no_traffic) ) { continue_driving(); }
Programming Languages In many cases, a simple if statement is not sufficient, as multiple alternative outcomes need to be evaluated. if($light eq “green”) { continue_driving(); } else { stop_car(); } if($light eq “green”) { continue_driving(); } elsif($light eq “red”) { stop_car(); } else { go_fast_to_beat_the_yellow(); }
Programming Languages • Control of Program Execution • Sometimes you need to iterate through a statement multiple times... • Looping constructs: • for • foreach • while
Programming Languages • Control of Program Execution • Sometimes you need to iterate through a statement multiple times... • Looping constructs: • for (…) { … } • foreach $var (@list) { … } • while (COND) { … }
Programming Languages • Foreach Loop… • foreach $var (@list) { • do_stuff($var); • } • foreach $name (@name_list) { • print “Name = $name\n”; • } • foreach $name (@name_list) { • if($hair_color{$name} eq “blond”) { • print “$name has blond hair.\n”; • } • }
Programming Languages for (INIT; COND; POST) { do_stuff(); } for ($i=0; $i < 50;$i++) { print “i = $i\n”; } for ($i=0; $i < 50; $i++) { if($prime{$i} == 1) { print “$i is prime!\n”; } else { print “$i is not prime.\n”; } }
Programming Languages while (COND) { do_stuff(); } while($line = <FILE_HANDLE>) { print “$line”; } while($flag ==0) { if($prime{$position} == 1) { $flag = 1; } else { $position++; } }