system() question

1561 views
Skip to first unread message

David Andre

unread,
Nov 6, 1995, 3:00:00 AM11/6/95
to
I posted this around a week or 2 ago. I haven't seen any response yet.

Can anyone help me with this??

The ypcat command below is essentially returning a list of machines. Let's
say I wanted to do an ls on each machine in @LIST. How can that be done?
I thought the following might work, but it didn't. It does the rsh to the
machine *first*, then does the ls locally on the machine this script is
run from. Can anyone help?? I would like it to do the ls on the machine I
am rsh'ing to.

I can do this in a shell script, I'm just wondering how it can be done
in Perl.

Thanks In Advance for your help,
-David

#!/usr/sbin/perl
#
#
#

@LIST=`ypcat -k platforms | sort | cut -f1 -d" "`;

foreach (@LIST) {
system("rsh $_ ls");
sleep(2);

}

exit;


--


David Andre
<an...@hks.com>


Robert Paul Braddock

unread,
Nov 6, 1995, 3:00:00 AM11/6/95
to
David Andre (an...@argo.hks.com) wrote:
: I posted this around a week or 2 ago. I haven't seen any response yet.
Annoying, isn't it? I never get answers to questions in newsgroups.

: system("rsh $_ ls");
This command works as expected for me. Maybe you want to specify a
directory to ls instead of your home directory, so you can be sure it is
behaving as you think? Or perhaps you have a wierd rsh...

...........................................................................
" Thus to be lost and thus to sink and die, // Robert Braddock
Perchance were death indeed!" // xet...@rice.edu

Michael Schilli

unread,
Nov 6, 1995, 3:00:00 AM11/6/95
to
David Andre (an...@argo.hks.com) wrote:
: I posted this around a week or 2 ago. I haven't seen any response yet.

: @LIST=`ypcat -k platforms | sort | cut -f1 -d" "`;

: foreach (@LIST) {

: system("rsh $_ ls");

: sleep(2);

: }

Your ypcat command is appending newlines to the hostnames.

foreach (@LIST) {
chop; #<--- Yeah!


system("rsh $_ ls");

sleep(2);
}

should do the trick.

-Michael.

Mark-Jason Dominus

unread,
Nov 7, 1995, 3:00:00 AM11/7/95
to
In article <47lc8q$o...@argo.hks.com> an...@argo.hks.com (David Andre) writes:
> I thought the following might work, but it didn't. It does the rsh to the
> machine *first*, then does the ls locally on the machine this script is
> run from. Can anyone help??
>
> @LIST=`ypcat -k platforms | sort | cut -f1 -d" "`;
>
> foreach (@LIST) {
> system("rsh $_ ls");
> sleep(2);
> }

Yep.

> The ypcat command below is essentially returning a list of machines.

It's returning a list of machines, one per line. So the elements of
@LIST all end with newlines, and when you interpolate one into that
system command, you get something like

