SlideShare una empresa de Scribd logo
1 de 10
Standard I/O and Pipes
File Descriptors
• Linux architecture relays on files as the primary mean of
communication between the different processes and the user
interface.
• Each process has it’s own list of open files. Every open file is
represented as a ‘file descriptor’ - a numerical ID related to a
specific file opened by this process.
• The file descriptor keeps track of the location in the file where
the reading or writing took place and advances it as with
every character read or written.
File Descriptors and Streams
• The first three file descriptor for each process has a
conventional meaning and default binding to the terminal.
 Standard Input: designated “STDIN” and/or the number “0”; this
descriptor is responsible for accepting data
 Standard Output: designated “STDOUT” and/or the number “1”; this
descriptor is responsible for sending out normal data
 Standard Error: designated “STDERR” and/or the number “2”; this
descriptor is responsible for sending out error data
Note: Most processed use standard text as the default data type, but this
is not always true.
Try this running this command: cat /bin/ls
Note: Terminal is considered a device by the system. It is represented as a
device file and can be read from and written to.
Redirecting STDOUT
• We can use the “>” or “1>”mark to redirect STDOUT to a file.
• “>” is a meta-character which tells the shell to create the
redirection before the program starts, that way when it does
– the STDOUT is already set to be a file, hence the output will
go there and not be displayed on-screen.
• When redirecting output with “>”, any existing content in the
file we redirect to will be completely overwritten.
• Redirection of STDOUT does not affect STDIN or STDERR;
errors will keep showing up on the screen by default.
# ls -l file? > file_list
# cat file_list
-rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1
-rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2
-rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3
# ls -l file? > file_list
# cat file_list
-rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1
-rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2
-rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3
Appending and Overwrite
protection
• In order to append the redirected output to an existing file,
rather than overwriting it completely, we’d use the “>>”
marks:
• We can use a BASH option named “noclobber” to prevent
files from being overwritten during redirection by running:
 set –o noclobber
# ls -l [dk]* >> file_list
# cat file_list
-rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1
-rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2
-rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile1
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile2
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 kfile9
# ls -l [dk]* >> file_list
# cat file_list
-rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1
-rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2
-rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile1
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile2
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 kfile9
Redirecting STDERR
• As discussed before, STDERR is also represented by the
number “2”.
• In order to redirect STDERR to a file, we’d use “2>”:
• Redirection of STDERR is not affected by redirections of
STDOUT, each is a completely separate stream.
• At times we do not want the STDOUT or STDERR to be
redirected anywhere, whether it’s the display or a file, we can
redirect either of the streams or both of them into a device
file named /dev/null
• Anything redirected to /dev/null will be discarded for good.
# sdtrre 2> error_file
# cat error_file
-bash: sdtrre: command not found
# sdtrre 2> error_file
# cat error_file
-bash: sdtrre: command not found
Redirecting STDIN
• Input redirections are much less common than STDOUT or STDERR
redirections because many applications already take their input by default
from the keyboard or a file.
• The “<“ mark is used to redirect input to a command from a file rather
than from the keyboard
# cat input_redirect
/tmp/test/
# ls -l < input_redirect
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile1
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile2
drwxrwxr-x 2 nir nir 4096 Jul 19 13:58 directory
-rw-rw-r-- 1 nir nir 33 Jul 20 10:53 error_file
-rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1
-rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2
-rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3
-rw-rw-r-- 1 nir nir 336 Jul 20 10:47 file_list
-rw-rw-r-- 1 nir nir 11 Jul 20 11:10 input_redirect
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 kfile9
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 mfile1
# cat input_redirect
/tmp/test/
# ls -l < input_redirect
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile1
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile2
drwxrwxr-x 2 nir nir 4096 Jul 19 13:58 directory
-rw-rw-r-- 1 nir nir 33 Jul 20 10:53 error_file
-rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1
-rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2
-rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3
-rw-rw-r-- 1 nir nir 336 Jul 20 10:47 file_list
-rw-rw-r-- 1 nir nir 11 Jul 20 11:10 input_redirect
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 kfile9
-rw-rw-r-- 1 nir nir 0 Jul 19 15:11 mfile1
Multiple Redirection
• Redirections can be merged so that we can redirect multiple
streams in a single action.
• In the following example, we will see how to merge both
STDOUT and STDERR and send them to /dev/null to be
discarded
• The above example redirects STDOUT (>) to /dev/null and
then binds STDERR to the same fate of STDOUT (2>&1)
Note: ‘/dev/null’ is a special device file. Data that is written into
this file gets discarded immediately
# date > /dev/null 2>&1# date > /dev/null 2>&1
Pipes
• The Pipe “|” meta-character, indicated to Bash that the
STDOUT of one command should be automatically passed as
STDIN to another command
command | command | command …
Pipes
• The Pipe “|” meta-character, indicated to Bash that the
STDOUT of one command should be automatically passed as
STDIN to another command
command | command | command …

