Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

execute shell script from perl

by rajaman (Sexton)
on Sep 28, 2017 at 17:53 UTC ( #1200296=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I am trying to call a shell script from my perl program, like:

my @array=qx( ./shellscript.sh <<< " string argument passed ");

But I get the following error:

sh: 1: Syntax error: redirection unexpected

However, when I run the shellscript directly on the command line as (./shellscript.sh <<< " string argument passed "), it works fine.

Any hints?

Thank you.

Replies are listed 'Best First'.
Re: execute shell script from perl
by choroba (Archbishop) on Sep 28, 2017 at 19:13 UTC
    qx , as documented in perlop, runs /bin/sh, which might be different to your terminal shell. Most probably, the shell in which the "Here String" works is bash, but /bin/sh is dash which doesn't support it.

    Also note that what follows <<< isn't a "string argument", but it gets passed to the preceding command on standard input (after some expansions).

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      /bin/sh is dash

      That all depends on the OS. That appears to be true for Debian and Debian-based OSes (Ubuntu, ArchLinux, etc). On Fedora 25 /bin/sh is a symbolic link to bash. On some OSes /bin/sh is still the Bourne shell.

        On some OSes /bin/sh is still the Bourne shell.

        And that's not all. /bin/sh varies wildly with OS and OS version, so it should be avoided where possible. If one has to resort to using a shell (why on earth?), one should explicitly use that shell (e.g. use /bin/bash, /bin/ksh or the like) instead of hoping for the default shell.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: execute shell script from perl
by haukex (Bishop) on Sep 29, 2017 at 06:44 UTC

    I wrote about the topic of running external commands at length here. In this case, since you seem to want to pass something to the script's STDIN and get something from its STDOUT, IPC::Run3 is a good choice IMO; it allows you to capture STDERR too if you need to. Note that running the script with a relative path ("./shellscript.sh") is another possible point of failure, as it relies on the current working directory.

    use warnings; use strict; use IPC::Run3 'run3'; my $in = " string argument passed "; my @cmd = ('/path/to/shellscript.sh'); run3 \@cmd, \$in, \my @out or die "run3 failed"; $?==0 or die "command failed, \$?=$?"; chomp(@out); print "<<$_>>\n" for @out;

    Update: Changed output to array to be more in line with OP's code.

      IPC::Run3 works great. Thanks all for the suggestions.
Re: execute shell script from perl
by NetWallah (Canon) on Sep 29, 2017 at 05:45 UTC
    Following up on choroba(++)'s post , this produces the expected result:
    perl -E 'say for qx(/bin/bash -c "cat <<< \\"This is text\\"")'

                    All power corrupts, but we need electricity.

Re: execute shell script from perl
by Anonymous Monk on Sep 28, 2017 at 18:03 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1200296]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2022-07-07 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?