Standard I/O and Pipes. Standard Input and Output Linux provides three I/O channels to Programs Standard input (STDIN) - keyboard by default Standard.

Презентация:



Advertisements
Похожие презентации
Unit 3 Text Processing and System Configuration tools.
Advertisements

Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
Using Bash Shell. Command Line Shortcuts File Globbing Globbing is wildcard expansion: * - matches zero or more characters ? - matches any single character.
© 2009 Avaya Inc. All rights reserved.1 Chapter Three, Voic Pro Advanced Functions Module Three – TAPI.
© 2005 Cisco Systems, Inc. All rights reserved.INTRO v Managing Your Network Environment Managing Cisco Devices.
Multiples Michael Marchenko. Definition In mathematics, a multiple is the product of any quantity and an integer. in other words, for the quantities a.
© 2005 Cisco Systems, Inc. All rights reserved.INTRO v Module Summary The Cisco Discovery Protocol is an information-gathering tool used by network.
© 2009 Avaya Inc. All rights reserved.1 Chapter Three, Voic Pro Advanced Functions Module One – Text to Speech.
Unit 2 Users Management. Users Every user is assigned a unique User ID number (UID) UID 0 identifies root User accounts normally start at UID 500 Users'
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
Linux Daemons. Agenda What is a daemon What is a daemon What Is It Going To Do? What Is It Going To Do? How much interaction How much interaction Basic.
Basic Input - Output. Output functions printf() – is a library function that displays information on-screen. The statement can display a simple text message.
Week Configuring the Bash Shell 2 Environment Variables Bash variables are local to a single shell by default Set with VARIABLE= VALUE Environment.
© 2003, Cisco Systems, Inc. All rights reserved. CSPFA Chapter 9 Routing.
AVVID Troubleshooting Tools © 2004 Cisco Systems, Inc. All rights reserved. Troubleshooting Using Other Tools IPTT v
S12-1 PAT318, Section 12, March 2005 SECTION 12 LISTS.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Route Selection Using Policy Controls Employing AS-Path Filters.
Copyright 2003 CCNA 1 Chapter 9 TCP/IP Transport and Application Layers By Your Name.
© 2006 Cisco Systems, Inc. All rights reserved.BCMSN v Defining VLANs Correcting Common VLAN Configuration Errors.
PAT312, Section 20, December 2006 S20-1 Copyright 2007 MSC.Software Corporation SECTION 20 LISTS.
Транксрипт:

Standard I/O and Pipes

Standard Input and Output Linux provides three I/O channels to Programs Standard input (STDIN) - keyboard by default Standard output (STDOUT) - terminal window by default Standard error (STDERR) - terminal window by default

Redirecting Output to a File STDOUT and STDERR can be redirected to files: command operator filename Supported operators include: > Redirect STDOUT to file 2> Redirect STDERR to file &> Redirect all output to file File contents are overwritten by default. >> appends.

Redirecting Output to a File Examples This command generates output and errors when run as non- root: $ ls –l f1.txt > f2.txt (only result is stored not error) Operators can be used to store output and errors: $ ls –l tot.txt 2> f3.txt(only error is stored not the result) $ ls –l tot.txt f1.txt &> f3.txt (concatenates the result and stores in f3.txt)

Redirecting STDOUT to a Program (Piping) Pipes (the | character) can connect commands: command1 | command2 Sends STDOUT of command1 to STDIN of command2 instead of the screen. STDERR is not forwarded across pipes Used to combine the functionality of multiple tools command1 | command2 | command3... etc

Redirecting STDOUT to a Program Examples less: View input one page at a time: $ ls -l /etc | less Input can be searched with / mail: Send input via $ echo "test " | mail -s "test" lpr : Send input to a printer $ echo "test print" | lpr $ echo "test print" | lpr -P printer_name

Combining Output and Errors Some operators affect both STDOUT and STDERR &>: Redirects all output: $ ls -l /etc -name passwd &> file1.txt 2>&1: Redirects STDERR to STDOUT Useful for sending all output through a pipe $ ls -l /etc -name passwd 2>&1 | less (): Combines STDOUTs of multiple programs $ ( cal 2007 ; cal 2008 ) | less

Redirecting to Multiple Targets (tee) $ command1 | tee filename | command2 Stores STDOUT of command1 in filename, then pipes to command2 Uses: Troubleshooting complex pipelines Simultaneous viewing and logging of output

Redirecting STDIN from a File Redirect standard input with < Some commands can accept data redirected to STDIN from a file: $ tr 'A-Z' 'a-z' < file1.txt This command will translate the uppercase characters in file1.txt to lowercase Equivalent to: $ cat file1.txt | tr 'A-Z' 'a-z'

Sending Multiple Lines to STDIN Redirect multiple lines from keyboard to STDIN with <<WORD All text until WORD is sent to STDIN Sometimes called a heretext $ mail -s "Please Call" <<END > Hi Jane, > > Please give me a call when you get in. We may need > to do some maintenance on server1. > > Details when you're on-site, > Boris > END

Scripting: for loops Performs actions on each member of a set of values Example: for NAME in joe jane julie do MESSAGE='Projects are due today!' echo $MESSAGE | mail -s Reminder $ADDRESS done Can also use command-output and file lists: for num in $(seq 1 10) Assigns 1-10 to $num seq X Y prints the numbers X through Y for file in *.txt Assigns names of text files to $file