Más contenido relacionado

La actualidad más candente

Kernel Recipes 2017: Performance Analysis with BPF
Kernel Recipes 2017: Performance Analysis with BPFKernel Recipes 2017: Performance Analysis with BPF
Kernel Recipes 2017: Performance Analysis with BPFBrendan Gregg
 
Hachioji.pm in Machida の LT
Hachioji.pm in Machida の LTHachioji.pm in Machida の LT
Hachioji.pm in Machida の LTMasahiro Honma
 
101 3.5 create, monitor and kill processes
101 3.5 create, monitor and kill processes101 3.5 create, monitor and kill processes
101 3.5 create, monitor and kill processesAcácio Oliveira
 
Linux Commands - 3
Linux Commands - 3Linux Commands - 3
Linux Commands - 3Kanchilug
 
Linux tech talk
Linux tech talkLinux tech talk
Linux tech talkPrince Raj
 
3.3 perform basic file management
3.3 perform basic file management3.3 perform basic file management
3.3 perform basic file managementAcácio Oliveira
 
Deeper dive in Docker Overlay Networks
Deeper dive in Docker Overlay NetworksDeeper dive in Docker Overlay Networks
Deeper dive in Docker Overlay NetworksLaurent Bernaille
 

La actualidad más candente (11)

Kernel Recipes 2017: Performance Analysis with BPF
Kernel Recipes 2017: Performance Analysis with BPFKernel Recipes 2017: Performance Analysis with BPF
Kernel Recipes 2017: Performance Analysis with BPF
 
Tsig 17022011
Tsig 17022011Tsig 17022011
Tsig 17022011
 
Log
LogLog
Log
 
Hachioji.pm in Machida の LT
Hachioji.pm in Machida の LTHachioji.pm in Machida の LT
Hachioji.pm in Machida の LT
 
101 3.5 create, monitor and kill processes
101 3.5 create, monitor and kill processes101 3.5 create, monitor and kill processes
101 3.5 create, monitor and kill processes
 
Log
LogLog
Log
 
Linux Commands - 3
Linux Commands - 3Linux Commands - 3
Linux Commands - 3
 
Log
LogLog
Log
 
Linux tech talk
Linux tech talkLinux tech talk
Linux tech talk
 
3.3 perform basic file management
3.3 perform basic file management3.3 perform basic file management
3.3 perform basic file management
 
Deeper dive in Docker Overlay Networks
Deeper dive in Docker Overlay NetworksDeeper dive in Docker Overlay Networks
Deeper dive in Docker Overlay Networks
 

Similar a 05 standard io_and_pipes

Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commandsSagar Kumar
 
Linux fundamental - Chap 12 Hardware Management
Linux fundamental - Chap 12 Hardware ManagementLinux fundamental - Chap 12 Hardware Management
Linux fundamental - Chap 12 Hardware ManagementKenny (netman)
 
Advanced administration and problem determination
Advanced administration and problem determinationAdvanced administration and problem determination
Advanced administration and problem determinationsolarisyougood
 
