The Shell. EOAS Software Carpentry Workshop. September 20th, 2016

Similar documents
The Unix Shell. Pipes and Filters

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ăț

A Brief Introduction to the Linux Shell for Data Science

CENG 334 Computer Networks. Laboratory I Linux Tutorial

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

Introduction of Linux

Introduction to Linux

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

Lec 1 add-on: Linux Intro

Introduction to Supercomputing

Linux Bootcamp Fall 2015

Linux Shell Script. J. K. Mandal

MBV4410/9410 Fall Bioinformatics for Molecular Biology. More Unix

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

Computer Systems and Architecture

Chapter 4. Unix Tutorial. Unix Shell

Introduction To Linux. Rob Thomas - ACRC

Introduction to Linux Part 1. Anita Orendt and Wim Cardoen Center for High Performance Computing 24 May 2017

UNIX Essentials Featuring Solaris 10 Op System

Essential Linux Shell Commands

Introduction to the UNIX command line

DATA 301 Introduction to Data Analytics Command Line. Dr. Ramon Lawrence University of British Columbia Okanagan

Why learn the Command Line? The command line is the text interface to the computer. DATA 301 Introduction to Data Analytics Command Line

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

The Unix Shell & Shell Scripts

Lab Working with Linux Command Line

Introduction: What is Unix?

Recap From Last Time: Setup Checklist BGGN 213. Todays Menu. Introduction to UNIX.

Introduction To. Barry Grant

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

Computer Systems and Architecture

The Directory Structure

Unix Introduction to UNIX

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

Introduction to Unix: Fundamental Commands

GNU/Linux Course Lesson 1. Puria Nafisi

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.

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

A Brief Introduction to the Command Line. Hautahi Kingi

Advanced Linux Commands & Shell Scripting

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

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

CSCI 2132 Software Development. Lecture 4: Files and Directories

Recap From Last Time:

Introduction to UNIX command-line

BGGN 213 Working with UNIX Barry Grant

Chapter-3. Introduction to Unix: Fundamental Commands

Practical 02. Bash & shell scripting

Files

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

Perl and R Scripting for Biologists

Basic UNIX commands. HORT Lab 2 Instructor: Kranthi Varala

Introduction. File System. Note. Achtung!

Linux environment. Graphical interface X-window + window manager. Text interface terminal + shell

Introduction to Unix and Linux. Workshop 1: Directories and Files

Introduction to Linux. Fundamentals of Computer Science

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

Introduction to UNIX command-line II

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

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 1

CS CS Tutorial 2 2 Winter 2018

Command Line Interface The basics

Mills HPC Tutorial Series. Linux Basics I

Introduction to the Linux Command Line

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

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

COSC UNIX. Textbook. Grading Scheme

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

Introduction to Linux for BlueBEAR. January

Introduction to Linux Workshop 1

Part 1: Basic Commands/U3li3es

Introduction to Linux

Working With Unix. Scott A. Handley* September 15, *Adapted from UNIX introduction material created by Dr. Julian Catchen

Linux Introduction Martin Dahlö Valentin Georgiev

Linux Command Line Primer. By: Scott Marshall

Working with Basic Linux. Daniel Balagué

Introduction to Linux

file:///users/cerdmann/downloads/ harvard-latest.html

5/8/2012. Creating and Changing Directories Chapter 7

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

Shell Programming Systems Skills in C and Unix

Introduction to Linux

Introduction to Linux

Operating Systems. Copyleft 2005, Binnur Kurt

Arkansas High Performance Computing Center at the University of Arkansas

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Introduction to Unix - Lab Exercise 0

You should see something like this, called the prompt :

Files and Directories

User Guide Version 2.0

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

Linux Kung Fu. Ross Ventresca UBNetDef, Fall 2017

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 UNIX Command Line

Unix basics exercise MBV-INFX410

Using Linux as a Virtual Machine

Digital Humanities. Tutorial Regular Expressions. March 10, 2014

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

Transcription:

The Shell EOAS Software Carpentry Workshop September 20th, 2016

Getting Started You need to download some files to follow this lesson. These files are found on the shell lesson website (see etherpad) 1. Make a new folder in your Desktop called shell-novice. 2. Download shell-novice-data.zip and move the file to this folder. 3. If it s not unzipped yet, double-click on it to unzip it. You should end up with a new folder called workshop.

Introduction Learning Goals 1. Explain how the shell relates to the keyboard, the screen, the operating system, and users programs. 2. Explain when and why command-line interfaces should be used instead of graphical interfaces.

Introduction Learning Goals 1. Explain how the shell relates to the keyboard, the screen, the operating system, and users programs. 2. Explain when and why command-line interfaces should be used instead of graphical interfaces. Why use the shell?

