The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

unix pipe

Feeling stupid, I am asking you...
Why this doesn't work:

$which some_file|file

? (in bash)
wednesday
Wednesday, January 04, 2006
 
 
Well, if the final 'file' is a filename you're trying to write, and not the name of an executable, you're trying to pipe data to something that has no STDIN to recieve it.

That's what ">" is for.
AllanL5
Wednesday, January 04, 2006
 
 
no, "file" as a command, to determine type of the file
wednesday
Wednesday, January 04, 2006
 
 
The 'file' command doesn't expect to get the file_spec string on STDIN, it expects it on the command line, no?

So you're trying to pipe the file_spec to a place 'file' is not looking for it.  Thus it's not going to work.
AllanL5
Wednesday, January 04, 2006
 
 
which some_file | xargs file
Matt.
Wednesday, January 04, 2006
 
 
I thought "which file_name" writes to STDOUT and the pipe pipes it in STDIN, or ... ?
wednesday
Wednesday, January 04, 2006
 
 
Yes, "which filename" does send its output to STDOUT.  The "|" symbol does pipe its STDOUT to the next command's STDIN.

The problem is, the 'next' command is 'file'.  'file' doesn't take its input from STDIN, it takes it from the command line.  So your series of commands breaks at that point.

You CAN have 'which filename' create a file with its results, then start up 'file -f listfile' to check the types of the filenames written to listfile.
AllanL5
Wednesday, January 04, 2006
 
 
Ooh, see matt's answer.  'xargs' purpose in life is apparently to convert STDIN stuff to command line stuff.  Neat.
AllanL5
Wednesday, January 04, 2006
 
 
Ok, thanx guys, got it.
wednesday
Wednesday, January 04, 2006
 
 
which some_executable  | file -f -
argon
Wednesday, January 04, 2006
 
 
Or use backquote, which just runs the command within and inserts it as text:

file `which somefile`
Dan Maas
Thursday, January 05, 2006
 
 
> 'xargs' purpose in life is apparently to convert STDIN stuff to command line stuff.

I found out about xargs earlier this year. Now I look back at all the silly stuff I did before to handle large groups of files and shake my head, all replaced with:

find [find's args] | xargs something [something's args]

Only time there is a problem is when you don't want multiple executions of "something" and you have a huge number of arguments to it.

Remember that xargs chops up the incoming argument list depending on your operating system's limits on command line length, and executes "something" once on each chunk.
dot for this one
Friday, January 06, 2006
 
 
An alternative to xargs:

In a Bourne shell or workalike (bash, ksh, but not [t]csh):

for i in `find . -name whatever ...`; do
  # anything you want
done

e.g. for the "file" example above:

for i in `find . -print`; do
  file $i
done
David Jones Send private email
Sunday, January 08, 2006
 
 
Or the slightly more esoteric

find somefile -print | while read line
do
    file $line
done

which avoids size limitations of backtics.

You can even pipe out -

find somefile -print | while read line
do
    file $line
done | grep "ASCII"
shell esoterica
Thursday, January 26, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz