Introduction to Unix & Perl Programming 1

Similar documents
Getting Started With UNIX Lab Exercises

Practical Session 0 Introduction to Linux

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

CENG 334 Computer Networks. Laboratory I Linux Tutorial

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one]

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

CS Fundamentals of Programming II Fall Very Basic UNIX

Unix Tutorial Haverford Astronomy 2014/2015

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

CS 215 Fundamentals of Programming II Spring 2019 Very Basic UNIX

CSC116: Introduction to Computing - Java

Unix/Linux Operating System. Introduction to Computational Statistics STAT 598G, Fall 2011

Introduction to Linux Environment. Yun-Wen Chen

INSE Lab 1 Introduction to UNIX Fall 2017

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

1. The Mac Environment in SIE 1222

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Introduction: What is Unix?

CSC116: Introduction to Computing - Java

Chapter-3. Introduction to Unix: Fundamental Commands

CS 2400 Laboratory Assignment #1: Exercises in Compilation and the UNIX Programming Environment (100 pts.)

Mills HPC Tutorial Series. Linux Basics I

CSCE 212H, Spring 2008, Matthews Lab Assignment 1: Representation of Integers Assigned: January 17 Due: January 22

CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C

Using the Zoo Workstations

CSC111 Computer Science II

1. The Mac Environment in Sierra Hall 1242

CMSC 201 Spring 2017 Lab 01 Hello World

Introduction to Unix: Fundamental Commands

History. Terminology. Opening a Terminal. Introduction to the Unix command line GNOME

Physics REU Unix Tutorial

Some Linux (Unix) Commands that might help you in ENSC351

The Unix Shell & Shell Scripts

Due: February 26, 2014, 7.30 PM

COMS 6100 Class Notes 3

CMSC 201 Spring 2018 Lab 01 Hello World

INTRODUCTION TO BIOINFORMATICS

15-122: Principles of Imperative Computation

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Exploring UNIX: Session 3

Unix basics exercise MBV-INFX410

EE516: Embedded Software Project 1. Setting Up Environment for Projects

9 and 11-Jan CSCI 4152/6509 Natural Language Processing Lab 1: FCS Computing Environment, SVN Tutorial. FCS Computing Environment, SVN Tutorial

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Scripting Languages Course 1. Diana Trandabăț

Getting started with Hugs on Linux

CS CS Tutorial 2 2 Winter 2018

CSC116: Introduction to Computing - Java

Introduction to Linux

Hitchhiker s Guide to VLSI Design with Cadence & Synopsys

Systems Programming and Computer Architecture ( ) Exercise Session 01 Data Lab

Programming Studio #1 ECE 190

last time in cs recitations. computer commands. today s topics.

CST8207: GNU/Linux Operating Systems I Lab Six Linux File System Permissions. Linux File System Permissions (modes) - Part 1

A Brief Introduction to the Linux Shell for Data Science

Laboratory 1 Semester 1 11/12

Computer Systems and Architecture

Lecture 1. A. Sahu and S. V. Rao. Indian Institute of Technology Guwahati

: the User (owner) for this file (your cruzid, when you do it) Position: directory flag. read Group.

Computer Systems and Architecture

UNLV Computer Science Department CS 135 Lab Manual

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Mauro Ceccanti e Alberto Paoluzzi

BIOINFORMATICS POST-DIPLOMA PROGRAM SUBJECT OUTLINE Subject Title: OPERATING SYSTEMS AND PROJECT MANAGEMENT Subject Code: BIF713 Subject Description:

Unix Basics. Systems Programming Concepts

Unix tutorial. Thanks to Michael Wood-Vasey (UPitt) and Beth Willman (Haverford) for providing Unix tutorials on which this is based.

Part 1: Basic Commands/U3li3es

Linux/Cygwin Practice Computer Architecture

2) clear :- It clears the terminal screen. Syntax :- clear

Introduc)on to Unix and Perl programming

Lab 1 Introduction to UNIX and C

UNIX Essentials Featuring Solaris 10 Op System

UNIX Tutorial Two

3. A Periodic Alarm: intdate.c & sigsend.c

Introduction to the Linux Command Line

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CSC209. Software Tools and Systems Programming.

CS 300. Data Structures

THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Esercitazione Introduzione al linguaggio di shell

CSC105, Introduction to Computer Science I. Introduction. Perl Directions NOTE : It is also a good idea to

Tutorial 1: Unix Basics

The Directory Structure

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

Getting started with Hugs on Linux

CSC 112 Lab 1: Introduction to Unix and C++ Fall 2009

