SlideShare a Scribd company logo
RedHat Enterprise Linux Essential
     Unit 7: Standard I/O and Pipes
Objectives
Upon completion of this unit, you should be able to:

Redirect I/O channels to files

Connect commands using pipes

Use the for loops to iterate over sets of values
Standard Input and Output

Linux provides three I/O channels to Programs

   Standard input (STDIN) - keyboard by default (0)

   Standard output (STDOUT) - terminal window by default (1)

   Standard error (STDERR) - terminal window by default (2)

     ls –l myfile nofile
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
 command > file - Direct standard output of command to file

 command >> file - Append standard output of command to file

 command < file - Send file as input to command

 command 2> file - Redirect error messages from command to file

 command 2>> file - Append error messages from command to file
Redirecting Output to a File
                          Examples
 This command generates output and errors when run as non-
  root:
 $ find /etc -name passwd

 Operators can be used to store output and errors:
 $ find /etc -name passwd > find.out

 $ find /etc -name passwd 2> /dev/null

 $ find /etc -name passwd > find.out 2> find.err
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 email:
      $ echo "test email" | mail -s "test" student
Combining Output and Errors
 Some operators affect both STDOUT and STDERR
   &>: Redirects all output:

    $ find /etc -name passwd &> find.all

   2>&1: Redirects STDERR to STDOUT

    Useful for sending all output through a pipe

    $ find /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


  ls -lR /etc | tee stage1.out | sort | tee stage2.out 

  | uniq -c | tee stage3.out | sort -r | tee stage4.out | less
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' < .bash_profile

 This command will translate the uppercase characters in
  .bash_profile to lowercase

 Equivalent to:
          $ cat .bash_profile | 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 see" student@localhost <<END

   > Hi all,

   >I’m Tam

   > END
Scripting: for loops

Performs actions on each member of a set of values

Example:
  for NAME in joe jane julie

  do

       ADDRESS="$NAME@example.com"

       MESSAGE='Projects are due today!'

       echo $MESSAGE | mail -s Reminder $ADDRESS

  done
Scripting: for loops continued

 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
example
#!/bin/bash
# alive2.sh
# Checks to see if hosts 192.168.0.1-192.168.0.20 are
  alive
# Iterate through IP addresses
for n in {1..20}; do
         host=192.168.0.$n
         ping -c2 $host &> /dev/null
         if [ $? = 0 ]; then
                 echo "$host is UP"
         else
                 echo "$host is DOWN"
         fi
done
Unit 7 standard i o

More Related Content

PDF
Course 102: Lecture 7: Simple Utilities
Ahmed El-Arabawy
 
PDF
Unit 10 investigating and managing
root_fibo
 
PPTX
Pipes and filters
bhatvijetha
 
PPTX
Basic unix commands
swtjerin4u
 
PDF
Course 102: Lecture 8: Composite Commands
Ahmed El-Arabawy
 
PDF
Basic basic solaris quick referent card
Bui Van Cuong
 
PPTX
Basics of unix
Deepak Singhal
 
PPTX
Linux commands part -2
bhatvijetha
 
Course 102: Lecture 7: Simple Utilities
Ahmed El-Arabawy
 
Unit 10 investigating and managing
root_fibo
 
Pipes and filters
bhatvijetha
 
Basic unix commands
swtjerin4u
 
Course 102: Lecture 8: Composite Commands
Ahmed El-Arabawy
 
Basic basic solaris quick referent card
Bui Van Cuong
 
Basics of unix
Deepak Singhal
 
Linux commands part -2
bhatvijetha
 

What's hot (11)

PDF
Course 102: Lecture 6: Seeking Help
Ahmed El-Arabawy
 
PDF
Linux basic commands with examples
abclearnn
 
PPTX
Unix - Filters/Editors
ananthimurugesan
 
PPTX
Linux basics
BhaskarNeeluru
 
PDF
Basic linux commands
Harikrishnan Ramakrishnan
 
PPT
Using Unix
Dr.Ravi
 
PDF
Unix Commands
Dr.Ravi
 
ODP
Prabu linux
Prabu Cse
 
ODP
Babitha.linux
banubabitha
 
ODP
Babitha.linux
banubabitha
 
PPT
Basic command ppt
Rohit Kumar
 
Course 102: Lecture 6: Seeking Help
Ahmed El-Arabawy
 
Linux basic commands with examples
abclearnn
 
Unix - Filters/Editors
ananthimurugesan
 