Writing Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxWriting Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxRajKumar Rampelli
 
Unmasking Careto through Memory Forensics (video in description)
Unmasking Careto through Memory Forensics (video in description)Unmasking Careto through Memory Forensics (video in description)
Unmasking Careto through Memory Forensics (video in description)Andrew Case
 
Linux fundamental - Chap 01 io
Linux fundamental - Chap 01 ioLinux fundamental - Chap 01 io
Linux fundamental - Chap 01 ioKenny (netman)
 
Fail2ban - the system security for green hand -on linux os
Fail2ban  - the system security  for green hand -on linux osFail2ban  - the system security  for green hand -on linux os
Fail2ban - the system security for green hand -on linux osSamina Fu (Shan Jung Fu)
 
Computer technicians-quick-reference-guide
Computer technicians-quick-reference-guideComputer technicians-quick-reference-guide
Computer technicians-quick-reference-guideShathees Rao
 
PLNOG15 - IRR Lockdown - Job Snijders
PLNOG15 - IRR Lockdown - Job SnijdersPLNOG15 - IRR Lockdown - Job Snijders
PLNOG15 - IRR Lockdown - Job SnijdersPROIDEA
 
Simplest-Ownage-Human-Observed… - Routers
 Simplest-Ownage-Human-Observed… - Routers Simplest-Ownage-Human-Observed… - Routers
Simplest-Ownage-Human-Observed… - RoutersLogicaltrust pl
 
Filip palian mateuszkocielski. simplest ownage human observed… routers
Filip palian mateuszkocielski. simplest ownage human observed… routersFilip palian mateuszkocielski. simplest ownage human observed… routers
Filip palian mateuszkocielski. simplest ownage human observed… routersYury Chemerkin
 
Jackpot! Attacking Arcade Machines
Jackpot! Attacking Arcade MachinesJackpot! Attacking Arcade Machines
Jackpot! Attacking Arcade MachinesPatrick Sayler
 
How to convert your Linux box into Security Gateway - Part 1
How to convert your Linux box into Security Gateway - Part 1How to convert your Linux box into Security Gateway - Part 1
How to convert your Linux box into Security Gateway - Part 1n|u - The Open Security Community
 
stackconf 2020 | Speeding up Linux disk encryption by Ignat Korchagin
stackconf 2020 | Speeding up Linux disk encryption by Ignat Korchaginstackconf 2020 | Speeding up Linux disk encryption by Ignat Korchagin
stackconf 2020 | Speeding up Linux disk encryption by Ignat KorchaginNETWAYS
 
Rtfm_ Red Team Field Manual ( PDFDrive ).pdf
Rtfm_ Red Team Field Manual ( PDFDrive ).pdfRtfm_ Red Team Field Manual ( PDFDrive ).pdf
Rtfm_ Red Team Field Manual ( PDFDrive ).pdfMacMelter
 

Similar a 05 standard io_and_pipes (20)

Linux And perl
Linux And perlLinux And perl
Linux And perl
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
Iptables presentation
Iptables presentationIptables presentation
Iptables presentation
 
Linux fundamental - Chap 12 Hardware Management
Linux fundamental - Chap 12 Hardware ManagementLinux fundamental - Chap 12 Hardware Management
Linux fundamental - Chap 12 Hardware Management
 
Linux Hardening - nullhyd
Linux Hardening - nullhydLinux Hardening - nullhyd
Linux Hardening - nullhyd
 
Advanced administration and problem determination
Advanced administration and problem determinationAdvanced administration and problem determination
Advanced administration and problem determination
 
Writing Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxWriting Character driver (loadable module) in linux
Writing Character driver (loadable module) in linux
 
Unmasking Careto through Memory Forensics (video in description)
Unmasking Careto through Memory Forensics (video in description)Unmasking Careto through Memory Forensics (video in description)
Unmasking Careto through Memory Forensics (video in description)
 
Linux fundamental - Chap 01 io
Linux fundamental - Chap 01 ioLinux fundamental - Chap 01 io
Linux fundamental - Chap 01 io
 