Introduction Learning Goals 1. Explain how the shell relates to the keyboard, the screen, the operating system, and users programs. 2. Explain when and why command-line interfaces should be used instead of graphical interfaces. Why use the shell? Connecting to supercomputers

Introduction Learning Goals 1. Explain how the shell relates to the keyboard, the screen, the operating system, and users programs. 2. Explain when and why command-line interfaces should be used instead of graphical interfaces. Why use the shell? Connecting to supercomputers Automate repetitive tasks

Introduction Learning Goals 1. Explain how the shell relates to the keyboard, the screen, the operating system, and users programs. 2. Explain when and why command-line interfaces should be used instead of graphical interfaces. Why use the shell? Connecting to supercomputers Automate repetitive tasks Reproducibility

Files and Directories Learning Goals 1. Explain the similarities and differences between a file and a directory. 2. Translate an absolute path into a relative path and vice versa. 3. Construct absolute and relative paths that identify specific files and directories. 4. Explain the steps in the shell s read-run-print cycle. 5. Identify actual command, flags, and filenames in command-line call. 6. Demonstrate the use of tab completion, and explain its advantages. Sample Code whoami pwd / ls ls -F ls -F Desktop cd Desktop cd.. ls -F -a ls north-pacificgyre/2012-07-03 ls no tab

Exercise If pwd displays /users/backup, and -r tells ls to display things in reverse order, what command will display: pnas sub/ pnas final/ original/ 1. ls pwd 2. ls -r -F 3. ls -r -F /users/backup 4. Either #2 or #3 above, but not #1.

Exercise If pwd displays /users/backup, and -r tells ls to display things in reverse order, what command will display: pnas sub/ pnas final/ original/ 1. ls pwd 2. ls -r -F 3. ls -r -F /users/backup 4. Either #2 or #3 above, but not #1.

Working with Files and Directories Learning Goals 1. Create a directory hierarchy that matches a given diagram. 2. Create files in that hierarchy using an editor or by copying and renaming existing files. 3. Display the contents of a directory using the command line. 4. Delete specified files and/or directories. Sample Code mkdir thesis cd thesis nano draft.txt rm draft.txt rm thesis rmdir thesis rm -r thesis mv thesis/draft.txt thesis/quotes.txt mv thesis/quotes.txt. cp quotes.txt thesis/quotations.txt

Exercise Jamie is working on a project and she sees that her files arent very well organized: $ ls -F analyzed/ fructose.dat raw/ sucrose.dat The fructose.dat and sucrose.dat files contain output from her data analysis. What command(s) could you run so that the commands below will produce the output shown? $ ls analyzed raw $ ls analyzed fructose.dat sucrose.dat

Exercise Jamie is working on a project and she sees that her files arent very well organized: $ ls -F analyzed/ fructose.dat raw/ sucrose.dat The fructose.dat and sucrose.dat files contain output from her data analysis. What command(s) could you run so that the commands below will produce the output shown? $ ls analyzed raw $ ls analyzed fructose.dat sucrose.dat Solution $ mv fructose.dat analyzed/fructose.dat $ mv sucrose.dat analyzed/sucrose.dat

Pipes and Filters Learning Goals 1. Redirect a command s output to a file. 2. Process a file instead of keyboard input using redirection. 3. Construct command pipelines with two or more stages. 4. Explain what usually happens if a program or pipeline isn t given any input to process. 5. Explain Unix s small pieces, loosely joined philosophy. cd molecules wc *.pdb wc -l wc -l *.pdb > lengths.txt cat lengths.txt sort lengths.txt sort lengths.txt > sorted-lengths.txt head -1 sorted-lengths.txt sort lengths.txt head -1 wc -l *.txt wc -l *.txt sort head -5 ls *Z.txt

Exercise In our current directory, we want to find the 3 files which have the least number of lines. Which command listed below would work? 1. wc -l * > sort -n > head -3 2. wc -l * sort -n head 1-3 3. wc -l * head -3 sort -n 4. wc -l * sort -n head -3

Exercise In our current directory, we want to find the 3 files which have the least number of lines. Which command listed below would work? 1. wc -l * > sort -n > head -3 2. wc -l * sort -n head 1-3 3. wc -l * head -3 sort -n 4. wc -l * sort -n head -3

Loops https://xkcd.com/1411/

Loops Write a loop that applies one or more commands separately to each file in a set of files. Trace the values taken on by a loop variable during execution of the loop. Explain the difference between a variable s name and its value. Explain why spaces and some punctuation characters shouldn t be used in file names. Demonstrate how to see what commands have recently been executed. Re-run recently executed commands without retyping them.

