So my co-worker Anthony was having trouble getting the last access times of all the files in a directory:
ls | perl -nle 'print "$_: ", -A'
He got lots of blanks when it came to the -A. So I told him to turn on -w. He got lots of warnings. I tried the program on my machine, and it worked fine. I tried it on his machine, and it didn't work. So I tried explicitly using /bin/ls and it worked.

With a sour expression on my face, I typed alias ls, and what did I see? ls --color But his terminal wasn't set up to display color. That was a fun brain-wrecker: the ANSI codes were in the filenames, and thus, -A "\e[...foobar\e[..." returned undef.

_____________________________________________________
Jeff japhy Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Why system calls suck.
by xphase_work (Pilgrim) on Jul 31, 2001 at 21:13 UTC
    This illustrates, in addition to security issues, why all system commands in any script should be prefaced by their full path.

    Another good example on solaris machines is the /usr/ucb directory. It contains the BSD versions of unix commands, which are different than the /bin and /usr/bin binaries.

    me@hostname > alias ps /usr/ucb/ps
    This has completely different syntax and output than /usr/bin/ps

    System calls are also not very portable, as ps -ef won't work on the BSD versions of ps and ps aux will fail on the solaris versions.

    --xPhase

      This illustrates, in addition to security issues, why all system commands in any script should be prefaced by their full path.
      I used to be in that camp, but I've found that it causes portability problems. We have a pile of legacy code that runs on many different machines. Some of the system calls use absoulte paths, and this causes trouble when common programs migrate between /bin, /usr/bin, /usr/local/bin, and other places on various versions of Solaris, Linux, Irix, etc...

      For a while, I tried to maintain a config section with the absolute paths to every executable I might want to use. That didn't seem to help matters, because the config has to be tweaked all the time. What I really want is to be able to drop my script on a random machine and have it work with the minimum of fuss.

      I've been thinking that a better solution might be to set $ENV{PATH} explicitly (possibly to different value depending on the OS), and then avoid using absolute paths whenever possible. Has anyone been there and brought back some advice?

      System calls are also not very portable, as ps -ef won't work on the BSD versions of ps and ps aux will fail on the solaris versions.
      ps is just pathological. If you need that kind of information out of the system, you're in for pain one way or another.
Re: Why system calls suck.
by buckaduck (Chaplain) on Jul 31, 2001 at 23:55 UTC
    I've seen a very similar problem with our users here. The "ls" alias is commonly used for "ls -F", which will for example append asterisks to filenames if they are executable. If you use a system call to read the filelist and then search for Perl programs like this:
    # This doesn't work my @files = `ls *`; chomp(@files); @files = grep { /\.p[lm]$/ } @files;
    It won't work because any executable files have that darned asterisk at the end.

    But that's just a symptom of a larger issue: There are better ways to get a list of files. Use system commands only as a last resort, and even then you should use a fully qualified pathname.

    buckaduck

Re: Why system calls suck.
by blakem (Monsignor) on Aug 01, 2001 at 22:39 UTC
    You can skip the aliasing in some shells (including bash) by escaping the command with '\'.Instead of:

    ls | perl -nle 'print "$_: ", -A'
    Try:
    \ls | perl -nle 'print "$_: ", -A'
    The second one should not expand to 'ls --color' like the first one is prone to do.

    -Blake

Re: Why system calls suck.
by runrig (Abbot) on Aug 01, 2001 at 00:54 UTC
    ... or "Why aliasing your UNIX commands to include ANSI escape codes or other non-standard garbage sucks."
Re: Why system calls suck.
by Anonymous Monk on Aug 01, 2001 at 13:49 UTC
    That is why /bin/sh rules.