Fail2ban - the system security for green hand -on linux os
Fail2ban  - the system security  for green hand -on linux osFail2ban  - the system security  for green hand -on linux os
Fail2ban - the system security for green hand -on linux os
 
Computer technicians-quick-reference-guide
Computer technicians-quick-reference-guideComputer technicians-quick-reference-guide
Computer technicians-quick-reference-guide
 
PLNOG15 - IRR Lockdown - Job Snijders
PLNOG15 - IRR Lockdown - Job SnijdersPLNOG15 - IRR Lockdown - Job Snijders
PLNOG15 - IRR Lockdown - Job Snijders
 
Simplest-Ownage-Human-Observed… - Routers
 Simplest-Ownage-Human-Observed… - Routers Simplest-Ownage-Human-Observed… - Routers
Simplest-Ownage-Human-Observed… - Routers
 
Filip palian mateuszkocielski. simplest ownage human observed… routers
Filip palian mateuszkocielski. simplest ownage human observed… routersFilip palian mateuszkocielski. simplest ownage human observed… routers
Filip palian mateuszkocielski. simplest ownage human observed… routers
 
Jackpot! Attacking Arcade Machines
Jackpot! Attacking Arcade MachinesJackpot! Attacking Arcade Machines
Jackpot! Attacking Arcade Machines
 
How to convert your Linux box into Security Gateway - Part 1
How to convert your Linux box into Security Gateway - Part 1How to convert your Linux box into Security Gateway - Part 1
How to convert your Linux box into Security Gateway - Part 1
 
OS_lab_file.pdf
OS_lab_file.pdfOS_lab_file.pdf
OS_lab_file.pdf
 
Linux
LinuxLinux
Linux
 
stackconf 2020 | Speeding up Linux disk encryption by Ignat Korchagin
stackconf 2020 | Speeding up Linux disk encryption by Ignat Korchaginstackconf 2020 | Speeding up Linux disk encryption by Ignat Korchagin
stackconf 2020 | Speeding up Linux disk encryption by Ignat Korchagin
 
Rtfm_ Red Team Field Manual ( PDFDrive ).pdf
Rtfm_ Red Team Field Manual ( PDFDrive ).pdfRtfm_ Red Team Field Manual ( PDFDrive ).pdf
Rtfm_ Red Team Field Manual ( PDFDrive ).pdf
 

Más de Shay Cohen

Linux Performance Tunning Memory
Linux Performance Tunning MemoryLinux Performance Tunning Memory
Linux Performance Tunning MemoryShay Cohen
 
Linux Performance Tunning Kernel
Linux Performance Tunning KernelLinux Performance Tunning Kernel
Linux Performance Tunning KernelShay Cohen
 
Linux Performance Tunning introduction
Linux Performance Tunning introductionLinux Performance Tunning introduction
Linux Performance Tunning introductionShay Cohen
 
chroot and SELinux
chroot and SELinuxchroot and SELinux
chroot and SELinuxShay Cohen
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/CoreShay Cohen
 
Infra / Cont delivery - 3rd party automation
Infra / Cont delivery - 3rd party automationInfra / Cont delivery - 3rd party automation
Infra / Cont delivery - 3rd party automationShay Cohen
 
14 network tools
14 network tools14 network tools
14 network toolsShay Cohen
 
13 process management
13 process management13 process management
13 process managementShay Cohen
 
12 linux archiving tools
12 linux archiving tools12 linux archiving tools
12 linux archiving toolsShay Cohen
 
11 linux filesystem copy
11 linux filesystem copy11 linux filesystem copy
11 linux filesystem copyShay Cohen
 
10 finding files
10 finding files10 finding files
10 finding filesShay Cohen
 
08 text processing_tools
08 text processing_tools08 text processing_tools
08 text processing_toolsShay Cohen
 
07 vi text_editor
07 vi text_editor07 vi text_editor
07 vi text_editorShay Cohen
 
