Unix Shell. Advanced Shell Tricks

Similar documents
The Unix Shell. Pipes and Filters

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

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

5/20/2007. Touring Essential Programs

CSE 15L Winter Midterm :) Review

The Unix Shell. Permissions

ITST Searching, Extracting & Archiving Data

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

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

Introduction: What is Unix?

Module 8 Pipes, Redirection and REGEX

CSC 2500: Unix Lab Fall 2016

find starting-directory -name filename -user username

Reading and manipulating files

Lec 1 add-on: Linux Intro

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

A shell can be used in one of two ways:

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

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

CS 25200: Systems Programming. Lecture 11: *nix Commands and Shell Internals

Introduction To. Barry Grant

Bash Shell Programming Helps

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:


Essentials for Scientific Computing: Bash Shell Scripting Day 3

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

BGGN 213 Working with UNIX Barry Grant

Chapter 1 - Introduction. September 8, 2016

LING 408/508: Computational Techniques for Linguists. Lecture 5

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


Topic 2: More Shell Skills

6.033 Computer System Engineering

DAVE LIDDAMENT INTRODUCTION TO BASH

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

Useful Unix Commands Cheat Sheet

Introduction to the shell Part II

Introduction to Unix: Fundamental Commands

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

Review of Fundamentals

Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More

Assignment 1: Communicating with Programs

Recap From Last Time:

Unzip command in unix

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

Chapter-3. Introduction to Unix: Fundamental Commands

UNIX System Programming Lecture 3: BASH Programming

Mills HPC Tutorial Series. Linux Basics II

CSE 303 Lecture 4. users/groups; permissions; intro to shell scripting. read Linux Pocket Guide pp , 25-27, 61-65, , 176

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

bash Scripting Introduction COMP2101 Winter 2019

Essential Unix and Linux! Perl for Bioinformatics, ! F. Pineda

UNIX Essentials Featuring Solaris 10 Op System

Week Overview. Simple filter commands: head, tail, cut, sort, tr, wc grep utility stdin, stdout, stderr Redirection and piping /dev/null file

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

CENG 334 Computer Networks. Laboratory I Linux Tutorial

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

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

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

PROGRAMMAZIONE I A.A. 2015/2016

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

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Exploring UNIX: Session 3

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

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

Processes and Shells

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

Introduction to Linux

Linux Essentials. Programming and Data Structures Lab M Tech CS First Year, First Semester

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

BASH and command line utilities Variables Conditional Commands Loop Commands BASH scripts

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

Linux Shell Script. J. K. Mandal

Instructions for Installing H5P on Moodle. First, Check & See if the H5P plugin is already installed

Linux Command Line Interface. December 27, 2017

Advanced Linux Commands & Shell Scripting

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

Introduction to UNIX Shell Exercises

Chapter 9. Shell and Kernel

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

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

Introduction to Linux Organizing Files

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Basics. I think that the later is better.

Practical 02. Bash & shell scripting

Assignment clarifications

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

Scripting. More Shell Scripts Loops. Adapted from Practical Unix and Programming Hunter College

Chapter 4. Unix Tutorial. Unix Shell

Command-line interpreters

Lab #2 Physics 91SI Spring 2013

The Unix Shell. Job Control

Shells & Shell Programming (Part B)

9.2 Linux Essentials Exam Objectives

CSCI 2132 Software Development. Lecture 5: File Permissions

Computer Systems and Architecture

A Brief Introduction to the Linux Shell for Data Science

Computer Systems and Architecture

Transcription:

The Advanced Shell Tricks Copyright The University of Southampton 2011 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.

How should I do this? Some technical problem

In previous episodes, we ve seen how to: Combine existing programs using pipes & filters $ wc l *.pdb sort head -1

In previous episodes, we ve seen how to: Combine existing programs using pipes & filters Redirect output from programs to files $ wc l *.pdb > lengths

In previous episodes, we ve seen how to: Combine existing programs using pipes & filters Redirect output from programs to files Use variables to control program operation $ SECRET_IDENTITY=Dracula $ echo $SECRET_IDENTITY Dracula

In previous episodes, we ve seen how to: Combine existing programs using pipes & filters Redirect output from programs to files Use variables to control program operation Very powerful when used together

In previous episodes, we ve seen how to: Combine existing programs using pipes & filters Redirect output from programs to files Use variables to control program operation Very powerful when used together But there are other useful things we can do with these let s take a look

First, let s revisit redirection data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb

First, let s revisit redirection $ ls *.pdb > files list all pdb files redirect to a file data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb

