Redirection and Pipes

Similar documents
Unix Processes. What is a Process?

Sperimentazioni I LINUX commands tutorial - Part II

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

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

The Unix Shell & Shell Scripts

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

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

S E C T I O N O V E R V I E W

Process Management! Goals of this Lecture!

I/O and Shell Scripting

CSC UNIX System, Spring 2015

An Illustrated Guide to Shell Magic: Standard I/O & Redirection

Introduction to UNIX Shell Exercises

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

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

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

Process Management 1

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

System Programming. Introduction to Unix

The Classical OS Model in Unix

Review of Fundamentals

Systems Programming/ C and UNIX

5/8/2012. Exploring Utilities Chapter 5

Chapter 1 - Introduction. September 8, 2016

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Essentials for Scientific Computing: Bash Shell Scripting Day 3

6 Redirection. Standard Input, Output, And Error. 6 Redirection

Lecture 3. Unix. Question? b. The world s best restaurant. c. Being in the top three happiest countries in the world.

commands exercises Linux System Administration and IP Services AfNOG 2015 Linux Commands # Notes

Processes and Shells

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

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

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

Introduction to Linux Organizing Files

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

Shells and Shell Programming

Lab 2: Linux/Unix shell

Introduction to Unix CHAPTER 6. File Systems. Permissions

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

5/20/2007. Touring Essential Programs

Introduction to UNIX. Introduction. Processes. ps command. The File System. Directory Structure. UNIX is an operating system (OS).

Introduction to UNIX. CSE 2031 Fall November 5, 2012

What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

Advanced UNIX. 2. The Shell (Ch.5, Sobell) Objectives. to supplement the Introduction to UNIX slides with extra information about the Shell

Welcome to Linux. Lecture 1.1

M2PGER FORTRAN programming. General introduction. Virginie DURAND and Jean VIRIEUX 10/13/2013 M2PGER - ALGORITHME SCIENTIFIQUE

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

Consider the following program.

CSC 2500: Unix Lab Fall 2016

UNIX System Programming Lecture 3: BASH Programming

minit Felix von Leitner September 2004 minit

An Introduction to Unix Power Tools

Lecture-4. Introduction to Unix: More Commands, Boot-up Actions and X Window

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

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

Command-line interpreters

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.

The Unix Shell. Job Control

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Introduction to Linux

Shell Programming Systems Skills in C and Unix

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

A Brief Introduction to the Linux Shell for Data Science

Shells and Shell Programming

Linux Command Line Primer. By: Scott Marshall

Introduction To. Barry Grant

EECS2301. Lab 1 Winter 2016

Bash print to stderr

Introduction to UNIX Part II

Introduction to the Linux Command Line January Presentation Topics

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

6.033 Computer System Engineering

Study Guide Processes & Job Control

System Administration

Basic UNIX commands. HORT Lab 2 Instructor: Kranthi Varala

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

Introduction to Linux. Roman Cheplyaka

Operating Systems. Lecture 07. System Calls, Input/Output and Error Redirection, Inter-process Communication in Unix/Linux

Basics. I think that the later is better.

18-Sep CSCI 2132 Software Development Lecture 6: Links and Inodes. Faculty of Computer Science, Dalhousie University. Lecture 6 p.

Process Management! Goals of this Lecture!

Pipes and FIFOs. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

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

Useful Unix Commands Cheat Sheet

find starting-directory -name filename -user username

Introduction to the Shell

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

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

Lecture 7: file I/O, more Unix

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

The Shell, System Calls, Processes, and Basic Inter-Process Communication

Advanced Linux Commands & Shell Scripting

Command Line Interface Techniques

Inter-Process Communication

The Unix Shell. Pipes and Filters

Scripting Languages Course 1. Diana Trandabăț

CST Algonquin College 2

Transcription:

Redirection and Pipes Boris Veytsman July 26, 2001 This document contains lecture notes for informal Unix seminar for ITT AES employees (Reston, VA). No information in this document is either endorsed by or attributable to ITT. This document contains no ITT Privileged/Proprietary Information.

Unix Filters A plumbing system is very much like your electrical system, except that instead of electricity, it has water, and instead of wires, it has pipes, and instead of radios and waffle irons, it has faucets and toilets. So the truth is that your plumbing systems is nothing at all like your electrical system, which is good, because electricity can kill you. Dave Barry, The Taming of the Screw 1

