http://qs321.pair.com?node_id=691255

bronto has asked for the wisdom of the Perl Monks concerning the following question:

Dearest monks and nuns.

Recently I have been bitten by an unexpected behaviour of a commercial software. In short, if I started a daemon with qx in a perl script, it failed to work properly.

I am still investigating. Anyway, it seems that the daemon fails to work properly if it doesn't run under a "classic" Bourne shell, while qx runs it inside a Bash shell.

The following script:

#!/usr/bin/perl use strict ; use warnings ; my $output = qx'echo $SHELL' ; print $output ;

returns

/bin/bash

The man page for qx says:

qx/STRING/ ‘STRING‘ A string which is (possibly) interpolated and then exe- cuted as a system command with "/bin/sh" or its equiva- lent.[...]

This leads me to the question: how does qx select the aforementioned "/bin/sh equivalent"?

Ciao!
--bronto


In theory, there is no difference between theory and practice. In practice, there is.

Replies are listed 'Best First'.
Re: how does qx select a "/bin/sh equivalent"?
by almut (Canon) on Jun 10, 2008 at 15:37 UTC

    Looking at $SHELL isn't necessarily a reliable way to determine what shell variant is being used. As strace shows, Perl is doing an execve("/bin/sh", ["sh", "-c", "echo $SHELL"] ... (at least on Linux), and that's essentially all it can do... Other than that, it's the shell's job to behave like a classic bourne shell when being called as /bin/sh (which bash actually claims to do...).

Re: how does qx select a "/bin/sh equivalent"?
by ikegami (Patriarch) on Jun 10, 2008 at 16:20 UTC

    To see what Perl uses,

    $ perl -le'use Config; print $Config{sh}' /bin/sh

    But note that on many systems, /bin/sh is bash.

    $ /bin/sh -c 'echo $SHELL' /bin/bash

    If you want to use an alternate shell,

    sub backticks_mysh { open(my $pipe, '-|', '/bin/mysh', '-c', $_[0]) or return; local $/ = wantarray ? $/ : undef; <$pipe> }

      Ok, it was some time ago, but I do not think that the $SHELL variable is reliable. look at my

      $ /bin/sh -c 'echo $SHELL' /bin/bash
      although:
      $ file /bin/sh /bin/sh: symbolic link to `dash'
      and,
      $ file /bin/dash /bin/dash: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), d +ynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[s +ha1]=0x890a514bd261794a6d39da58ea2372fdf48e98d6, stripped
      I don't really know why. Maybe DASH keeps the environment of the calling shell, but I do not trust $SHELL.
        I used $SHELL to demonstrate that /bin/sh is not always the bourne shell. I don't see how your reply relates.
Re: how does qx select a "/bin/sh equivalent"?
by pc88mxer (Vicar) on Jun 10, 2008 at 15:51 UTC
    This leads me to the question: how does qx select the aforementioned "/bin/sh equivalent"?
    The shell that is used is determined at the time that the perl executable was compiled. On Unix-like systems it will probably be /bin/sh, but on other platforms (OS2, Win32, VMS) it could be very different.

    Also, the shell is not guaranteed to be "equivalent" to /bin/sh. On non-Unix systems perhaps "counterpart" is a better way to describe it.