Linux basics
BhaskarNeeluru
 
Basic linux commands
Harikrishnan Ramakrishnan
 
Using Unix
Dr.Ravi
 
Unix Commands
Dr.Ravi
 
Prabu linux
Prabu Cse
 
Babitha.linux
banubabitha
 
Babitha.linux
banubabitha
 
Basic command ppt
Rohit Kumar
 
Ad

Viewers also liked (15)

PDF
DHCP
ali07zahrani
 
PDF
Server 2012 essentials
Steven Teiger
 
PDF
Red hat linux essentials
elshiekh1980
 
ODP
Using CloudStack With Clustered LVM
Marcus L Sorensen
 
PDF
Unit 8 text processing tools
root_fibo
 
PPT
Linux Forensics
Santosh Khadsare
 
PPTX
Rhel cluster basics 1
Manoj Singh
 
PPTX
Red hat linux essentials
Haitham Raik
 
PDF
RedHat Cluster!
Mohammed Al_Maraghy
 
PDF
RHEL-7 Administrator Guide for RedHat 7
Hemnath R.
 
PDF
Alphorm.com Support Formation Red Hat Administration Système III RHCSE-(Rh254)
Alphorm
 
ODP
Introduction to Red Hat
Albert Wong
 
PPT
RedHat Linux
Apo
 
PDF
Linux Cluster Concepts
nixsavy
 
PDF
JBoss Application Server 7
Ray Ploski
 
Server 2012 essentials
Steven Teiger
 
Red hat linux essentials
elshiekh1980
 
Using CloudStack With Clustered LVM
Marcus L Sorensen
 
Unit 8 text processing tools
root_fibo
 
Linux Forensics
Santosh Khadsare
 
Rhel cluster basics 1
Manoj Singh
 
Red hat linux essentials
Haitham Raik
 
RedHat Cluster!
Mohammed Al_Maraghy
 
RHEL-7 Administrator Guide for RedHat 7
Hemnath R.
 
Alphorm.com Support Formation Red Hat Administration Système III RHCSE-(Rh254)
Alphorm
 
Introduction to Red Hat
Albert Wong
 
RedHat Linux
Apo
 
Linux Cluster Concepts
nixsavy
 
JBoss Application Server 7
Ray Ploski
 
Ad

Similar to Unit 7 standard i o (20)

PPT
101 3.4 use streams, pipes and redirects
Acácio Oliveira
 
DOCX
lec4.docx
ismailaboshatra
 
PDF
Session3
Learn 2 Be
 
PPTX
shellScriptAlt.pptx
NiladriDey18
 
PPT
Shell Scripts
Dr.Ravi
 
PDF
Text mining on the command line - Introduction to linux for bioinformatics
BITS
 
PPT
Bioinformatica 29-09-2011-p1-introduction
Prof. Wim Van Criekinge
 
PPT
Shell Scripting
Gaurav Shinde
 
PPT
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ewout2
 
PPTX
Linux And perl
Sagar Kumar
 
PPTX
578781850-RHSA-1-Chapte578781850-RHSA-1-Chapter-4r-4.pptx
AbdellahELMAMOUN
 
PPT
ShellAdvanced shell scripting programm.ppt
ubaidullah75790
 
PPT
Unix And Shell Scripting
Jaibeer Malik
 
PPTX
Linux System commands Essentialsand Basics.pptx
mba1130feb2024
 
PPTX
Licão 08 system redirects
Acácio Oliveira
 
PPTX
Bash Shell Scripting
Raghu nath
 
PPT
101 3.2 process text streams using filters
Acácio Oliveira
 
PPT
390aLecture05_12sp.ppt
mugeshmsd5
 
PDF
Unix Tutorial
Sanjay Saluth
 
PDF
Course 102: Lecture 12: Basic Text Handling
Ahmed El-Arabawy
 
101 3.4 use streams, pipes and redirects
Acácio Oliveira
 
lec4.docx
ismailaboshatra
 
Session3
Learn 2 Be
 
shellScriptAlt.pptx
NiladriDey18
 
Shell Scripts
Dr.Ravi
 
Text mining on the command line - Introduction to linux for bioinformatics
BITS
 
Bioinformatica 29-09-2011-p1-introduction
Prof. Wim Van Criekinge
 
Shell Scripting
Gaurav Shinde
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ewout2
 
Linux And perl
Sagar Kumar
 
578781850-RHSA-1-Chapte578781850-RHSA-1-Chapter-4r-4.pptx
AbdellahELMAMOUN
 