A Unix system activity is mostly processing large streams of texts. Examples: Send a stream of HTML-encoded text to a client (Web server) Take a log of HTTP server and find all clients from.gov computers (Web analysis) Substitute all instances of Mr. Bean to Dr. Bean in the text (text processing) You have input and change it into output. 2

Filter Filter 1. A text stream 2. Programs take it from stdin, do something and put it into stdout (filters). connection < input valve > and >> output valve 3

Suppose I want to know what does the user Steve Helfand do on my machine. Step 1: Obtain the list of all processes: boris@reston-0491:~$ ps au USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 256 0.0 0.0 1004 0 tty2 SW Jul04 0:00 [getty] root 257 0.0 0.0 1004 0 tty3 SW Jul04 0:00 [getty] root 258 0.0 0.0 1004 0 tty4 SW Jul04 0:00 [getty] root 259 0.0 0.0 1004 0 tty5 SW Jul04 0:00 [getty] root 260 0.0 0.0 1004 0 tty6 SW Jul04 0:00 [getty] root 30255 0.0 0.0 1004 0 tty1 SW Jul05 0:00 [getty] shelfand 27111 0.0 0.2 2020 736 pts/2 S Jul13 0:01 -bash shelfand 27113 0.0 0.0 2224 0 pts/2 TW Jul13 0:00 [equal.pl]... 4

Step 2: Take only the ones belonging to shelfand: grep ˆshelfand : boris@reston-0491:~$ ps au grep ^shelfand shelfand 27111 0.0 0.2 2020 736 pts/2 S Jul13 0:01 -bash shelfand 27113 0.0 0.0 2224 0 pts/2 TW Jul13 0:00 [equal.pl] shelfand 27114 0.0 0.0 3184 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 27324 0.0 0.0 3180 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 28009 0.0 0.0 2224 0 pts/2 TW Jul13 0:00 [equal.pl] shelfand 28404 0.0 0.0 3120 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 28408 0.0 0.0 3116 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 28410 0.0 0.0 2224 0 pts/2 TW Jul13 0:00 [equal.pl] shelfand 28699 0.0 0.0 3128 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 28720 0.0 0.0 3124 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 28723 0.0 0.0 2224 0 pts/2 TW Jul13 0:00 [equal.pl] shelfand 29832 0.0 0.0 3148 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 29920 0.0 0.0 3144 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 29922 0.0 0.0 2220 0 pts/2 TW Jul13 0:00 [equal.pl]... 5

Step 3: Print only the name of the process (field 11): awk {print $11} boris@reston-0491:~$ ps au grep ^shelfand awk {print $11} -bash [equal.pl] [trial.pl] [trial.pl] [equal.pl] [trial.pl] [trial.pl] [equal.pl] [trial.pl] [trial.pl] [equal.pl] [trial.pl] [trial.pl] [equal.pl] [trial.pl] [trial.pl] [trial.pl]... 6

Step 4: Find the unique names: sort -u boris@reston-0491:~$ ps au grep ^shelfand awk {print $11} sort -u [a.out] -bash [equal.pl] [trial.pl] Step 5: Save everything in a file boris@reston-0491:~$ ps au grep ^shelfand awk {print $11} \ sort -u > results 7

ps au grep ^shelfand awk {print $11} sort u results 8

Argument List and Backtics He draweth out the thread of his verbosity finer than the staple of his argument. William Shakespeare, Love s Labour s Lost 9

Two ways to supply information to the program: 1. Through standard input: ls *.c more shows the list of files screen by screen 2. Through argument list: more *.c shows each file contents screen by screen Additional valve in the filters! arg Filter arg Filter 10

A creative use of argument list: Filter Filter Filter We can process the argument list through its own filters. backtics. The tool: 11

