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

i am trying to write a program which calls shell scripts using cygwin on windows , but it give me dialog box for "open file with " for the shell script.how can can i make it run under existing environment

eg.

$path = "/old/new2.sh"; $command ="$path"; system $command; my $output = qx/new2.sh /;

Replies are listed 'Best First'.
Re: run in-program shell script under windows using cygwin
by ww (Archbishop) on Jul 30, 2014 at 11:51 UTC
    Wrapping shell scripts in Perl tends (NB: 'tends!') to be a less than optimal solution for many/most problem cases. Perl can probably do the job with its native capabilities, without the overhead of shelling-out.

    In short, if you're writing new shell scripts, consider writing Perl; if you're re-using existing ones, consider (the often easy job of) translating them.


    check Ln42!

Re: run in-program shell script under windows using cygwin
by NetWallah (Canon) on Jul 30, 2014 at 15:26 UTC
    Expanding on what Anonymous Monk has wisely quoth -
    When you call the WINDOWS shell with your "qx" command, and pass a file wht a ".sh" extension, Windows needs help to figure out what program to use to open the ".sh" file. It does not know, so it pops up a dialog asking you.

    One solution is to explicitly call the cygwin shell and pass the .sh script.

    Another possible solution is to use the windows "assoc" command or GUI equivalent to associate the .sh extension with cygwin.

            Profanity is the one language all programmers know best.

Re: run in-program shell script under windows using cygwin
by Anonymous Monk on Jul 30, 2014 at 11:42 UTC
    windows doesn't know what to do with cygwin shell programs, so system '...cygwinbash.exe', 'new2.sh'
Re: run in-program shell script under windows using cygwin
by Laurent_R (Canon) on Jul 30, 2014 at 17:56 UTC
    I was also surprised when I discovered that recently. One example. I have this silly shell script:
    $ cat hello.sh #!/usr/bin/bash echo "hello world!"
    Running this Perl one-liner yields no output:
    $ perl -e '$command = "./hello.sh"; $out = `$command`; print $out, "\n +";'
    But this:
    $ perl -e '$command = "./hello.sh"; $out = `bash $command`; print $out +, "\n";' hello world!
    and also this:
    $ perl -e '$command = "bash ./hello.sh"; $out = `$command`; print $out +, "\n";' hello world!
    both work.

      Running this Perl one-liner yields no output:

      $ perl -e '$command = "./hello.sh"; $out = `$command`; print $out, "\n +";'

      Very strange indeed: I've just tried on Cygwin, and that's working as expected for me, producing the desired output.

        Thank you for your comment, AppleFritter, that's even more puzzling. Just in case anyone has any doubt, I can show that my shebang line in the bash script should be correct:
        $ which bash /usr/bin/bash
        It is a mystery for me at this point. Maybe something to do with the environment variables. To tell the truth, I also have a Strawberry and an ActivePerl instances installed on that MS Win 7 box, this might create problems. But the Perl version used under Cygwin is really the Cygwin Perl version:
        $ perl -v This is perl 5, version 14, subversion 4 (v5.14.4) built for cygwin-th +read-multi (with 7 registered patches, see perl -V for more detail) Copyright 1987-2013, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge.

      I was also surprised ...

      Why?

      $ perl -V:sh sh='cmd /x /c';

      Unless your cygwin perl hash something bash in there, cmd.exe is your shell

      My *cygwin*alike* has

      $ C:\citrusperl\mingw\msys\bin\perl -V:sh sh='/bin/sh';

      So even if *my*cygwin*alike* has got /bin/sh , it won't work when calling perl from cmd.exe, only from the bash itself ... in actual cygwin that might get resolved (if cygwin perl is linked to cygwin dll) but it depends on the perl being called

        So even if *my*cygwin*alike* has got /bin/sh , it won't work when calling perl from cmd.exe

        Actually it will, same magic that actual cygwin uses to resolve it is used by the mingw/msys compiled version

        So check your paths, check your shebangs people :)

        I was surprised because I did not expect that, simply.

        Now, running your command, I get this:

        $ perl -V:sh sh='/bin/sh';
        And I have also this:
        $ which sh /usr/bin/sh
        Well, there is something that I don't get here...

        when i run the above i get

        $ which sh /usr/bin/sh

        and

        $ perl -V:sh sh='cmd /x /c';
Re: run in-program shell script under windows using cygwin
by ikegami (Patriarch) on Jul 31, 2014 at 14:50 UTC
    Sounds like you are using a Windows build of Perl, not a cygwin one. While cygwin knows how to execute /old/new2.sh (i.e. by looking for the shebang), Windows doesn't know anything about shebang lines (and wouldn't find /usr even if it did). Windows uses file associations to determine how to execute a non-binary.
Re: run in-program shell script under windows using cygwin
by sandy105 (Scribe) on Jul 31, 2014 at 08:17 UTC

    i am running active perl 5.16 on windows 7