ShellAdvanced shell scripting programm.ppt
ubaidullah75790
 
Unix And Shell Scripting
Jaibeer Malik
 
Linux System commands Essentialsand Basics.pptx
mba1130feb2024
 
Licão 08 system redirects
Acácio Oliveira
 
Bash Shell Scripting
Raghu nath
 
101 3.2 process text streams using filters
Acácio Oliveira
 
390aLecture05_12sp.ppt
mugeshmsd5
 
Unix Tutorial
Sanjay Saluth
 
Course 102: Lecture 12: Basic Text Handling
Ahmed El-Arabawy
 

More from root_fibo (9)

PDF
Unit 13 network client
root_fibo
 
PDF
Unit 12 finding and processing files
root_fibo
 
PDF
Unit 11 configuring the bash shell – shell script
root_fibo
 
PDF
Unit3 browsing the filesystem
root_fibo
 
PDF
Unit 9 basic system configuration tools
root_fibo
 
PDF
Unit 6 bash shell
root_fibo
 
PDF
Unit 5 vim an advanced text editor
root_fibo
 
PDF
Unit 4 user and group
root_fibo
 
PDF
Unit2 help
root_fibo
 
Unit 13 network client
root_fibo
 
Unit 12 finding and processing files
root_fibo
 
Unit 11 configuring the bash shell – shell script
root_fibo
 
Unit3 browsing the filesystem
root_fibo
 
Unit 9 basic system configuration tools
root_fibo
 
Unit 6 bash shell
root_fibo
 
Unit 5 vim an advanced text editor
root_fibo
 
Unit 4 user and group
root_fibo
 
Unit2 help
root_fibo
 

Recently uploaded (20)

PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
The Future of AI & Machine Learning.pptx
pritsen4700
 

Unit 7 standard i o

  • 1. RedHat Enterprise Linux Essential Unit 7: Standard I/O and Pipes
  • 2. Objectives Upon completion of this unit, you should be able to: Redirect I/O channels to files Connect commands using pipes Use the for loops to iterate over sets of values
  • 3. Standard Input and Output Linux provides three I/O channels to Programs  Standard input (STDIN) - keyboard by default (0)  Standard output (STDOUT) - terminal window by default (1)  Standard error (STDERR) - terminal window by default (2) ls –l myfile nofile
  • 4. 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.
  • 5. Redirecting Output to a File  command > file - Direct standard output of command to file  command >> file - Append standard output of command to file  command < file - Send file as input to command  command 2> file - Redirect error messages from command to file  command 2>> file - Append error messages from command to file
  • 6. Redirecting Output to a File Examples  This command generates output and errors when run as non- root:  $ find /etc -name passwd  Operators can be used to store output and errors:  $ find /etc -name passwd > find.out  $ find /etc -name passwd 2> /dev/null  $ find /etc -name passwd > find.out 2> find.err
  • 7. 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
  • 8. 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 email: $ echo "test email" | mail -s "test" student
  • 9. Combining Output and Errors  Some operators affect both STDOUT and STDERR  &>: Redirects all output: $ find /etc -name passwd &> find.all  2>&1: Redirects STDERR to STDOUT Useful for sending all output through a pipe $ find /etc -name passwd 2>&1 | less  (): Combines STDOUTs of multiple programs $ ( cal 2007 ; cal 2008 ) | less
  • 10. 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 ls -lR /etc | tee stage1.out | sort | tee stage2.out | uniq -c | tee stage3.out | sort -r | tee stage4.out | less
  • 11. 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' < .bash_profile  This command will translate the uppercase characters in .bash_profile to lowercase  Equivalent to: $ cat .bash_profile | tr 'A-Z' 'a-z'
  • 12. 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 see" student@localhost <<END > Hi all, >I’m Tam > END
  • 13. Scripting: for loops Performs actions on each member of a set of values Example: for NAME in joe jane julie do ADDRESS="[email protected]" MESSAGE='Projects are due today!' echo $MESSAGE | mail -s Reminder $ADDRESS done
  • 14. Scripting: for loops continued  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
  • 15. example #!/bin/bash # alive2.sh # Checks to see if hosts 192.168.0.1-192.168.0.20 are alive # Iterate through IP addresses for n in {1..20}; do host=192.168.0.$n ping -c2 $host &> /dev/null if [ $? = 0 ]; then echo "$host is UP" else echo "$host is DOWN" fi done