Suppose I want to kill all Steve Helfand s processes on my machine. I can use kill number number..., but how can I obtain the numbers? Step 1: Get the list of processes: boris@reston-0491:~$ ps au grep ^shelfand shelfand 27111 0.0 0.2 2020 736 pts/2 S Jul13 0:01 -bash shelfand 27113 0.0 0.0 2224 0 pts/2 TW Jul13 0:00 [equal.pl] shelfand 27114 0.0 0.0 3184 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 27324 0.0 0.0 3180 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 28009 0.0 0.0 2224 0 pts/2 TW Jul13 0:00 [equal.pl] shelfand 28404 0.0 0.0 3120 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 28408 0.0 0.0 3116 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 28410 0.0 0.0 2224 0 pts/2 TW Jul13 0:00 [equal.pl] shelfand 28699 0.0 0.0 3128 0 pts/2 TW Jul13 0:00 [trial.pl] shelfand 28720 0.0 0.0 3124 0 pts/2 TW Jul13 0:00 [trial.pl]... 12

Step 2: Get the numbers of processes: awk {print $2} : boris@reston-0491:~$ ps au grep ^shelfand awk {print $2} 27111 27113 27114 27324 28009 28404 28408 28410 28699 28720 28723 29832 29920 29922 29923 29964... 13

Step 3: Send this to kill: boris@reston-0491:~$ kill ps au grep ^shelfand awk {print $2} bash: kill: (27111) - Not owner bash: kill: (27113) - Not owner bash: kill: (27114) - Not owner bash: kill: (27324) - Not owner bash: kill: (28009) - Not owner bash: kill: (28404) - Not owner bash: kill: (28408) - Not owner bash: kill: (28410) - Not owner bash: kill: (28699) - Not owner bash: kill: (28720) - Not owner bash: kill: (28723) - Not owner bash: kill: (29832) - Not owner bash: kill: (29920) - Not owner bash: kill: (29922) - Not owner bash: kill: (29923) - Not owner bash: kill: (29964) - Not owner... 14

ps au grep ^shelfand awk {print $2} kill 15

Standard Error A man of genius makes no mistakes. His errors are volitional and are the portals of discovery. James Joyce, Ulysses Besides standard output, a Unix program usually has a standard error. Another output valve: Filter stderr Filter stderr 16

Example: kill ps au grep ^shelfand awk {print $2} > results The file results is empty You can use >&: boris@reston-0491:~$ kill ps au grep ^shelfand \ awk {print $2} >& results boris@reston-0491:~$ cat results bash: kill: (27111) - Not owner bash: kill: (27113) - Not owner... 17

Advanced Redirection in /bin/sh Any sufficiently advanced technology is indistinguishable from magic. Arthur C. Clarke Unfortunately /bin/csh has nothing beyond >& (see http:// www.perl.com/pub/language/versus/csh.html). Use /bin/sh and derivatives. 18

File descriptors: streams associated with I/O (like in C, Fortran, Perl... ) Standard descriptors: 0: standard input 1: standard output 2: standard error User-made descriptors: 3, 4,... Redirection of descriptors: m>&n means send m to the place n is going 19

Sending error to file and processing input. calculate 2>error.log analyze Sending output to file and processing error. Here we must be careful. The obvious solution is wrong: calculate 1>results 2>&1 analyze 1. Standard output goes to the file results 2. Standard error goes to the same place, i.e. to the file results 20

Right solution: calculate 2>&1 1>results analyze 1. Standard error goes to where standard output is going 2. Standard output goes to the file results, leaving standard error untouched calculate results analyze 21

Switching error and output: Again the obvious solution is wrong: calculate 2>&1 1>&2 What does it do? To switch two variable you need the third one! calculate 3>&2 2>&1 1>&3 The pipe connects only standard output to standard input, but due to redirection we can make whatever we want! 22

Duplicating Streams: tee clone, n: 1. An exact duplicate, as in our product is a clone of their product. 2. A shoddy, spurious copy, as in their product is a clone of our product. 23

Suppose we want to both save the output and analyze it? In plumbing we have T-connectors. In Unix we have tee. Filter tee file 24

Example: calculate tee results analyze > processed_results GNU tee can write to several files, append (-a), etc. 25

Concatenating Files: cat Besides forking we need joining... A commune is where people join together to share their lack of wealth. R. Stallman cat (from concatenate) takes several files and joins them together: file file file cat Filter 26

cat can even use standard input: file file file cat Filter 27

Example: cat file1 file2 - file3 cat will dump file1, then file2, then its input, then file3. 28