Unix Workshop Aug 2014

Introduction to Linux (Part I) BUPT/QMUL 2018/03/14

Connecting to ICS Server, Shell, Vim CS238P Operating Systems fall 18

MTU Computer Structure

LAB #7 Linux Tutorial

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Welcome (back) to CS1007!

Perl and R Scripting for Biologists

Introduction to Unix

Transcription:

Introduction to Unix & Perl Programming 1 Bioinformatics: Issues and Algorithms CSE 308-408 Fall 2007 Lecture 4 Lopresti Fall 2007 Lecture 4-1 -

Administrative notes Homework #1 has been posted on Blackboard and is due on Tuesday, Sept. 11 at 5:00 pm. Submit your work online using the Blackboard Assignment function. CSE Department Distinguished Seminar Series Topic: Architecture of Product Lines Speaker: Dr. David M. Weiss, Avaya Laboratories Location: Packard Lab 466 Date: Thurs., Sept. 6, 4:00 pm 5:00 pm Reception @ 3:30 pm in Packard Lobby CSE Department Ice Cream Social (yum!) Location: Packard Lab 360 Date: Tues., Sept. 11, 4:10 pm 5:00 pm Lopresti Fall 2007 Lecture 4-2 -

Programming environment For CSE 308-408, you may develop the programs for your programming assignments using Perl on any platform. Most Linux installations generally include Perl (see discussion on page 17 of your BBP book). In particular, you can use machines in the CSE Dept. Sun Lab. These are accessible remotely from all over campus (and beyond) using standard secure shell (ssh) client software. See: http://www.lehigh.edu/helpdesk/ssh32/ssh32.shtml Wherever you develop your code, however, at the end of the day, you must make sure it runs on our systems. In addition, I will provide specific naming conventions to facilitate the testing of your programs. Lopresti Fall 2007 Lecture 4-3 -

Secure shell (ssh) client on the Windows desktop Lopresti Fall 2007 Lecture 4-4 -

Remote login procedure for the CSE Dept. Sun Lab Initiate a secure shell (ssh) session to sunlab.cse.lehigh.edu, which provides a connection to a generally random machine among all of the Suns. Since all of the Suns have unique ssh keys, you may find that, after your first connection, your client complains about changed keys on the destination machine. You can either ignore this complaint, or go directly to one of the Suns using its One True Name (as displayed in its login prompt). The full list of Suns is displayed in the login motd on the machine named gateway. Our sys admins have confirmed that all students registered for CSE 308-408 have active accounts. If you have forgotten your password, send email to help@cse.lehigh.edu. Lopresti Fall 2007 Lecture 4-5 -

Logging in Lopresti Fall 2007 Lecture 4-6 -

Basic Unix commands you should know Files ls, more, cp, mv, rm, tail, cat Directories pwd, cd, mkdir, rmdir Processes ps, kill Running Perl perl (or /usr/bin/perl ) Other w, finger, time, spell But the single most important command is man. Typing: % man command will display a manual page for any Unix command (or function). Close behind in importance is apropos. Typing: % apropos keyword will list all Unix commands that mention keyword. Lopresti Fall 2007 Lecture 4-7 -

Typing man man at the Unix shell prompt User Commands man(1) NAME man - find and display reference manual pages SYNOPSIS man [ - ] [ -adflrt ] [ -M path ] [ -T macro-package ] [ -s section ] name... man [ -M path ] -k keyword... man [ -M path ] -f file... DESCRIPTION The man command displays information from the reference manuals. It displays complete manual pages that you select by name, or one-line summaries selected either by keyword (-k), or by the name of an associated file (-f). If no manual page is located, man prints an error message. Lopresti Fall 2007 Lecture 4-8 -

Typing man apropos at the Unix shell prompt User Commands apropos(1) NAME apropos - locate commands by keyword lookup SYNOPSIS apropos keyword... DESCRIPTION The apropos utility displays the man page name, section number, and a short description for each man page whose NAME line contains keyword.... Each word is considered separately and the case of letters is ignored. Words which are part of other words are considered; for example, when looking for `compile', apropos finds all instances of `compiler' also. Lopresti Fall 2007 Lecture 4-9 -

Basic UNIX file commands ls ls -l list contents of current directory generate long listing with more details more file page through file one screen at a time cp file1 file2 mv file1 file2 rm file tail file cat file cat file1 file2 > file3 copy file1 to file2 (i.e., duplicate) move file1 to file2 (i.e., rename) remove file (i.e., delete, erase) display last lines in file dump file to display concatenate file1 and file2 into file3 Unix I/O redirection Lopresti Fall 2007 Lecture 4-10 -

