Lab #2 Physics 91SI Spring 2013

Similar documents
Introduction: What is Unix?

Introduction To. Barry Grant

Review of Fundamentals

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

Lab 4: Shell Scripting

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

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]

CS 3410 Intro to Unix, shell commands, etc... (slides from Hussam Abu-Libdeh and David Slater)

Submitting your Work using GIT

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

Unix. Examples: OS X and Ubuntu

STATS Data Analysis using Python. Lecture 15: Advanced Command Line

Linux Command Line Interface. December 27, 2017

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Lab 4: Bash Scripting

Table Of Contents. 1. Zoo Information a. Logging in b. Transferring files 2. Unix Basics 3. Homework Commands

Lab 1 Introduction to UNIX and C

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

A Brief Introduction to the Linux Shell for Data Science

Exercise 1: Basic Tools

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

COMP2100/2500 Lecture 17: Shell Programming II

1 Installation (briefly)

CSC209. Software Tools and Systems Programming.

Introduction to remote command line Linux. Research Computing Team University of Birmingham

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

Introduction to UNIX. SURF Research Boot Camp April Jeroen Engelberts Consultant Supercomputing

Hg Mercurial Cheat Sheet

Introduction to the shell Part II

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Lab 2: Linux/Unix shell

Bioinformatics? Reads, assembly, annotation, comparative genomics and a bit of phylogeny.

Introduction in Unix. Linus Torvalds Ken Thompson & Dennis Ritchie

How to Work with Mercurial

Open up a terminal, make sure you are in your home directory, and run the command.

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

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

Introduction to Linux for BlueBEAR. January

CSE Linux VM. For Microsoft Windows. Based on opensuse Leap 42.2

Cloud Computing and Unix: An Introduction. Dr. Sophie Shaw University of Aberdeen, UK

Introduction to UNIX. Introduction EECS l UNIX is an operating system (OS). l Our goals:

Chapter-3. Introduction to Unix: Fundamental Commands

LAB #5 Intro to Linux and Python on ENGR

Cloud Computing and Unix: An Introduction. Dr. Sophie Shaw University of Aberdeen, UK

Recap From Last Time:

27-Sep CSCI 2132 Software Development Lab 4: Exploring bash and C Compilation. Faculty of Computer Science, Dalhousie University

Unix/Linux Primer. Taras V. Pogorelov and Mike Hallock School of Chemical Sciences, University of Illinois

BGGN 213 Working with UNIX Barry Grant

Unix basics exercise MBV-INFX410

Version control with git and Rstudio. Remko Duursma

Introduction to Linux

COMS 6100 Class Notes 3

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

CENG 334 Computer Networks. Laboratory I Linux Tutorial

213/513/613 Linux/Git Bootcamp. Cyrus, Eugene, Minji, Niko

Linux at the Command Line Don Johnson of BU IS&T

Using GitHub to Share with SparkFun a

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Advanced Linux Commands & Shell Scripting

An Introduction to Cluster Computing Using Newton

Introduction to the UNIX command line

Version Control with Git

UNIX Essentials Featuring Solaris 10 Op System

Section 2: Developer tools and you. Alex Mariakakis (staff-wide)

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

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

CSCI 2132 Software Development. Lecture 4: Files and Directories

unix intro Documentation

LAB 8 (Aug 4/5) Unix Utilities

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

Creating a Shell or Command Interperter Program CSCI411 Lab

About CVS. 1 Version Control - what is it? why is it useful?

Unix Tools / Command Line

Practical Unix exercise MBV INFX410

A quick (and maybe practical) guide to Git and version control. By Jay Johnson

Lesson 9 Transcript: Backup and Recovery

Shell Scripting. Jeremy Sanders. October 2011

ITST Searching, Extracting & Archiving Data

B a s h s c r i p t i n g

An introduction to Linux Part 4

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

CSCI 2132 Software Development. Lecture 5: File Permissions