Variables in loops Suppose that ls initially displays: fructose.dat glucose.dat sucrose.dat What is the output of: for datafile in *.dat do ls *.dat done

Variables in loops Suppose that ls initially displays: fructose.dat glucose.dat sucrose.dat What is the output of: for datafile in *.dat do ls *.dat done ANSWER: fructose.dat glucose.dat sucrose.dat fructose.dat glucose.dat sucrose.dat fructose.dat glucose.dat sucrose.dat

Saving to a file in a loop In the same directory, what is the effect of this loop? for sugar in *.dat do echo $sugar cat $sugar > xylose.dat done 1. Prints fructose.dat, glucose.dat, and sucrose.dat, and the text from sucrose.dat will be saved to a file called xylose.dat. 2. Prints fructose.dat, glucose.dat, and sucrose.dat, and the text from all three files would be concatenated and saved to a file called xylose.dat. 3. Prints fructose.dat, glucose.dat, sucrose.dat, and xylose.dat, and the text from sucrose.dat will be saved to a file called xylose.dat. 4. None of the above

Saving to a file in a loop In the same directory, what is the effect of this loop? for sugar in *.dat do echo $sugar cat $sugar > xylose.dat done 1. Prints fructose.dat, glucose.dat, and sucrose.dat, and the text from sucrose.dat will be saved to a file called xylose.dat. 2. Prints fructose.dat, glucose.dat, and sucrose.dat, and the text from all three files would be concatenated and saved to a file called xylose.dat. 3. Prints fructose.dat, glucose.dat, sucrose.dat, and xylose.dat, and the text from sucrose.dat will be saved to a file called xylose.dat. 4. None of the above

Scripts 1. Write a shell script that runs a command or series of commands for a fixed set of files. 2. Run a shell script from the command line. 3. Write a shell script that operates on a set of files defined by the user on the command line. 4. Create pipelines that include user-written shell scripts.

Find the longest file with a given extension Write a shell script called longest.sh that takes the name of a directory and a filename extension as its parameters, and prints out the name of the file with the most lines in that directory with that extension. For example: $ bash longest.sh /tmp/data pdb would print the name of the.pdb file in /tmp/data that has the most lines.

Script reading comprehension Joel s data directory contains three files: fructose.dat, glucose.dat, and sucrose.dat. Explain what a script called example.sh would do when run as bash example.sh *.dat if it contained the following lines: # Script 1 echo *.* # Script 2 for filename in $1 $2 $3 do cat $filename done # Script 3 echo $*.dat

Script reading comprehension Joel s data directory contains three files: fructose.dat, glucose.dat, and sucrose.dat. Explain what a script called example.sh would do when run as bash example.sh *.dat if it contained the following lines: # Script 1 echo *.* ANSWER: Prints example.sh fructose.dat glucose.dat sucrose.dat

Script reading comprehension Joel s data directory contains three files: fructose.dat, glucose.dat, and sucrose.dat. Explain what a script called example.sh would do when run as bash example.sh *.dat if it contained the following lines: # Script 2 for filename in $1 $2 $3 do cat $filename done ANSWER: Shows contents of fructose.dat, glucose.dat, and sucrose.dat

Script reading comprehension Joel s data directory contains three files: fructose.dat, glucose.dat, and sucrose.dat. Explain what a script called example.sh would do when run as bash example.sh *.dat if it contained the following lines: # Script 3 echo $*.dat ANSWER: Prints fructose.dat glucose.dat sucrose.dat.dat

Finding things 1. Use grep to select lines from text files that match simple patterns. 2. Use find to find files whose names match simple patterns. 3. Use the output of one command as the command-line parameters to another command.

Directory tree

find pipeline reading comprehension Write a short explanatory comment for the following shell script: find. -name *.dat -print wc -l sort -n

Matching ose.dat but not temp The -v flag to grep inverts pattern matching, so that only lines which do not match the pattern are printed. Given that, which of the following commands will find all files in /data whose names end in ose.dat (e.g., sucrose.dat or maltose.dat), but do not contain the word temp? 1. find /data -name *.dat -print grep ose grep -v temp 2. find /data -name ose.dat -print grep -v temp 3. grep -v "temp" $(find /data -name *ose.dat -print) 4. None of the above.

Matching ose.dat but not temp The -v flag to grep inverts pattern matching, so that only lines which do not match the pattern are printed. Given that, which of the following commands will find all files in /data whose names end in ose.dat (e.g., sucrose.dat or maltose.dat), but do not contain the word temp? 1. find /data -name *.dat -print grep ose grep -v temp 2. find /data -name ose.dat -print grep -v temp 3. grep -v "temp" $(find /data -name *ose.dat -print) 4. None of the above.