Basic UNIX directory commands pwd print working (current) directory cd dir change directory (down one level) to dir cd.. change directory up one level mkdir dir create a new directory named dir rmdir dir remove directory dir (must be empty first) Lopresti Fall 2007 Lecture 4-11 -

Basic UNIX process commands ps ps -f ps -a ps -A ps -A more prints information about active processes generates full listing with more details prints information about all of your processes prints information about all processes pages through processes one screen at a time Unix pipe kill PID kill -9 PID kills active process with process ID PID kills active process PID with extreme prejudice Lopresti Fall 2007 Lecture 4-12 -

Other basic UNIX commands w finger userid time command spell file list who is currently using machine print details on a specific user report time used to execute command run Unix spell checker on file Wait... aren't we missing one that's kind of important? perl file run Perl interpreter on source code file Lopresti Fall 2007 Lecture 4-13 -

Let's get started writing code... You'll need a way to input and edit your program. Possible choices include vi and emacs. These have some advantages because you can edit directly in a terminal window. vi running in a terminal window The following online tutorials seem reasonable: http://www.eng.hawaii.edu/tutor/vi.html http://www2.lib.uchicago.edu/~keith/tcl-course/emacs-tutorial.html Lopresti Fall 2007 Lecture 4-14 -

Let's get started writing code... A strategy to follow (but you'll develop your own preferences): One terminal window open for editing code...... another terminal window open for running code. Lopresti Fall 2007 Lecture 4-15 -

Let's get started writing code... You can also use an application like NotePad or WordPad running on a PC for editing, but you'll have to upload your Perl code to a Unix machine to run it. * Editing Perl code in Notepad File transfer window in ssh * There are implementations of Perl available for Windows PC's, but remember that ultimately your code will have to run on the CSE Dept. Unix system. Lopresti Fall 2007 Lecture 4-16 -

A simple Perl program print "\n"; \n character sequence tells Perl to display a new line What will this do? Let's save it in a file named welcome and run it... umbria:~/cse308/chapter3% perl welcome umbria:~/cse308/chapter3% Lopresti Fall 2007 Lecture 4-17 -

A less-simple Perl program print "Welcome "; print "to "; print "the "; print "Wonderful "; print "World "; print "of "; print "Bioinformatics!\n"; ; (semicolon) character is statement separator in Perl Let's save it in a file named welcome2 and run it... umbria:~/cse308/chapter3% perl welcome2 umbria:~/cse308/chapter3% Exactly same output as before! Simpler is better, however. Lopresti Fall 2007 Lecture 4-18 -

Checking Perl syntax Everyone makes mistakes from time-to-time. Perl has a way of checking whether or not your program contains certain simple mistakes called syntax errors : print "\n"; umbria:~/cse308/chapter3% perl -c welcome welcome syntax OK umbria:~/cse308/chapter3% -c stands for check Note: correct syntax does not mean no bugs! Lopresti Fall 2007 Lecture 4-19 -

Checking Perl syntax Now consider: pint "\n"; umbria:~/cse308/chapter3% perl -c welcome String found where operator expected at welcome line 1, near "pint "\n"" (Do you need to predeclare pint?) syntax error at welcome line 1, near "pint "Welcome to the Wonderful World of Bioinformatics!\n"" welcome had compilation errors. umbria:~/cse308/chapter3% Useful information, once you learn to decipher it! Lopresti Fall 2007 Lecture 4-20 -

More detailed source code checking Consider program whoops: print ; "\n"; umbria:~/cse308/chapter3% perl whoops umbria:~/cse308/chapter3% umbria:~/cse308/chapter3% perl -c whoops Whoops indeed no output! whoops syntax OK umbria:~/cse308/chapter3% That didn't help... umbria:~/cse308/chapter3% perl -c -w whoops Useless use of a constant in void context at whoops line 1. whoops syntax OK umbria:~/cse308/chapter3% Cryptic, but better! -w asks for warnings. Lopresti Fall 2007 Lecture 4-21 -

More detailed source code checking Consider program whoops: print ; "\n"; umbria:~/cse308/chapter3% perl whoops umbria:~/cse308/chapter3% umbria:~/cse308/chapter3% perl -c whoops whoops syntax OK umbria:~/cse308/chapter3% umbria:~/cse308/chapter3% perl -c -w whoops Useless use of a constant in void context at whoops line 1. whoops syntax OK Really important note: even if your program passes both of these tests, it might still have bugs! It is considered a fundamental impossibility for static testing no matter how rigorous to catch all bugs. Even so, it's a good idea to employ the -c and -w options to check your code. umbria:~/cse308/chapter3% Lopresti Fall 2007 Lecture 4-22 -

