Solved

Pipe operator confusion!

Posted on 2014-03-19
8
267 Views
Last Modified: 2014-03-19
Ah hello.

I am reading a Linux book which gives the following example command

ls | diff /dev/fd/0 oldfilelist

It apparently will compare the output of ls to that stored in oldfilelist.  I understand that /dev/fd/0 is stdout, but I don't quite understand how it ties in with the use of the pipe.  

1) Can someone please clarify how this is working?

2) When I type

ls |

I get a single > prompt.  I have tried to find out what this means/what is happening but have failed.  Can someone also clarify this please?

TIA
0
Comment
Question by:mrwad99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 19

Assisted Solution

by:Ken Butters
Ken Butters earned 200 total points
ID: 39940102
The pipe redirects the output from the command prior to the pipe, as input to the command following the pipe.

In your case, the output of "ls" is redirected as input to the next command which is the "diff"

if you type "ls |"  you are saying to redirect the output of "ls" command, but not saying where to redirect the output to... so it is lost.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 39940119
Correction: /dev/fd/0 is stdin not, stdout.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 39940120
Thanks Ken, and sorry because my question was unclear.  I understand that the pipe is for redirection, but what I don't understand is how the output from 'ls' suddenly gets treated as the first argument to diff, which is in fact standard input!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 50 total points
ID: 39940148
Think in terms of inputs and outputs: the pipe takes the output of the command on the left and serves it as input to the command on the right of the pipe.

HTH,
Dan
0
 
LVL 19

Accepted Solution

by:
Ken Butters earned 200 total points
ID: 39940161
If I understand what you are asking... that is the whole purpose of Pipe...to redirect the stdout to the stdin.

"ls" creates a list of files which are directed to stdout
the pipe command redirects all of the stdout of the "ls" to stdin of the next command
The diff command is using "/dev/fd/0" to tell where the stdin comes into play
0
 
LVL 19

Author Comment

by:mrwad99
ID: 39940222
Argh I typed a huge comment then but accidentally navigated away and lost it!  Oh well..

In a nutshell, my confusion came from the fact that I have never had to explicitly refer to the input stream before, hence I was not seeing that /dev/fd/0 as an argument to a command after a | is in fact the output of the command prior to |.

Obvious now I know :)

I have figured out my second question: it seems that the prompt is waiting for me to enter whatever I want on the right hand side of the pipe: if I type ls -l, then hit enter, then type "more", it is as if I typed "ls -l | more" outright.

Thanks both for providing input :)
0
 

Expert Comment

by:TerryZumwalt
ID: 39940225
I do not believe that "ls |" is doing anything extra for you.   You can get the same results with just "diff item1 item2" as you will with " ls | diff item1 item2. " 

When you use the pipe operator the system expects to see a command or string after the pipe.  If you do not enter any information after the pipe and press enter, then you are prompt to enter it which is why you get the > .

1. For example, if you enter "ls |" and press enter your will get the >.
2. Then enter diff and your files to compare:  > "diff item1 item2"  then press enter.
3. Your second set of input completed the initial pipe requirements and you will see a list if your info was entered correctly or an error if not.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 39940253
Thanks Terry, and welcome to EE.

Shame, if I was a bit slower you would have gotten your first points for that explanation...

The purpose of the "ls |" is to compare the current contents of the working directory with those stored in oldfilelist, as a mechanism for seeing what has changed in the directory without having to output the current directory contents first.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
RHEL6 + dockers - No route to host 7 104
WMIC Output Formatting 1 85
Merging two files with Perl 5 60
bash parse log file and split according to date 5 29
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question