Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.

Slides:



Advertisements
Similar presentations
การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation วิบูลย์ วราสิทธิชัย นักวิชาการคอมพิวเตอร์ ศูนย์คอมพิวเตอร์ ม. สงขลานครินทร์ เวอร์ชั่น 1 วันที่
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 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
T UTORIAL OF U NIX C OMMAND & SHELL SCRIPT S 5027 Professor: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2015.
Lecture 02CS311 – Operating Systems 1 1 CS311 – Lecture 02 Outline UNIX/Linux features – Redirection – pipes – Terminating a command – Running program.
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.
Unix Files, IO Plumbing and Filters The file system and pathnames Files with more than one link Shell wildcards Characters special to the shell Pipes and.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
UNIX Filters.
1 UNIX essentials (hands-on) the directory tree running programs the shell (using the T-shell) → command line processing → special characters → command.
Shell Script Examples.
Chapter 4: UNIX File Processing Input and Output.
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.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Shell The Shell The agency that.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
LIN 6932 Unix Lecture 6 Hana Filip. LIN 6932 HW6 - Part II solutions posted on my website see syllabus.
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
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.
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
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.
Linux+ Guide to Linux Certification, Third Edition
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Pipes and Redirection in Linux ASFA Programming III C. Yarbrough.
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.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
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.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Chapter Four I/O Redirection1 System Programming Shell Operators.
Searching and Sorting. Why Use Data Files? There are many cases where the input to the program may come from a data file.Using data files in your programs.
I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
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.
– Introduction to the Shell 1/21/2016 Introduction to the Shell – Session Introduction to the Shell – Session 3 · Job control · Start,
Linux+ Guide to Linux Certification, Second Edition
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
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.
Linux Administration Working with the BASH Shell.
Filters and Utilities. Notes: This is a simple overview of the filtering capability Some of these commands are very powerful ▫Only showing some of the.
CIRC Summer School 2016 Baowei Liu
Linux 201 Training Module Linux Adv File Mgmt.
Tutorial of Unix Command & shell scriptS 5027
Lesson 5-Exploring Utilities
The UNIX Shell Learning Objectives:
Chapter 6 Filters.
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
Unix Talk #2 (sed).
Chapter Four UNIX File Processing.
Exploring Shell Commands, Streams, and Redirection
More advanced BASH usage
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Presentation transcript:

Module 6 – Redirections, Pipes and Power Tools.

STDin 0 STDout 1 STDerr 2 Redirections

Capture STDout with 1> or simply > Append STDout with >> # ls > list.txt Redirections

Capture STDerr with 2> Append STDerr with 2>> # ls bogos.txt 2> errors.txt Redirections

Merging Standard Output With Standard Error Merge standard output and standard error in to the same file with 2>&1 – binding the streams together # ls ort.txt bogos.txt 2>&1 both.txt

Regular Expressions (RegEx).Matches any single character [list]Matches any single character in the list [range]Matches any single character in range [^ ]Matches any single character not in list or range *Matches previous character 0 or more times \{n\}Matches previous character n times \{n,\}Matches previous character at least n times \{n,m\}Matches previous character between n and m times ^Matches regular expressions at the start of the line only $Matches regular expressions at the end of the line only \Quote recognition of special characters

A pipe ( | ) takes the output of the command and passes it as an input into the following command. A pipe will continue to pass STDout even if there is an error. he will simply drop STDerr # ls -l /etc | wc -l Pipes

The grep command is used to search text or searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines. # grep ro /etc/passwd Or # cat /etc/passwd | grep ro -i, --ignore-case # cat /etc/httpd/conf/httpd.conf | grep -i server The grep command

Combining grep with regex # ls -l /etc | grep "^d“ # ls -l /etc | grep "d$“ # grep '50[0-9]' /etc/passwd # ifconfig | grep -o '[0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9][0-9][0-9]‘ # ifconfig | grep -o '[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}'

The cut command can cut fields. We need to define the delimiter and we need to tell the cut command which field to cut: # cat /etc/passwd | cut –d”:” -f1 !!! Don’t forget to sort the spaces for cut with the tr command. The cut command

The cut command can cut fields. # ls -l /tmp | cut –d” ” -f1,5 !!! Don’t forget to sort the spaces for cut with the tr command. The cut command

The tr command The tr is used to squeeze multiple spaces into one space, helping the cut command in cutting the rows: # ls -l | tr -s “ “ | cut -d” “ –f1,4 !!! note the tr command “need” a space after the –s option

By default the sort command sort files in alphabetical order. We can sort by numbers with -n # ls -l /etc | sort !!! The sort command usually comes before the uniq command. The sort command

The uniq command can identify uniq entries, duplicate entries and can also count those entries. # uniq # uniq -d (duplicates) # uniq -dc (duplicates and count) The uniq command

Although awk is a complete pattern scanning and processing language, it is most commonly used as a Unix command-line filter to reformat the output of other commands. Basic awk example: # cat /etc/passwd | awk ‘{print $1}’ awk

sed is a steam editor We used sed earlier when we learned Search&Replace in VI. Remember VI ? :1,$s/old/new/g And in the shell: # sed –i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux # sed -n '/ort/p' passwd > ort.txt sed

A process is considered to have completed correctly in Linux if its exit status was 0. Any other result greater then 0 is considered an error. To find the exit status: # echo $? Unix/Linux Exit Status

; Run multiple commands in one run, sequentially. # ls ; echo hello ; tail -2 /etc/passwd & Sends process background(run in background paralleling ) #./script.sh & && What the double-ampersands are telling the system is "wait for the first command to successfully complete. If it does complete successfully, run the next command". Chaining Commands

Search Commands updatedb + locate which and whereis The find command (as root) # find / -name passwd

Exercise 1.Use redirection to save the ls command to a file. 2.Generate an error with ls and save the errors in a file 3.Generate both “good” STDout and “bad” STDerr and save them both in one file. 4.Count the directories under /home 5.Count all the users in the system 6.print only the permissions and the file-name fields using the cut command 7.Use awk to print the first and last field from /etc/passwd 8.use the find command to search for all files and directories with the name of passwd

Exercise 9. Log-in to the instructor server and find how many users are logged-in more then once and how many times are those users logged-in. 10.Where is the binary of the fdisk tool ? 11.find all the files and directories in the system with full permissions (777) 12.Find all the files in the system bigger then 2MB 13.Use the df command to print only the directory name (mount point) and the size 14. Use the fdisk tool to print only the disks without the partitions. 15.Copy /etc/passwd to your home dir and replace the shell from bash to sh