Traditional Access Permissions

Lec 1 add-on: Linux Intro

Exploring UNIX: Session 3

Reading and manipulating files

find starting-directory -name filename -user username

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Linux shell scripting Getting started *

Linux Command Line Primer. By: Scott Marshall

LAB 0: LINUX COMMAND LINE AND SVN

Linux and Git Boot Camp

Portions adapted from A Visual Guide to Version Control. Introduction to CVS

Mills HPC Tutorial Series. Linux Basics I

CSC209. Software Tools and Systems Programming.

The Unix Shell. Pipes and Filters

CSC UNIX System, Spring 2015

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

Unix Shells and Other Basic Concepts

Utilities. September 8, 2015

Transcription:

Lab #2 Physics 91SI Spring 2013 Objective: Some more experience with advanced UNIX concepts, such as redirecting and piping. You will also explore the usefulness of Mercurial version control and how to write short programs using UNIX commands in the form of shell scripts. As will become usual for the labs, you will ssh into a Corn machine, where you will do these exercises. You do not need X11-forwarding (ssh -X), but it can be nice to have a GUI text editor, and it can t hurt if you have it. Please cd to your Physics 91 SI directory, but do not create a lab2 subdirectory yet - see below. Part 0: Getting Started There s one bit of administrivia that needs to be done before using Mercurial. Using a text editor (gedit or nano, or your choice), open the file ~/.hgrc (note the similarity to the ~/.cshrc file from Lab 1). Paste the following text (replacing with your own username), and save the file: [ui] editor = gedit username = John Doe <spamandeggs@stanford.edu> merge = emacs [merge-tools] emacs.args = -q --eval "(ediff-merge-with-ancestor \"$local\" \"$other\" \"$base\" nil \"$output\")" Also, if you didn t get to the Aliases and Links section of Lab 1, we suggest that you follow these instructions to create environment variables for the course directory and your working directory. First, open, ~/.cshrc and find the section titled Environmental Variables (In gedit, just use Ctrl-f). Add the following lines (making sure the si in the first line is lowercase): setenv SICDIR "/afs/ir.stanford.edu/class/physics91si" setenv SI "~/physics91si" Save the file, and when you next log in you ll be able to access the course directory and your working directory using these environment variables (i.e. cd $SICDIR or ls $SI/lab1/). Part 1: Using Mercurial

survey.py cd into your Physics 91 SI directory; you re going to create your lab2 directory by cloning the Mercurial repository located in /afs/ir/class/physics91si/src/lab2. Do this by running the command hg clone /afs/ir/class/physics91si/src/lab2 lab2 Or, if you created an environment variable last class, you can do something like hg clone $SICDIR/src/lab2 lab2. Go into lab2 and run ls and hg log to see what files were saved in the directory and when they were added/edited. You can also use hg status to see which files Mercurial is tracking. The first file you re going to interact with is survey.py, a little Python program to ask you some quick survey questions. Go ahead a run it by typing python survey.py and answer its questions. Now, from the hg log, figure out what changeset you re in right now and which ones survey.py was added/edited in. How can you figure out what edits were made previously? You could revert to each version and look at the file to try to find the difference, but Mercurial has a nice command, hg diff, that makes this a lot easier. To find out how to use it, use Mercurial s own help system: hg help <command> After you ve figured out what each of the two edits were, revert back to the original survey.py and see if you can get away with lying to the survey. (This does involve understanding some Python code, but I promise, the code that matters should be pretty straightforward to figure out). You should also try creating a file with the contents no <enter> yes (2 lines) and save it (say as answers.txt), and run python survey.py < answers.txt as an example of redirecting a file to stdin. define_ions.py Now we re going to play with define_ions.py. Go ahead and run it, note that it takes 5 numerical arguments, so type something along the lines of python define_ions.py 5 4 6 7 8