Making code self-executing Recall: umbria:~/cse308/chapter3% perl welcome umbria:~/cse308/chapter3% Nicer if we could just type program name call this welcome3: Location of Perl interpreter #! /usr/bin/perl -w print "\n"; umbria:~/cse308/chapter3% chmod u+x welcome3 umbria:~/cse308/chapter3% welcome3 umbria:~/cse308/chapter3% Mark code as executable Lopresti Fall 2007 Lecture 4-23 -

Iteration Iteration is a basic programming construct. We repeat the same sequence of statements over and over until a prespecified condition is either satisfied or violated. while ( some condition is true ) { do something } Presumably something happens here to make condition false eventually Lopresti Fall 2007 Lecture 4-24 -

Iteration Iteration is a basic programming construct. We repeat the same sequence of statements over and over until a prespecified condition is either satisfied or violated. #! /usr/bin/perl -w # The 'forever' program - a (Perl) program, # which does not stop until someone presses Ctrl-C. use constant TRUE => 1; use constant FALSE => 0; Any line starting with # is a comment Constants provide useful English names Executes so long as condition is true while ( TRUE ) { print "\n"; sleep 1; } Program sleeps here for 1 second Lopresti Fall 2007 Lecture 4-25 -

Getting caught in a loop Save previous program as forever, then: umbria:~/cse308/chapter3% chmod u+x forever umbria:~/cse308/chapter3% forever ^C umbria:~/cse308/chapter3% Type Ctrl-C (^C) to kill process and end infinte loop Lopresti Fall 2007 Lecture 4-26 -

Variables Variables are containers whose contents (values) can change over lifetime of a program. In Perl, simpliest variable is a scalar, which holds single value. #! /usr/bin/perl -w Variable $count set equal to 0 # The 'tentimes' program - a (Perl) program, # which stops after ten iterations. use constant HOWMANY => 10; $count = 0; False when $count equals HOWMANY while ( $count < HOWMANY ) { print "\n"; $count++; } Variable $count incremented Lopresti Fall 2007 Lecture 4-27 -

A well-behaved loop Save previous program as tentimes, then: umbria:~/cse308/chapter3% chmod u+x tentimes umbria:~/cse308/chapter3% tentimes umbria:~/cse308/chapter3% Lopresti Fall 2007 Lecture 4-28 -

Selection Selection is another basic programming construct that allows program to take one path or another depending on condition. if ( some condition is true ) { do something } else { do something else } The else clause is optional Lopresti Fall 2007 Lecture 4-29 -

A loop with selection #! /usr/bin/perl -w # The 'fivetimes' program - a (Perl) program, # which stops after five iterations. use constant TRUE => 1; use constant FALSE => 0; use constant HOWMANY => 5; $count = 0; Test for equality: note == and not =! while ( TRUE ) { $count++; print "\n"; if ( $count == HOWMANY ) { last; } Terminates current loop immediately } Lopresti Fall 2007 Lecture 4-30 -

A loop with selection Save previous program as fivetimes, then: umbria:~/cse308/chapter3% chmod u+x fivetimes umbria:~/cse308/chapter3% fivetimes umbria:~/cse308/chapter3% Which style of loop is better, tentimes or fivetimes? Your BBP textbook offers the helpful maxim: There's more than one way to do it. Lopresti Fall 2007 Lecture 4-31 -

Checking parity of an integer ('odd' vs. 'even') #! /usr/bin/perl -w use constant HOWMANY => 4; $count = 0; while ( $count < HOWMANY ) { $count++; if ( $count == 1 ) { print "odd\n"; } elsif ( $count == 2 ) { print "even\n"; } elsif ( $count == 3 ) { print "odd\n"; } else { print "even\n"; } } # at this point $count is four. While this works, it seems kind of awkward and not very general. (What happens if we want to do this for a large number of values?) Lopresti Fall 2007 Lecture 4-32 -

More than one way to do it... While it's true there's more than one way to do it, some ways are definitely better than other ways. Case in point: #! /usr/bin/perl -w # The 'terrible' program - a poorly formatted 'oddeven'. use constant HOWMANY => 4; $count = 0; while ( $count < HOWMANY ) { $count++; if ( $count == 1) { print "odd\n"; } elsif ( $count == 2 ) { print "even\n"; } elsif ( $count == 3 ) { print "odd\n"; } else # at this point $count is four. { print "even\n"; } } This program works, but it is very hard to understand. metis:~/cse308/chapter3% terrible odd even odd even metis:~/cse308/chapter3% Moral: make your programs easy to read by using whitespace and good formatting style. Lopresti Fall 2007 Lecture 4-33 -

