More advanced BASH usage

Slides:



Advertisements
Similar presentations
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
Advertisements

CIS 118 – Intro to UNIX Shells 1. 2 What is a shell? Bourne shell – Developed by Steve Bourne at AT&T Korn shell – Developed by David Korn at AT&T C-shell.
EMT 2390L Lecture 4 Dr. Reyes Reference: The Linux Command Line, W.E. Shotts.
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
A Guide to Unix Using Linux Fourth Edition
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin
Linux+ Guide to Linux Certification, Second Edition
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Guide To UNIX Using Linux Third Edition
Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.
Introduction to Unix – CS 21 Lecture 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
Introduction to Linux and Shell Scripting Jacob Chan.
Introduction to UNIX/Linux Exercises Dan Stanzione.
Sydney Opera House. Week Three Agenda Administrative Issues Link of the week Review week two lab assignment This week’s expected outcomes Next lab assignment.
Advanced File Processing
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0.
Advanced UNIX Shell Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 8 Shell.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
Significance of Scripting Languages for Operating System Administration Vladimir Mateljan Željka Požgaj Krunoslav Peter INFuture2007.
Linux+ Guide to Linux Certification, Third Edition
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Quiz 15 minutes Open note, open book, open computer Finding the answer – working to get it – is what helps you learn I don’t care how you find the answer,
Pipes and Filters Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
40 Years and Still Rocking the Terminal!
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Linux+ Guide to Linux Certification, Second Edition
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Jessica Miller & Ruth Anderson
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Linux Administration Working with the BASH Shell.
Exploring Shell Commands, Streams, and Redirection
Linux 201 Training Module Linux Adv File Mgmt.
Lesson 5-Exploring Utilities
Sydney Opera House.
The Linux Operating System
stdin, stdout, stderr Redirection
Exploring Shell Commands, Streams, and Redirection
CSE 303 Concepts and Tools for Software Development
INTRODUCTION TO UNIX: The Shell Command Interface
Exploring Shell Commands, Streams, and Redirection
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
The Linux Command Line Chapter 6
Guide To UNIX Using Linux Third Edition
Tutorial of Unix Command & shell scriptS 5027
Exploring Shell Commands, Streams, and Redirection
Chapter Four UNIX File Processing.
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
CST8177 Scripting 2: What?.
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Presentation transcript:

More advanced BASH usage

What we know already How to run existing programs Those which are in the PATH Those which are somewhere else How to modify the options for a program using switches How to supply data to programs using file paths and wildcards

What else can we do Record the output of programs Check for errors in programs which are running Link programs together into small pipelines Automate the running of programs over batches of files All of these are possible with some simple BASH scripting

Recording the output of programs One of the aspects of POSIX is a standard system for sending data to and from programs. Three data streams exist for all Linux programs (though they don't have to use them all) STDIN (Standard Input - a way to send data into the program) STDOUT (Standard Output - a way to send expected data out of the program) STDERR (Standard Error - a way to send errors or warnings out of the program) By default STDOUT and STDERR are connected to your shell, so when you see text coming from a program, it's coming from these streams.

Recording the output of programs STDIN STDOUT STDERR Rather than leaving these streams connected to the screen, you can link them to either files, or other programs to either create logs, or to build small pipelines

Redirecting standard streams You can redirect using arrows at the end of your command > [file] Redirects STDOUT < [file] Redirects STDIN 2> [file] Redirects STDERR 2>&1 Sends STDERR to STDOUT so you only have one output stream $ find . -print > file_list.txt 2> errors.txt $ ls Data Desktop Documents Downloads errors.txt examples.desktop file_list.txt Music Pictures Public Templates Videos $ head file_list.txt . ./Downloads ./Pictures ./Public ./Music ./.bash_logout ./.local ./.local/share ./.local/share/icc ./.local/share/icc/edid-33d524c378824a7b78c6c679234da6b1.icc

Linking programs together Part of the original UNIX design was to have lots of small programs doing specific jobs, and then to link them together to perform more advanced tasks. There are a couple of mechanisms to do this Pipes Backticks

Linking programs together using pipes Pipes are a mechanism to connect the STDOUT of one program to the STDIN of another. You can use them to build small pipelines To create a pipe just use a pipe character | between programs pr1 | pr2 is the equivalent to pr1 > temp; pr2 < temp $ ls | head -2 Data Desktop

Useful programs for pipes Whilst you can theoretically use pipes to link any programs, there are some which are particularly useful, these are things like: wc - to do word and line counting grep - to do pattern searching sort - to sort things uniq - to deduplicate things less - to read large amounts of output zcat/gunzip/gzip - to do decompression or compression

Small example pipeline Take a compressed fastq sequence file, extract from it all of the entries containing the telomere repeat sequence (TTAGGG) and count them zcat file.fq.gz | grep TTAGGGTTAGGG | wc -l $ zcat file.fq.gz | wc -l 179536960 $ zcat file.fq.gz | grep TTAGGGTTAGGG | wc -l 3925

Using backticks to capture output Pipes are great to link streams of data together, but they need to be supported by both programs In some cases you want to use the output of one program as an argument to another one. You can do this using backticks - these are like quotes, but their contents are executed, and the contents are replaced with the STDOUT of the executed code

Backticks example I want to find out where a program (fastqc) is installed, and I want to find out whether that location is a real file, or a symlink to somewhere else. I can do this in 2 separate steps, but this involves copy/paste which fastqc ls -l [whatever came out of step 1] Backticks let you do this in a single step. $ ls -l `which fastqc` -rwxr-xr-x 1 andrewss bioinf 14400 Dec 21 2017 /bi/apps/fastqc/0.11.7/fastqc

Iterating over files When processing data it is common to need to re-run the same command multiple times for different input/output files. Some programs will support being provided with multiple input files, but many will not. You can use the automation features of the BASH shell to automate the running of these types of programs

The BASH for loop Simple looping construct Loop over a set of files Loop over a set of values Creates a temporary environment variable which you can use when creating commands

Examples of for loops for file in *txt do echo $file grep .sam $file | wc -l done for value in {5,10,20,50} do run_simulation --iterations=$value > ${value}_iterations.log 2>&1 done for value in {10..100} do run_simulation --iterations=$value > ${value}_iterations.log 2>&1 done