and go ahead and look at the error message that gets printed out. Oh no, somebody gave you a program that doesn t work (or maybe you were editing something a while ago and accidentally did something and now it doesn t work anymore)! But they re sure that it was working before; perhaps a working version was saved in one of the older changesets, before the accident. Find a way to get back to the working version, and also find a way to search for the accidental change that broke the program. (Hint: you want to use hg update here. Type hg help update for more information) Think about when it s okay to just revert back to an old version and commit and when it s better to search for the accidental change, fix it, and then commit. Maybe their last edit included some important changes, on top of the one accidental change? (doesn t matter for this example, but important to remember for your own use) The hg diff command may be useful here. Go back to the tip, fix the mistake, and use hg commit to save a new snapshot of the files, and include a helpful commit message, briefly explaining what happened and what you did. Run the program again now and see what happens, but this time run it with these arguments: python define_ions.py 8 5 9 3 1000 Wow! That s a lot of text! You can t even scroll up to the top in your terminal! If only there was a way to redirect all of that output to a file that you can just open up in a text editor to view...try that out. (Alternatively, try piping the output straight to less) Once you ve done that, go ahead and add the output file to your Mercurial repository and make another commit (remember to give a concise, useful commit message!) Part 2: Piping and Command Substitution So let s start off with a simple example for you to see right on your own screen. Try ls grep.py and ls grep.py -v (the -v in the second one tells grep to exclude all of the lines that match the pattern). You can also try ls grep.pdb and then run rm `ls grep.pdb` (careful! those are backquote characters - the key should be near Esc), which will remove all of the files that match the pattern.pdb. Now you re going to work through a more complicated example, stringing together a few different commands to eventually find the name and size of the largest file in your home (~) directory, so cd over there. Start off with du -as (remember, UNIX is case-sensitive!), which will print out the name and size of all of the files in your home directory and its sub-directories in a way that you ll want (if you want, go ahead and check out the man page for du to learn more about the -a and -S options).

Now, using pipes ( ), use the following commands to parse the output of du -as: sort -nr cut -f2 head -5 Add them one at a time to build up a chain, and see how the output changes. What does each of these commands do? If you have time, check the man pages for each to see what each option flag does. Now add the tail command to your string of piped commands and fiddle around with the options given to head and tail so that your output is just the location of the largest file (not directory, files (usually) have a suffix like.doc). Your output might also contain an error message talking about not having access to something. That error message is annoying, so run your long command again, but this time add a redirect to a file to the end of it. View the output file and notice how the error message wasn t redirected to the file, just the location of your largest file. This is an example of the difference between stdout and stderr; they may both print to the terminal under normal conditions, but when you use a redirect, it by default only redirects stdout to the file. Now, for the finishing touch, try du -h `cat output.txt`(again, these are backquotes!) to see the size of your largest file in a human readable format. Part 3: Shell Scripting Challenge We hinted at this with the Challenge in Lab #1, and now you ll get a chance to do something a little more useful. Shell scripting is best for when the commands that you need to type out are long and complicated OR they re something you ll do a lot and don t want to write every time. So let s work on an example that serves both: submitting your lab directories when you re done. Write a shell script, called submit.sh, that will run hg status, an echo command to remind you to check that you ve got everything in the repository, hg commit (don t use -m option), and then, using whoami, pwd, along with echo and cut, create a file that will contain, on a single line, the full path of your submission directory. Then add a line that uses hg push DEST (where DEST is the directory name in your file) to submit your work. Finally, add lines that use rm to delete any temporary files you created, so that the submission process doesn t leave any side effects. Your submission directory has the format /afs/ir/class/physics91si/submissions/jchaves/lab2 NOTE: You will need to use redirection to eventually create a file with the above address, but you will need to know the difference between > and >>. Also, there will be some annoyances

with newlines when redirecting to this file, so look into echo -n and tr -d \n < FILE > newfile (this latter command makes a copy of a file, stripping out newline characters).