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.

Slides:



Advertisements
Similar presentations
UNIT 12 UNIX I/O Redirection.
Advertisements

UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
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.
Introduction to UNIX CSE 2031 Fall May 2015.
Introduction to Unix – CS 21 Lecture 10. Lecture Overview Midterm questions Jobs and processes description The foreground and background Controlling jobs.
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin
CS 497C – Introduction to UNIX Lecture 21: - The Shell Chin-Chih Chang
BIF703 Redirection Continued: Pipes. Redirection Recall from the previous slides we defined stdin, stdout, and stderr and we learned how to redirect these.
Guide To UNIX Using Linux Third Edition
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Chapter 4: UNIX File Processing Input and Output.
Introduction to UNIX/Linux Exercises Dan Stanzione.
3 File Processing Mauro Jaskelioff. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Agenda Basic Shell Operations Standard Input / Output / Error Redirection of Standard Input / Output / Error ( >, >>,
CIT 140: Introduction to ITSlide #1 CSC 140: Introduction to IT I/O Redirection.
Guide To UNIX Using Linux Fourth Edition
BIF703 stdin, stdout, stderr Redirection. stdin, stdout, stderr Recall the Unix philosophy “do one thing well”. Unix has over one thousand commands (utilities)
The “File System” Under UNIX, (almost) everything is a “file”: –Normal files –Directories –Hardware –Sockets –Pipes Things that are not files: –Users –Groups.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
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.
Introduction to Unix – CS 21 Lecture 9. Lecture Overview Shell description Shell choices History Aliases Topic review.
Linux+ Guide to Linux Certification, Third Edition
LINUX programming 1. INDEX UNIT-III PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Problem solving approaches in Unix,Using.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
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
Advanced Shell Tricks Copyright © The University of Southampton 2011 This work is licensed under the Creative Commons Attribution License See
I/O and Redirection. Standard I/O u Standard Output (stdout) –default place to which programs write u Standard Input (stdin) –default place from which.
Introduction to Unix – CS 21 Lecture 4. Lecture Overview * cp, mv, and rm Looking into files The file command head and tail cat and more What we’ve seen.
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
Operating Systems Lecture 10. Agenda for Today Review of previous lecture Input, output, and error redirection in UNIX/Linux FIFOs in UNIX/Linux Use of.
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
Manipulating Files Refresher. The touch Command touch is used to create a new, empty file. If the file already exists, touch updates the time and date.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
File and Folder CLI Commands 12/24/ Agenda Overview of OS functions and the SHELL Internal v External Commands Command History Making & Modifying.
Chapter 5: The Shell The Man in the Middle. In this chapter … The command line Input, output, and redirection Process management Wildcards and expansion.
Linux+ Guide to Linux Certification, Second Edition
Lesson 6-Using Utilities to Accomplish Complex Tasks.
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
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Exploring Shell Commands, Streams, and Redirection
Lesson 5-Exploring Utilities
CS1010 Programming Methodology
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
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
The Linux Command Line Chapter 6
Exploring Shell Commands, Streams, and Redirection
Chapter Four UNIX File Processing.
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
More advanced BASH usage
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Lecture 4 Redirecting standard I/O & Pipes
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:

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 redirection Piping between different programs Putting it all together for some powerful tools

Important Points From Lab Get used to both modes of chmod Understand how directory permissions affect access Write permission only doesn’t let you do anything Start thinking about using wildcards to speed up tasks

Good Commands To Know: wc Stands for word count Will report the number of lines, words, and characters in a file Works with all sorts of files, but only makes sense with text files Useful for all sorts of applications Either file direction or piping is required…

Example Of wc

sort Will sort the lines of a file into alphabetical order Useful on data files or output from programs Usage: sort [OPTIONS] [FILE]

Example Of sort

uniq Will go through a file and remove duplicate copies of a line Will only remove duplicate copies of lines that are adjacent Useful on files that might have repeated unnecessary data Usage: uniq [OPTIONS] [FILE]

Example Of uniq

Wouldn’t It Be Nice If We Could sort And uniq A File? No command exists that does both By the end of the lecture, we should see if this is possible Do you think this will be possible? Hint: Remember Unix was designed so that tasks could be easily automated and so that tasks could work together

What Exactly Is Input And Output? Input Everything a Unix program reads Usually from the keyboard What you type Output Everything a Unix program writes Usually to the screen What you see

Streams Of Input And Output stdin Input from the keyboard stdout Output to the screen stderr Error messages that show up on the screen

stdin Everything you type into the terminal The programs mostly expect all of their input from the user (you) typing at the keyboard

stdout The results of a running program Goes to the terminal you ran the program from

stderr Appears the same location as stdout Used for error messages Allows for the error messages to be separated from normal output

Redirecting These Streams Deciphering “echo ‘hi’ > helloFile” > Sends all output that normally would go to stdout into a file Completely overwrites whatever file was there or creates a new file if none existed

Example Of >

A Tricky Catch To Remember You must be careful because ‘>’ will overwrite files

Redirecting stdin < Makes all of the input for a file come from a file as opposed to the keyboard Allows you to input a lot of data without a lot of typing Exercise: Go to lab and run cat without any parameters

Example Of <

Redirection Of Output Without File Overwrite What if you want to add information to a file instead of overwriting it? >> Appends all output of a program to a file Will create a file if it doesn’t exist, but won’t overwrite any information currently in a file

Example Of Append Redirection

One More Stream Don’t forget about stderr 2> Not some weird smiley This says redirect stderr to a file Important to note why this notation exists 0 = stdin 1 = stdout 2 = stderr

Example Of Redirecting stderr

Example Of The Difference Between stdout And stderr

Is This Ever Useful? Short answer: Yes! Longer answer: Whenever you want to save the output of a program, redirect it Whenever you want to avoid typing input, redirect it Whenever you want to isolate any error messages, redirect it

Possible Uses For File Redirection Saving data generated by one program for use by another If you continually type the same input into different programs, place that data into a file and redirect it into the different programs

How To Remember Which Arrow Does What cat < inputFile Think of this as the inputFile dumping all of its contents into cat In other words, inputFile is going into cat cat > outputFile In this case, cat is dumping its results into outputFile In other words, cat is going into outputFile

What About Other Command Flags? The input or output file must come directly after the redirection operator Example: Right: ls –l * > listingOutput Wrong: ls > -l * listingOutput Question: How do you think this line is interpreted? As a general rule of thumb, redirection should come at the end of a line Redirection should come after all arguments to the program to avoid problems like the above

Combining These Flags Entirely possible to have both on the same line Each redirection operator assumes the next argument is what is being redirected Question: Can you have > and >> on the same line?

Answer

What Does This Command Do? ls /bin/doesntExist 2>> errorFile Appends stderr to the end of errorFile

What About These Commands?

Piping: Tying Programs Together We can combine some of the previous commands together with a pipe Pipes feed the output of one program into another | Yes, that’s a vertical bar

What Does A Pipe Look Like?

sort And uniq Answering our previous question, we see that it should be possible to both sort and uniq a file at once using pipes How exactly would we do it?

sort And uniq With Redirection

Same Example With Piping

How To Read Pipes You can have more than one pipe per line if you’d like Always read left to right, and output flows left to right

Putting It All Together What if we want to have both file redirection as well as piping? Well, things start to look ugly, but this is entirely doable

Complex Example

Really Complex Example

Is All This Stuff Worth It? Well, let’s compare this with how you do things in Windows With piping, several tasks can be combined and run in one single step What if I wanted to sort and uniq a Word file? Open the file and highlight all of the text Select Table->Sort (not intuitive) Specify how to sort and click o.k. Word doesn’t have a uniq feature…

Slowly, Your Power Grows With this information, you are slowly becoming a Unix power user Maybe you’ll save the world from dinosaurs yet…

Next Time More thorough explanation of wildcards Regular expression overview grep – what exactly is it? Quiz #1