Re: System call not acting as expected

From: Mike Kohary (sorry_at_no.spam)
Date: 09/08/04


Date: Wed, 8 Sep 2004 12:17:49 -0700


"Eric Schwartz" <emschwar@pobox.com> wrote in message
news:etobrggpoux.fsf@wilson.emschwar...
>
> Please read:
>
> perldoc -f system
>
> In general, if a function isn't doing what you expect, you should read
> the documentation for it first via 'perldoc -f $function', before
> posting here.

Thanks, I was unaware of that. Newbie alert! :)

> If you had, you would have read (in the first
> paragraph, no less!):
>
> If there is more than one argument in LIST, or if LIST is an array
> with more than one value, starts the program given by the first
> element of the list with arguments given by the rest of the list.
> If there is only one scalar argument, the argument is checked for
> shell metacharacters, and if there are any, the entire argument is
> passed to the system's command shell for parsing (this is "/bin/sh
> -c" on Unix platforms, but varies on other platforms).
>
> You seem to be expecting the latter behaviour, but because you're
> using commas, you're creating a list with more than one value, and
> thus trying getting the former. Why are you using commas there
> anyhow? Why not use the string concatenation operator:
>
> system("ls -d ".$log_location."*_instance_".$instance." > logfile_dirs");
>
> or just interpolate directly into the command line (this is the most
> perlish solution that uses system):
>
> system("ls -d $log_location*_instance_$instance > logfile_dirs");
>
> or better yet
>
> my @logfile_dirs = `ls -d $log_location*_instance_$instance`;
>
> Of course, this last only works if the next step is to read the file
> logfile_dirs into an array, which seems likely, but I don't know your
> application.

That's exactly right, and thanks for all the suggested alternatives - they
work perfectly of course. :)

> > The idea is to ls -d some directories to the file logfile_dirs.
>
> You might also look into opendir/readdir to iterate through
> directories; that is generally more portable, and will save you from
> other possible sillinesses, like when someone aliases 'ls' to 'ls
> --color', exports the alias, and then runs your program. Then you get
> all sorts of fun problems, like having to sort out terminal escape
> sequences.

Good idea - I didn't think of that either. I have so much to learn. Thanks
for all the help!

Mike



Relevant Pages

  • Re: System call not acting as expected
    ... You seem to be expecting the latter behaviour, ... Why are you using commas there ... all sorts of fun problems, like having to sort out terminal escape ... > The result of this line confirms that it is generating exactly the command ...
    (comp.lang.perl.misc)
  • Parallel mergesort does not provide increased performance on multi-processor machines.
    ... I wrote a parallel mergesort algorithm in C++. ... thread sorts the right half of the array and the other sorts the left half ... Could this have something to do with allocating memory or ...
    (comp.parallel)
  • Re: indirect sort
    ... puts it into an array and sorts this array is faster than ... just sorting the objects. ... And bear in mind that Dominik's little test sorts the doubles, but doesn't attempt to transfer that sorting to any objects; adding the code to also swap around a paralle pointer array would probably slow it down, albeit only fractionally. ... Java chooses mergesort for arbitrary objects, because it's stable, and quicksort for primitives, because it's fast, and since primitives which compare equal are indistinguishable even in principle, stability doesn't matter. ...
    (comp.lang.java.programmer)
  • Re: looping thru a fields values
    ... function to generate an array of FirstNames and an array of LastNames. ... "Rob" wrote in message ... > by commas. ... > append query but could only use left and instr to get the ...
    (microsoft.public.access.queries)
  • Re: tk widget
    ... mb_ser that is another field from an array. ... winfo exist on Done button Frame? ... so when you do a foreach over a string tcl only splits ... > on spaces and the commas become part of each element. ...
    (comp.lang.tcl)