A better parity checking program... Both oddeven and terrible reflect poor style. This is better: #! /usr/bin/perl -w use constant HOWMANY => 4; $count = 0; while ( $count < HOWMANY ) { $count++; if ( $count % 2 == 0) { print "even\n"; } } else { } # count % 2 is not zero. print "odd\n"; Moral: in general, aim for generality. :-) Modulo arithmetic operator (%) returns remainder of division. When divisor is 2, remainder is 0 if integer is even and 1 if odd. Lopresti Fall 2007 Lecture 4-34 -

A (slightly) better parity checking program... Even better, perhaps, because it is more succinct: #! /usr/bin/perl -w # The 'oddeven3' program - yet another version of 'oddeven'. use constant HOWMANY => 4; $count = 0; while ( $count < HOWMANY ) { $count++; print "even\n" if ( $count % 2 == 0 ); print "odd\n" if ( $count % 2!= 0 ); } This form of conditional statement is rarely found in other languages, but is very handy! (Perl calls it a statement qualifier. ) Lopresti Fall 2007 Lecture 4-35 -

Processing input We can't do much unless we can get data into our programs. #! /usr/bin/perl -w # The 'getlines' program which processes lines. while ( $line = <> ) { print $line; } metis:~/cse308/chapter3% getlines Hello, Lehigh! Hello, Lehigh! Are we having fun yet? Are we having fun yet? Hey, stop doing that!!! Hey, stop doing that!!! metis:~/cse308/chapter3% What I typed on keyboard Output from program Type Ctrl-D (^D) for end-of-file Lopresti Fall 2007 Lecture 4-36 -

Taking input from a file Under Unix, input can easily come from file or from keyboard: #! /usr/bin/perl -w while ( $line = <> ) { print $line; } Directs getlines to take its input from the file terrible metis:~/cse308/chapter3% getlines terrible #! /usr/bin/perl -w # The 'terrible' program - a poorly formatted 'oddeven'. use constant HOWMANY => 4; $count = 0; while ( $count < HOWMANY ) { $count++; if ( $count == 1) { print "odd\n"; } elsif ( $count == 2 ) { print "even\n"; } elsif ( $count == 3 ) { print "odd\n"; } else # at this point $count is four. { print "even\n"; } } metis:~/cse308/chapter3% Lopresti Fall 2007 Lecture 4-37 -

Pattern matching As noted earlier, pattern matching is a very important feature of Perl for bioinformatics applications. We'll consider this in more detail later, but for now, let's look at a simple example. #! /usr/bin/perl -w # The 'patterns' program - introducing regular expressions. while ( $line = <> ) { print $line if $line =~ /even/; } =~ is known as the binding operator in Perl Print the contents of the scalar variable $line if and only if it contains the pattern 'even' Lopresti Fall 2007 Lecture 4-38 -

Pattern matching #! /usr/bin/perl -w while ( $line = <> ) { print $line if $line =~ /even/; } metis:~/cse308/chapter3% patterns terrible # The 'terrible' program - a poorly formatted 'oddeven'. { print "even\n"; } elsif ( $count == 3 ) { print "odd\n"; } { print "even\n"; } } metis:~/cse308/chapter3% Note: patterns tell perl what to look for, not how to do it. Only lines containing the string even get printed metis:~/cse308/chapter3% patterns oddeven # The 'oddeven' program - a (Perl) program, # is an odd number, and 'even' when $count is an even print "even\n"; print "even\n"; metis:~/cse308/chapter3% Lopresti Fall 2007 Lecture 4-39 -

Maxims from BBP Chapter 3 Your book provides short summaries of key points ( maxims ): Programs execute in sequential order. Less is better (in general). There's more than one way to do it. Add comments to facilitate future code maintenance. Use human-friendly names for constants and variables. A condition results in a true or false value. For readability, use plenty of whitespace and good formatting. Patterns tell perl what to look for, not how to do it. Lopresti Fall 2007 Lecture 4-40 -

Wrap-up Readings for next time: BB&P Chapters 4-5 (more Perl programming). Remember: Come to class having done the readings. Check Blackboard regularly for updates. If enrolled in CSE 408, let me know which lecture topic you wish to scribe by Friday, Sept. 7. (Send me several choices.) Lopresti Fall 2007 Lecture 4-41 -