06 users groups_and_permissions
06 users groups_and_permissions06 users groups_and_permissions
06 users groups_and_permissionsShay Cohen
 
04 using and_configuring_bash
04 using and_configuring_bash04 using and_configuring_bash
04 using and_configuring_bashShay Cohen
 
03 browsing the filesystem
03 browsing the filesystem03 browsing the filesystem
03 browsing the filesystemShay Cohen
 
02 linux desktop usage
02 linux desktop usage02 linux desktop usage
02 linux desktop usageShay Cohen
 
09 string processing_with_regex copy
09 string processing_with_regex copy09 string processing_with_regex copy
09 string processing_with_regex copyShay Cohen
 
01 linux history overview
01 linux history overview01 linux history overview
01 linux history overviewShay Cohen
 

Más de Shay Cohen (19)

Linux Performance Tunning Memory
Linux Performance Tunning MemoryLinux Performance Tunning Memory
Linux Performance Tunning Memory
 
Linux Performance Tunning Kernel
Linux Performance Tunning KernelLinux Performance Tunning Kernel
Linux Performance Tunning Kernel
 
Linux Performance Tunning introduction
Linux Performance Tunning introductionLinux Performance Tunning introduction
Linux Performance Tunning introduction
 
chroot and SELinux
chroot and SELinuxchroot and SELinux
chroot and SELinux
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/Core
 
Infra / Cont delivery - 3rd party automation
Infra / Cont delivery - 3rd party automationInfra / Cont delivery - 3rd party automation
Infra / Cont delivery - 3rd party automation
 
14 network tools
14 network tools14 network tools
14 network tools
 
13 process management
13 process management13 process management
13 process management
 
12 linux archiving tools
12 linux archiving tools12 linux archiving tools
12 linux archiving tools
 
11 linux filesystem copy
11 linux filesystem copy11 linux filesystem copy
11 linux filesystem copy
 
10 finding files
10 finding files10 finding files
10 finding files
 
08 text processing_tools
08 text processing_tools08 text processing_tools
08 text processing_tools
 
07 vi text_editor
07 vi text_editor07 vi text_editor
07 vi text_editor
 
06 users groups_and_permissions
06 users groups_and_permissions06 users groups_and_permissions
06 users groups_and_permissions
 
04 using and_configuring_bash
04 using and_configuring_bash04 using and_configuring_bash
04 using and_configuring_bash
 
03 browsing the filesystem
03 browsing the filesystem03 browsing the filesystem
03 browsing the filesystem
 
02 linux desktop usage
02 linux desktop usage02 linux desktop usage
02 linux desktop usage
 
09 string processing_with_regex copy
09 string processing_with_regex copy09 string processing_with_regex copy
09 string processing_with_regex copy
 
01 linux history overview
01 linux history overview01 linux history overview
01 linux history overview
 

Último

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 

Último (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

05 standard io_and_pipes

  • 2. File Descriptors • Linux architecture relays on files as the primary mean of communication between the different processes and the user interface. • Each process has it’s own list of open files. Every open file is represented as a ‘file descriptor’ - a numerical ID related to a specific file opened by this process. • The file descriptor keeps track of the location in the file where the reading or writing took place and advances it as with every character read or written.
  • 3. File Descriptors and Streams • The first three file descriptor for each process has a conventional meaning and default binding to the terminal.  Standard Input: designated “STDIN” and/or the number “0”; this descriptor is responsible for accepting data  Standard Output: designated “STDOUT” and/or the number “1”; this descriptor is responsible for sending out normal data  Standard Error: designated “STDERR” and/or the number “2”; this descriptor is responsible for sending out error data Note: Most processed use standard text as the default data type, but this is not always true. Try this running this command: cat /bin/ls Note: Terminal is considered a device by the system. It is represented as a device file and can be read from and written to.
  • 4. Redirecting STDOUT • We can use the “>” or “1>”mark to redirect STDOUT to a file. • “>” is a meta-character which tells the shell to create the redirection before the program starts, that way when it does – the STDOUT is already set to be a file, hence the output will go there and not be displayed on-screen. • When redirecting output with “>”, any existing content in the file we redirect to will be completely overwritten. • Redirection of STDOUT does not affect STDIN or STDERR; errors will keep showing up on the screen by default. # ls -l file? > file_list # cat file_list -rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1 -rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2 -rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3 # ls -l file? > file_list # cat file_list -rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1 -rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2 -rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3
  • 5. Appending and Overwrite protection • In order to append the redirected output to an existing file, rather than overwriting it completely, we’d use the “>>” marks: • We can use a BASH option named “noclobber” to prevent files from being overwritten during redirection by running:  set –o noclobber # ls -l [dk]* >> file_list # cat file_list -rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1 -rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2 -rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3 -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile1 -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile2 -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 kfile9 # ls -l [dk]* >> file_list # cat file_list -rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1 -rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2 -rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3 -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile1 -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile2 -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 kfile9
  • 6. Redirecting STDERR • As discussed before, STDERR is also represented by the number “2”. • In order to redirect STDERR to a file, we’d use “2>”: • Redirection of STDERR is not affected by redirections of STDOUT, each is a completely separate stream. • At times we do not want the STDOUT or STDERR to be redirected anywhere, whether it’s the display or a file, we can redirect either of the streams or both of them into a device file named /dev/null • Anything redirected to /dev/null will be discarded for good. # sdtrre 2> error_file # cat error_file -bash: sdtrre: command not found # sdtrre 2> error_file # cat error_file -bash: sdtrre: command not found
  • 7. Redirecting STDIN • Input redirections are much less common than STDOUT or STDERR redirections because many applications already take their input by default from the keyboard or a file. • The “<“ mark is used to redirect input to a command from a file rather than from the keyboard # cat input_redirect /tmp/test/ # ls -l < input_redirect -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile1 -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile2 drwxrwxr-x 2 nir nir 4096 Jul 19 13:58 directory -rw-rw-r-- 1 nir nir 33 Jul 20 10:53 error_file -rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1 -rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2 -rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3 -rw-rw-r-- 1 nir nir 336 Jul 20 10:47 file_list -rw-rw-r-- 1 nir nir 11 Jul 20 11:10 input_redirect -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 kfile9 -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 mfile1 # cat input_redirect /tmp/test/ # ls -l < input_redirect -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile1 -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 dfile2 drwxrwxr-x 2 nir nir 4096 Jul 19 13:58 directory -rw-rw-r-- 1 nir nir 33 Jul 20 10:53 error_file -rw-rw-r-- 1 nir nir 135 Jul 19 13:42 file1 -rwxrwxr-- 1 nir nir 35 Jul 19 13:42 file2 -rw-rw-r-- 1 nir nir 200 Jul 19 13:42 file3 -rw-rw-r-- 1 nir nir 336 Jul 20 10:47 file_list -rw-rw-r-- 1 nir nir 11 Jul 20 11:10 input_redirect -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 kfile9 -rw-rw-r-- 1 nir nir 0 Jul 19 15:11 mfile1
  • 8. Multiple Redirection • Redirections can be merged so that we can redirect multiple streams in a single action. • In the following example, we will see how to merge both STDOUT and STDERR and send them to /dev/null to be discarded • The above example redirects STDOUT (>) to /dev/null and then binds STDERR to the same fate of STDOUT (2>&1) Note: ‘/dev/null’ is a special device file. Data that is written into this file gets discarded immediately # date > /dev/null 2>&1# date > /dev/null 2>&1
  • 9. Pipes • The Pipe “|” meta-character, indicated to Bash that the STDOUT of one command should be automatically passed as STDIN to another command command | command | command …
  • 10. Pipes • The Pipe “|” meta-character, indicated to Bash that the STDOUT of one command should be automatically passed as STDIN to another command command | command | command …

Notas del editor

  1. Exercise: ls -l file nosuchfile