First, let s revisit redirection $ ls *.pdb > files list all pdb files redirect to a file The redirection operator data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb

First, let s revisit redirection $ ls *.pdb > files list all pdb files redirect to a file But what about adding this together with other results generated later? data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb butane.ent heptane.ent hexane.ent nonane.ent decane.ent

First, let s revisit redirection $ ls *.pdb > files But what about adding this together with other results generated later? $ ls *.ent > more-files list all pdb files redirect to a file data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb butane.ent heptane.ent hexane.ent nonane.ent decane.ent

First, let s revisit redirection $ ls *.pdb > files But what about adding this together with other results generated later? $ ls *.ent > more-files We just want the ent files list all pdb files redirect to a file data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb butane.ent heptane.ent hexane.ent nonane.ent decane.ent

First, let s revisit redirection $ ls *.pdb > files list all pdb files redirect to a file But what about adding this together with other results generated later? $ ls *.ent > more-files $ cat files more-files > all-files append files into a single new file data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb butane.ent heptane.ent hexane.ent nonane.ent decane.ent

First, let s revisit redirection $ ls *.pdb > files But what about adding this together with other results generated later? $ ls *.ent > more-files $ cat files more-files > all-files Instead, we can do $ ls *.ent >> files list all pdb files redirect to a file append files into a single new file data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb butane.ent heptane.ent hexane.ent nonane.ent decane.ent

First, let s revisit redirection $ ls *.pdb > files But what about adding this together with other results generated later? $ ls *.ent > more-files $ cat files more-files > all-files Instead, we can do $ ls *.ent >> files Note the double > s the append operator list all pdb files redirect to a file append files into a single new file data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb butane.ent heptane.ent hexane.ent nonane.ent decane.ent

We know that Normally, standard output is directed to a display:

We know that Normally, standard output is directed to a display: shell ls

We know that Normally, standard output is directed to a display: But we have redirected it to a file instead: shell ls files

But what happens with error messages?

But what happens with error messages? For example $ ls /some/nonexistent/path > files ls: /some/nonexistent/path: No such file or directory

But what happens with error messages? For example $ ls /some/nonexistent/path > files ls: /some/nonexistent/path: No such file or directory No files are listed in files, as you might expect.

But what happens with error messages? For example $ ls /some/nonexistent/path > files ls: /some/nonexistent/path: No such file or directory No files are listed in files, as you might expect. But why isn t the error message in files?

This is because error messages are sent to the standard error (stderr), separate to stdout

This is because error messages are sent to the standard error (stderr), separate to stdout So what was happening with the previous example? shell ls files

This is because error messages are sent to the standard error (stderr), separate to stdout So what was happening with the previous example? shell stderr stdout ls stderr stdout files

This is because error messages are sent to the standard error (stderr), separate to stdout So what was happening with the previous example? shell stderr stdout ls stderr stdout files

We can capture standard error as well as standard output

We can capture standard error as well as standard output To redirect the standard error to a file, we can do: $ ls /some/nonexistent/path 2> error-log Redirect as before, but with a slightly different operator

We can capture standard error as well as standard output To redirect the standard error to a file, we can do: $ ls /some/nonexistent/path 2> error-log Now we have any error messages stored in error-log

We can capture standard error as well as standard output To redirect the standard error to a file, we can do: $ ls /some/nonexistent/path 2> error-log Now we have any error messages stored in error-log To redirect both stdout and stderr, we can then do: $ ls /usr /some/nonexistent/path > files 2> error-log

We can capture standard error as well as standard output To redirect the standard error to a file, we can do: $ ls /some/nonexistent/path 2> error-log Now we have any error messages stored in error-log To redirect both stdout and stderr, we can then do: $ ls /usr /some/nonexistent/path > files 2> error-log We can use both stdout and stderr redirection at the same time

We can capture standard error as well as standard output To redirect the standard error to a file, we can do: $ ls /some/nonexistent/path 2> error-log Now we have any error messages stored in error-log To redirect both stdout and stderr, we can then do: $ ls /usr /some/nonexistent/path > files 2> error-log Which would give us contents of /usr in files as well.

So why a 2 before the >?

So why a 2 before the >? Both stdout and stderr can be referenced by numbers: $ ls /usr /some/nonexistent/path 1> files 2> error-log

So why a 2 before the >? Both stdout and stderr can be referenced by numbers: $ ls /usr /some/nonexistent/path 1> files 2> error-log Refers to stdout Refers to stderr

So why a 2 before the >? Both stdout and stderr can be referenced by numbers: $ ls /usr /some/nonexistent/path 1> files 2> error-log To just redirect both to the same file we can also do: $ ls /usr /some/nonexistent/path &> everything With & denoting both stdout and stderr

