1 / 36

Introduction to UNIX and Perl

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.

marisa
Download Presentation

Introduction to UNIX and Perl

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to UNIX and Perl Todd Scheetz UI Summer Bioinformatics Workshop July 16, 2001

  2. 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.

  3. 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

  4. 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)

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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”

  11. 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

  12. 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

  13. pipes What is UNIX? • UNIX also provides a method for stringing multiple programs together • Pipes… • Ex. • head -20 *.pl | grep File | sort

  14. 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

  15. 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

  16. 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 • .

  17. 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/

  18. 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 • }

  19. 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.

  20. 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.

  21. 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

  22. Programming Languages Arithmetic Operations

  23. Programming Languages Arithmetic Operations

  24. 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>;

  25. . . . . . . . . . . . . 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

  26. 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

  27. 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

  28. 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;

  29. 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);

  30. 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(); }

  31. 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(); }

  32. Programming Languages • Control of Program Execution • Sometimes you need to iterate through a statement multiple times... • Looping constructs: • for • foreach • while

  33. Programming Languages • Control of Program Execution • Sometimes you need to iterate through a statement multiple times... • Looping constructs: • for (…) { … } • foreach $var (@list) { … } • while (COND) { … }

  34. 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”; • } • }

  35. 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”; } }

  36. Programming Languages while (COND) { do_stuff(); } while($line = <FILE_HANDLE>) { print “$line”; } while($flag ==0) { if($prime{$position} == 1) { $flag = 1; } else { $position++; } }

More Related