system("rsh plover
ls");

That's two commands, not one.

You need to remove the newlines from those hostnames before you use
them. Try

> foreach (@LIST) {
chop;


> system("rsh $_ ls");
> sleep(2);
> }

That's what `chop' is for.

> I can do this in a shell script...

That's because in a shell script, `...` transforms all the newlines
into spaces, so you don't have this problem of newlines being retained
in newline-separated items. Instead, you have the problem of not
being able to tell where lines end, which is usually worse.


Fred Kulack

unread,
Nov 7, 1995, 3:00:00 AM11/7/95
to
> I can do this in a shell script, I'm just wondering how it can be done
> in Perl.
>
> @LIST=`ypcat -k platforms | sort | cut -f1 -d" "`;
>
> foreach (@LIST) {
chop;
> system("rsh $_ ls");
> sleep(2);
>
> }

You'll need to chop the newline from $_ prior to using it in the middle of the system call like that. Shells don't keep those around, perl does by default.

Fred Kulack Team OS/2 is for EVERYONE!
-----------------------------------------------------------------------------
Open Systems Enablement - CPA (Internal : kulack@rchland )
IBM Rochester, MN (Internet : kul...@vnet.ibm.com )
ph: 507.253.5982 (Tie line : 553.5982 )
WWW Internal Page (Personal Page http://w3.rchland.ibm.com/~kulack/ )
--

Fred Kulack Team OS/2 is for EVERYONE!
-----------------------------------------------------------------------------
Open Systems Enablement - CPA (Internal : kulack@rchland )
IBM Rochester, MN (Internet : kul...@vnet.ibm.com )
ph: 507.253.5982 (Tie line : 553.5982 )
WWW Internal Page (Personal Page http://w3.rchland.ibm.com/~kulack/ )

Larry Wall

unread,
Nov 8, 1995, 3:00:00 AM11/8/95
to
In article <47lc8q$o...@argo.hks.com>, David Andre <an...@argo.hks.com> wrote:
: The ypcat command below is essentially returning a list of machines. Let's

: say I wanted to do an ls on each machine in @LIST. How can that be done?
: I thought the following might work, but it didn't. It does the rsh to the

: machine *first*, then does the ls locally on the machine this script is
: run from. Can anyone help?? I would like it to do the ls on the machine I
: am rsh'ing to.
:
: @LIST=`ypcat -k platforms | sort | cut -f1 -d" "`;
:
: foreach (@LIST) {
: system("rsh $_ ls");
: sleep(2);
:
: }

Debugging lesson #1. If you were to print the command before passing it
to system, all would suddenly be made clear.

Hint: try saying chop(@LIST) before the loop.

Larry

Mark-Jason Dominus

unread,
Nov 9, 1995, 3:00:00 AM11/9/95
to
In article <47lss5$k...@larry.rice.edu> xet...@rice.edu (Robert Paul Braddock) writes:
> Annoying, isn't it? I never get answers to questions in newsgroups.

There are a lot of reasons, many of which get repeated over and
over again, many of which don't. Some of the reasons you hear a lot
are: Questions are incoherently phrased. Questions have the form `my
code doesn't work' and don't include the code. Questions include the
code, and it's 200 lines long, and comp.lang.perl is not a debugging
service. Nobody knows the answer to the question. The question is so
badly punctuated that nobody can bear to read it. Questions aer
written by a non-native speaker of English and the native speakers who
are trying to understand it can't. (That's a shame, but it does
happen. I often wish that these people would post in their native
languages. I'd love to see more discussion in languages other than
English. Some Dutch guy tried posting all his comp.lang.c articles in
Dutch a few years ago, and all the Americans flamed him. How
humiliating for me!)

Apart from these oft-mentioned reasons are many others that are
not so often discussed, because anyone who tries to bring them up gets
flamed. But it's the truth: I know the reason I don't answer more
questions is because so many of them questions are so damn stupid.
They're stupid in a lot of different ways, but they're still stupid.
I don't want to suggest that that's why your questions go unanswered.
I don't know what you're asking. No doubt the reason your questions
go unanswered is because they're so deep and interesting that nobody
really knows the answer.

The most common stupid question is the one from someone who has
some high-level problem that they need solved. They have an idea that
they'd like to do it in perl, and they don't really know perl. So
instead of learning perl, they post to comp.lang.perl.misc.

Now don't get me wrong. I don't have any problems answering the
questions of someone who's trying to learn perl. I love THOSE
qusetions. But these people don't seem to be asking useful questions
for that.

Today, for example, I saw a question from a guy who wants to get a
list of hostnames out of nslookup. `How do I do that?' he says. And
it's hard to know what to make of that. What does he mean? Does he
not know how to open a pipe? Is his `open' command failing? What's
going on here? I can't give a useful answer without understanding the
problem. Having your `open' fail is a problem. Wanting to list
hostnames is not a problem; it's a desire.

Here are some similar questions that would have made more sense to me:

1. ``I'm a lazy asshole and I can't be bothered to learn to
program myself, but I know if I post here you'll give me
something for free.''

(OK, fair enough. At least I can send him a rate card.)

2. ``I'm trying to use `open' to talk to nslookup, but...
...here's my code; what's wrong?''

(Good question.)

3. ``I know perl has a `system' command for running programs,
but I can't see how to get my commands into nslookup
once I have run it.''

(Good question.)

4. ``I'm trying to use `getprotobynumber' to talk to nslookup...''

(Good question.)

See, I'm not just biting people's heads off, here. #4 is a good
question, because it gives me something to work with. OK, he has a
very weird idea about interprocess communication, but that's
ignorance, and that's what we're here to correct. He doesn't know
about `open'; I can refer him to the manual.

Here's another example: Some guy wants to assemble a list of email
addresses . ``How do I do that?'' he wants to know. Well, duh. Get a
big pad of paper and read news for a couple days and write down all
the addresses you see. Problem solved. What's it doing in
comp.lang.perl?

Oh, you wanted to do it in perl. Well, I guess I'd open a socket
to the NNTP server and send it some XHDR commands for the `From'
lines. But that's not Perl; you could do that in any language. I
could do it in Bourne Shell for you if I'm allowed to use a little
external thingie to handle the socket parts for me. What's it doing
in comp.lang.perl?

Oh, you wanted us to write the program for you? Wait, let me send
you my rate card.

Someone posted yesterday asking how to get the data from a file
where the start and end of the data is marked by keywords. Same thing
going on here. ``Well, here's how you solve your problem: First you
go take an introductory class in programming and learn to write
programs in some language. Then you go to the bookstore and buy this
book by Wall and Schwartz, it's really good. Read the book carefully
and try out the examples. Then if you still have general questions
like `How do I do this' instead of `I thought that this code would do
X but instead it does Y' you hire a professional to write your program
for you. Or you could just skip right to step 3. Want my rate
card?''

The worst stupid questions are the ones that come from people who
have no business asking them. The most perfect example of this that I
could have imagined was in comp.unix.questions a couple of years ago.
Some guy came and asked how you could tell if a file is a hard link.
My jaw flapped open and it's stayed open since then. I couldn't have
been any more stumped if he'd asked why Bodhidharma came from the
West. What do you say to this guy? Do you tell him the truth, that
all files are hard links, that even symbolic links are hard links?
He's not going to understand you; you might as well keep your mouth
shut. Do you tell him the truth, that the answer won't do him any
good because he doesn't know what a hard link is, so why did he bother
asking in the first place? No, that'll just make him mad. I followed
that message for the next couple weeks, and nobody said anything.
What could you say? The guy had no business asking the question and
no use for the answer. Maybe the right answer would have been to cut
off his finger or something. I dunno.

Some questions get ignored because they're boring. Someone asked
today how to compare two variables (I assumme he means the contents of
those variables) to see if they're exactly the same. You've gotta be
in an awfully good mood to take the time to answer that. Maybe
someone will. Maybe I will.

If I answer that one today, maybe he'll be back tomorrow asking
how to check to see if two variables contain different values. Maybe
I won't. What I find incredible is that if you tell these people to
go read the manual and come back in two weeks, you sometimes get
jumped on for not being helpful to beginners. Bah. If everyone todl
these people to go read the manual, they'd eventually figure out that
that's what you have to do ,and then I wouldn't have to spend so much
of my life dealing with incompetent programmers.

Some questions are logically nonsensical because the querent
thinks they know more than they do. A lot of these have the form
``How do I use X to accomplish Y?'' There's nothing wrong with this,
except that sometimes X is a chocolate-covered banana and Y is the
integration of European currency systems. I always get stuck on
these, probably because I can't get rid of the idea that the person
really has a good reason for wanting to use X. I know a half-dozen
easy simple ways to accomplish Y, but I can't imagine what X has to do
with it. This is a problem for me in my day job, too. Clients are
always saying to me ``We want to use product X to do multimedia
development on the world-wide web,'' and all I can think is ``Well,
gee, what would you want to go and do that for?'' Sometimes it turns
out that they want to do it because they want to impress the
manufacturers of X, and I don't work on those jobs.

The flip side of this is a questions like ``I want to accomplish
X, but I don't want to use Y. What can I use instead?'' Which,
again, is sometimes reasonable, and then sometimes X is closing a
filehandle and Y is the `close' function.

The questions I like the best are the ones that go like this:

``I want to accomplish X.

I thought that the following code fragment would do it:

...

But instead it does Y.

Why is that?''

This one is also pretty good:

``I want to accomplish X.

I thought I might be able to use facility Y.

But Y doesn't seem like it's quite right,
because of Z.

What should I use instead of Y, or how can I overcome Z?''

When I ignore these, it's usually because I don't know the answer.
There were an awful lot of them today. It makes me very happy.


There you go; a 160-line dissertation on why questions go unanswered.
Now don't let me hear you saying nobody ever answers your questions.

Reply all
Reply to author
Forward
0 new messages