So why a 2 before the >? Both stdout and stderr can be referenced by numbers: $ ls /usr /some/nonexistent/path 1> files 2> error-log To just redirect both to the same file we can also do: $ ls /usr /some/nonexistent/path &> everything With & denoting both stdout and stderr We can also use append for each of these too: $ ls /usr /some/nonexistent/path 1>> files 2>> error-log

> 1> Redirect stdout to a file 2> Redirect stderr to a file &> Redirect both stdout and stderr to the same file

> 1> Redirect stdout to a file 2> Redirect stderr to a file &> Redirect both stdout and stderr to the same file >> 1>> Redirect and append stdout to a file 2>> Redirect and append stderr to a file &>> Redirect and append both stdout and stderr to a file

We ve seen how pipes and filters work with using a single program on some input data

We ve seen how pipes and filters work with using a single program on some input data a_program 1 2 3

We ve seen how pipes and filters work with using a single program on some input data But what about running the same program separately, for each input?

We ve seen how pipes and filters work with using a single program on some input data But what about running the same program separately, for each input? a_program 1 a_program 2 a_program 3...

We ve seen how pipes and filters work with using a single program on some input data But what about running the same program separately, for each input? a_program 1 a_program 2 a_program 3... We can use loops for this

So what can we do with loops?

So what can we do with loops? Let s go back to our first set of pdb files, and assume we want to compress each of them data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb

So what can we do with loops? Let s go back to our first set of pdb files, and assume we want to compress each of them We could do the following for each: data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb $ zip cubane.pdb.zip cubane.pdb adding: cubane.pdb (deflated 73%)

So what can we do with loops? Let s go back to our first set of pdb files, and assume we want to compress each of them We could do the following for each: data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb $ zip cubane.pdb.zip cubane.pdb adding: cubane.pdb (deflated 73%) typical output from the zip command

So what can we do with loops? Let s go back to our first set of pdb files, and assume we want to compress each of them We could do the following for each: data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb $ zip cubane.pdb.zip cubane.pdb adding: cubane.pdb (deflated 73%) The zip file we wish to create typical output from the zip command

So what can we do with loops? Let s go back to our first set of pdb files, and assume we want to compress each of them We could do the following for each: data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb $ zip cubane.pdb.zip cubane.pdb adding: cubane.pdb (deflated 73%) The zip file we wish to create The file(s) we wish to add to the zip file typical output from the zip command

So what can we do with loops? Let s go back to our first set of pdb files, and assume we want to compress each of them We could do the following for each: data cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb $ zip cubane.pdb.zip cubane.pdb adding: cubane.pdb (deflated 73%) Not efficient for many files

Using a loop, we can iterate over each file, and run zip on each of them: $ for file in *.pdb; do zip $file.zip $file; done

Using a loop, we can iterate over each file, and run zip on each of them: $ for file in *.pdb; do zip $file.zip $file; done For each pdb file in this directory

Using a loop, we can iterate over each file, and run zip on each of them: $ for file in *.pdb; do zip $file.zip $file; done Run this command

Using a loop, we can iterate over each file, and run zip on each of them: $ for file in *.pdb; do zip $file.zip $file; done This is the end of the loop

Using a loop, we can iterate over each file, and run zip on each of them: $ for file in *.pdb; do zip $file.zip $file; done The semicolons separate each part of the loop construct

Using a loop, we can iterate over each file, and run zip on each of them: $ for file in *.pdb; do zip $file.zip $file; done This expands to a list of every pdb file

Using a loop, we can iterate over each file, and run zip on each of them: $ for file in *.pdb; do zip $file.zip $file; done This variable holds the next pdb file in the list

Using a loop, we can iterate over each file, and run zip on each of them: $ for file in *.pdb; do zip $file.zip $file; done We reference the file variable, and use. to add the zip extension to the filename

Using a loop, we can iterate over each file, and run zip on each of them: $ for file in *.pdb; do zip $file.zip $file; done We reference the file variable again

Using a loop, we can iterate over each file, and run zip on each of them: $ for file in *.pdb; do zip $file.zip $file; done adding: cubane.pdb (deflated 73%) adding: ethane.pdb (deflated 70%) adding: methane.pdb (deflated 66%) adding: octane.pdb (deflated 75%) adding: pentane.pdb (deflated 74%) adding: propane.pdb (deflated 71%)

