standard input to rmdir?

standard input to rmdir?

Post by Andy Thoma » Thu, 05 Nov 1998 04:00:00



Is there a way I can do something like:

        rmdir < cat dirlist

where dirlist is a list of directories to be deleted? The GNU version of
rmdir won't allow filenames as arguments.

I could write a shell script to do this but there must be an easier way.
(the shell is bash by the way, on a Linux system).

Andy

 
 
 

standard input to rmdir?

Post by Barry Margoli » Thu, 05 Nov 1998 04:00:00




Quote:>Is there a way I can do something like:

>    rmdir < cat dirlist

>where dirlist is a list of directories to be deleted? The GNU version of
>rmdir won't allow filenames as arguments.

Most programs don't take a list of filenames from stdin.  I don't know why
people seem to assume they can do things like the above.  To do what you
want, use:

rmdir `cat dirlist`

or

xargs rmdir < dirlist

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.

 
 
 

standard input to rmdir?

Post by Clinton Pier » Thu, 05 Nov 1998 04:00:00


On Wed, 4 Nov 1998 15:56:11 +0000, Andy Thomas


>Is there a way I can do something like:

>    rmdir < cat dirlist

>where dirlist is a list of directories to be deleted? The GNU version of
>rmdir won't allow filenames as arguments.

>I could write a shell script to do this but there must be an easier way.
>(the shell is bash by the way, on a Linux system).

>Andy

try:

        rmdir `cat dirlist`

The backticks evaluate "cat dirlist" as a shell command, and return the
values in-place.  rmdir then only "sees" the list of directories to
delete.  Voila!

If you have a command that does not take multiple arguments (which is
not the case here), or you have a LOT of arguments (thousands), look
into the "xargs" command.  It takes a stream of input and executes an
arbitrary command against that stream.

 
 
 

standard input to rmdir?

Post by Clinton Pier » Thu, 05 Nov 1998 04:00:00






>>Is there a way I can do something like:

>>        rmdir < cat dirlist

>>where dirlist is a list of directories to be deleted? The GNU version of
>>rmdir won't allow filenames as arguments.

>[...] I don't know why people seem to assume they can do things like the above.
>[...]

Because newbies having seen the magical behaviours of things like:

        grep pattern file
        grep pattern < file

Or even:

        grep pattern file1 file2 file3
        cat file1 file2 file3 | grep pattern

Without having the subtle differences explained assume that, "wherever a
utility needs a list of filename arguments, the contents of files can be

shoveled in as STDIN, and the utility will do the Right Thing."  This is
wrong, and they're mixing apples and oranges[1] but it sounds right if
you say it enough times.

I've seen this a lot in teaching UNIX to beginners.  It's common, and
easily messed up.[2]  Redirection, in-place substituion, the many-faced
behaviour of the Standard UNIX Filters, and the fact that the shell
manages some, but not all, of this beautiful dance is a prime source for
confusion.

[1] The apples of course being "the contents of the files" and the
oranges being "the names of the files".  Otherwise, they're completely
correct.[3]

[2] The subtlety being of course the difference between standard
"filter" behaviour and other utility behaviour.

[3] The usual way of expelling the demon is to put this on the
whiteboard:

        wc file
        wc < file
        wc < cat file     (or even "wc < `cat file`")

And spending a while explaining why #3 doesn't work as they would
hope.

 
 
 

standard input to rmdir?

Post by mc.. » Thu, 05 Nov 1998 04:00:00


: Is there a way I can do something like:

:       rmdir < cat dirlist

: where dirlist is a list of directories to be deleted? The GNU version of
: rmdir won't allow filenames as arguments.

: I could write a shell script to do this but there must be an easier way.
: (the shell is bash by the way, on a Linux system).

My guess is that xargs will do what you want.
xargs rmdir < dirlist

BTW, the cat construction is wrong even if rmdir took arguments on  STDIN.
-Mike

--
Michael Cope: Harvey Mudd College '00; Armand Hammer UWC '96

 
 
 

standard input to rmdir?

Post by Andy Thoma » Fri, 06 Nov 1998 04:00:00



> On Wed, 4 Nov 1998 15:56:11 +0000, Andy Thomas

> >Is there a way I can do something like:

> >       rmdir < cat dirlist

> >where dirlist is a list of directories to be deleted? The GNU version of
> >rmdir won't allow filenames as arguments.

> >I could write a shell script to do this but there must be an easier way.
> >(the shell is bash by the way, on a Linux system).

> >Andy

> try:

>    rmdir `cat dirlist`

> The backticks evaluate "cat dirlist" as a shell command, and return the
> values in-place.  rmdir then only "sees" the list of directories to
> delete.  Voila!

Thanks, this in fact worked!

I'd come across the idea of putting expressions within `...` quotes a
couple of years ago but had forgotten the difference between ' and `. (By
the way, what is the ` character called? Backticks?). My O'Reilly book on
Bash simply says `cat dirlist` is an archaic form of command substitution
and makes no other mention of this syntax!

Quote:> > If you have a command that does not take multiple arguments (which is
> not the case here), or you have a LOT of arguments (thousands), look
> into the "xargs" command.  It takes a stream of input and executes an
> arbitrary command against that stream.

I'll remember that for next time.

Thanks for jogging my memory,

Andy

 
 
 

standard input to rmdir?

Post by Thomas L|fgre » Sat, 07 Nov 1998 04:00:00



    >> [...]
    >> rmdir `cat dirlist`
    >>
    >> The backticks evaluate "cat dirlist" as a shell command, and
    >> return the values in-place.  rmdir then only "sees" the list of
    >> directories to delete.  Voila!

    Andy> Thanks, this in fact worked!

    Andy> I'd come across the idea of putting expressions within `...`
    Andy> quotes a couple of years ago but had forgotten the
    Andy> difference between ' and `. (By the way, what is the `
    Andy> character called? Backticks?). My O'Reilly book on Bash
    Andy> simply says `cat dirlist` is an archaic form of command
    Andy> substitution and makes no other mention of this syntax!

And that book is absolutely right.  This syntax is for backward
compatibility only, and is not recommended to use.  A cleaner way to
do it in bash is
 rmdir $(cat dirlist)
which has several advantages over the other syntax.  Read that part in
your bash book again, and you'll see that this is explained.

Tom
--
Wherever I lay my .emacs, that's my ${HOME}

 
 
 

1. standard input instead of input file

Hi,

Consider the paste command.

paste requires input_file_name.

Using the command line
        paste `tty`
we can enter data from standard input.

Is there any other way to do it? (I would like to use pipe with paste).

        Thanks in advance,
        Alex

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.

2. SCO Mail System

3. LCSDNYR 2001 -> standards, standards, standards

4. Adaptec 2930cu and RH 6.0

5. standard input to read

6. Help, Share folder between Samba and Netatalk

7. DOSEMU: dosemu needs a standard input

8. GTK and Tree selection

9. stty: standard input: Invalid argument received on a command shell execution

10. BUG: stty: standard input: Invalid argument

11. Testing standard input

12. HELP: stty: standard input: Invalid option error

13. modem backup from standard input - any software out there ?