Recent Posts

ads

Shell features , pipes and redirection


Shells  features, pipes and redirection



Pipe is used to pass output to another program or utility.

Redirect is used to pass output to either a file or stream.



Redirection is a mechanism whereby the output of a command can be fed either to some other device (eg a printer or file) or to another program or command.
Redirects a command's output from the "standard output device" (usually the monitor) to another device (eg printer) or a file.

Syntax:

To redirect output to a device:
Command > Device
To redirect output to a file:
Command > Filename


Every process has three channels, the three main file streams : StandardIn, StandardOut and Standard error.Here is the representation

Now let me explain you what standard output , standard input and standard error




As you can see the Error output is different than the Normal output


Now we will show how to redirect to a statement to a file

as you could see in the blue statement it overwrote the previous  file

So, ">" send standard output to a file  and overwrite it if it exit

We use ">>" if we want to add to that file we will send standard >> output to a file  and append it if it exist





There is a way to avoid that..




You will use it if  you think you already have a file,  and we could  overwrite something

We could see that the file "mywonderfulfile " was created on desktop







  • So, to make everything clear , STDOUT = 1 (the default one),so we don't need to write it all the time 
  • STDERR =2
as you could see here we use the 2> to show as a standard error and it's already redirected to myerror.txt




Here you will see how the information is going to the file

But in some case the "<" can be pointing out to theinformation to the command on the left






The "pipe" redirects the output of a program or command to a second program or command.

To make it more simple,basically the output of one commands is the input to another

Syntax:

Command1 | Command2

So these are the files that we have in Desktop





Now you wonder , How many files do you have in  this directory?
We can do that in different ways

  1. First let me explain. The wc (word count) command in Linux operating systems is used to find out number of newline count, word count,  in a files specified by the file arguments.
  2. ls -1 put all files everything in a single line


We used the output of "ls -1  "  and we made the input of " WC"





the command is ls -1 | wc - 1


The pipe is a simple but powerful tool if you combine it with multiples commands

There is another command: Tee



Tee redirect STOUT but also show it in command line







So, in this charter we understood STDIN, STDOUT,STDERR, redirections, piping and Tee.

If it was useful, please click like.. Thanks
Shell features , pipes and redirection Shell features , pipes and redirection Reviewed by ohhhvictor on August 04, 2018 Rating: 5

No comments:

Facebook

ads
Powered by Blogger.