Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

how does qx select a "/bin/sh equivalent"?

by bronto (Priest)
on Jun 10, 2008 at 15:26 UTC ( #691255=perlquestion: print w/replies, xml ) Need Help??
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 (Pope) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://691255]
Approved by almut
help
Chatterbox?
[shmem]: sounds good.
[shmem]: but I'm crumbling smaller stones. remember...
[stevieb]: I'm working on it to fatten it up and make it more reliant so I can finalize my Raspberry Pi automated build system for that software :) It's all well and fun, until I try to make it work with Windows lol
[shmem]: "debugging a program is more difficult than to write it in the first place. If you code your program as smart as you are, you are, by definition, too dumb to debug it."
[stevieb]: I literally laughed. That's good :) Perhaps I just need to go climb another mountain and forget about it

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2017-03-28 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (342 votes). Check out past polls.