Using a loop, we can iterate over each file, and run zip on each of them: $ for file in *.pdb; do zip $file.zip $file; done adding: cubane.pdb (deflated 73%) adding: ethane.pdb (deflated 70%) In one line, we ve ended up with all files zipped

Using a loop, we can iterate over each file, and run zip on each of them: $ for file in *.pdb; do zip $file.zip $file; done adding: cubane.pdb (deflated 73%) adding: ethane.pdb (deflated 70%) In one line, we ve ended up with all files zipped $ ls *.zip cubane.pdb.zip methane.pdb.zip pentane.pdb.zip ethane.pdb.zip octane.pdb.zip propane.pdb.zip

Now instead, what if we wanted to output the first line of each pdb file?

Now instead, what if we wanted to output the first line of each pdb file? We could use head -1 *.pdb for that, but it would produce: ==> cubane.pdb <== COMPND CUBANE ==> ethane.pdb <== COMPND ETHANE ==> methane.pdb <== COMPND METHANE

Now instead, what if we wanted to output the first line of each pdb file? We could use head -1 *.pdb for that, but it would produce: head produces this (it s not in the file) ==> cubane.pdb <== COMPND CUBANE ==> ethane.pdb <== COMPND ETHANE ==> methane.pdb <== COMPND METHANE

Now instead, what if we wanted to output the first line of each pdb file? We could use head -1 *.pdb for that, but it would produce: head produces this (it s not in the file) ==> cubane.pdb <== COMPND CUBANE ==> ethane.pdb <== COMPND ETHANE ==> methane.pdb <== COMPND METHANE this is actually the first line in this file!

Now instead, what if we wanted to output the first line of each pdb file? We could use head -1 *.pdb for that, but it would produce: head produces this (it s not in the file) ==> cubane.pdb <== COMPND CUBANE ==> ethane.pdb <== COMPND ETHANE ==> methane.pdb <== COMPND METHANE this is actually the first line in this file! Perhaps we only want the actual first lines

However, using a loop:

However, using a loop: $ for file in *.pdb; do head -1 $file; done

However, using a loop: $ for file in *.pdb; do head -1 $file; done We use $file as we did before, but this time with the head command

However, using a loop: $ for file in *.pdb; do head -1 $file; done COMPND CUBANE COMPND ETHANE COMPND METHANE COMPND OCTANE COMPND PENTANE COMPND PROPANE

What if we wanted this list sorted in reverse afterwards?

What if we wanted this list sorted in reverse afterwards? Simple! $ (for file in ls *.pdb; do head -1 $file; done) sort r

What if we wanted this list sorted in reverse afterwards? Simple! $ (for file in ls *.pdb; do head -1 $file; done) sort r Using a pipe, we can just add this on the end

What if we wanted this list sorted in reverse afterwards? Simple! $ (for file in ls *.pdb; do head -1 $file; done) sort r COMPND PROPANE COMPND PENTANE COMPND OCTANE COMPND METHANE COMPND ETHANE COMPND CUBANE

zip for ; do done; Create a compressed zip file with other files in it Loop over a list of data and run a command once for each element in the list

created by Steve Crouch July 2011 Copyright Software Carpentry and The University of Southampton 2010-2011 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information.

All this typing is giving me RSI Didn t we use a file to store the commands in earlier examples?

All this typing is giving me RSI Didn t we use a file to store the commands in earlier examples? - Yes we did. Let s have a look at how that works

If you put commands in a file and chmod the file to be executable then you can run the file as a command. SSTDMSJP01:test sjp23$ cat > y.sh for i in * do echo == $i == head -1 $i tail -1 $i Done SSTDMSJP01:test sjp23$ chmod 700 y.sh

SSTDMSJP01:test sjp23$./y.sh == a.txt == Hi there xxx == b.txt == File B File B == y.sh == for i in * done SSTDMSJP01:test sjp23$

What happens when I try to run a file that is not a script? $ more a.txt Hi there this is file a Xxx $

Unintended consequences $ chmod 700 a.txt $./a.txt./a.txt: line 1: Hi: command not found./a.txt: line 2: this: command not found./a.txt: line 3: is: command not found a: cannot open `a' (No such file or directory)./a.txt: line 5: xxx: command not found $ Hi there this is file a Xxx

How to make sure your script is a script 1) Only use the execute (x) permission if you are going to execute it. 2) Use an interpreter header as the first line of the file. (You can use the which bash to find your bash programs location.) #!/bin/bash for j in *

What about other control structures If test e myfile; then ; fi Case ; Esac While I m not going to tell you because 1)you can find this out on your own (man bash) 2)if you find yourself using if and case then you should probably switch to a programming language like python. It s safer.