Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Passing values from Perl script to shell script

by leocharre (Priest)
on Dec 09, 2009 at 18:24 UTC ( [id://811994]=note: print w/replies, xml ) Need Help??


in reply to Passing values from Perl script to shell script

May be of interest; I came across String::ShellQuote, very nice, escapes anything..
use String::ShellQuote 'shell_quote'; my $cmd = sprintf 'cp %s %s', shell_quote($weirdpath), shell_quote($weird2); `$cmd`;

Replies are listed 'Best First'.
Re^2: Passing values from Perl script to shell script
by afoken (Chancellor) on Dec 09, 2009 at 19:11 UTC
    May be of interest; I came across String::ShellQuote, very nice, escapes anything..

    Nope, it doesn't quote "anything". It works just for some unnamed version of some unnamed bourne shell. The author wanted to add more shells, but he did not since 2005. The test.t look very strange, especially I don't see any reasonable test for passing arguments via a shell.

    So, it's old, unmaintained, not well-tested, and broken for all shells except for that unspecified bourne shell. Use the multi-argument forms of exec, system, or open instead, they do not need quoting.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      So you claim it has problems, but you fail to mention any?

      • "Old" is not a problem. For example, Perl is very very old, yet we still use it.
      • Why do you think it's unmaintained? Unchanged isn't unmaintained. I would expect it to remain unchanged since it's not like bourne-shell quoting changes with time.
      • The fact that a module that quotes for sh doesn't work for csh isn't a bug.

      So what problem does it have?

        So what problem does it have?

        Problems with String::ShellQuote

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Well, if I want to take a text string, and turn it into a string that posix command line will interpret as one argument- what do you suggest to use instead? (And what you suggested in the post is not a replacement for String::ShellQuote)

      If you do not have a suggestion- I must say- String::ShellQuote has been working well for me. That said- as per your observations- maybe it's time to contact the author and offer contributions?

        I don't see why you want to invoke a shell in a way that was meant to interact with humans. You can pass an arbitary string to any shell script you want to invoke, just like you would do for any other program: using the multi-argument form of exec, system, or open.

        If the shell script is executable (chmod +x script.sh), there is really no difference to invoking a compiled program (except for the O/S kernel passing the script name to its interpreter binary as argument, but that's completely transparent). You use the shell script like any other program, i.e. exec('/path/to/script.sh',$any,$args,$you,$like).

        If the shell script is not executable (chmod -x script.sh), you pass the script name as first argument to a shell, and the remaining arguments after that. I.e. exec('/some/shell','/path/to/script.sh',$any,$args,$you,$like).

        In both cases, the shell script will see four parameters: The contents of $any as first parameter, the contents of $args as second parameter, the contents of $you as third parameter, and the contents of $like as fourth parameter.

        The only remaining problem are NUL bytes ("\x00") in the parameter values. They terminate the argument even if perl originally passed a much longer string, simply because the underlying API uses C strings that end at NUL bytes.

        Regarding the state of String::ShellQuote: Feel free to contact and support the author and improve the module.

        I don't see any use for that module on Unix-like systems, but with support for command.com and cmd.exe, it could be useful on Win32. Unfortunately, you can not be sure about proper quoting on Windows, as each INVOKED application is free to implement different quoting rules. See also the second half of Re^3: why IPC::Open3 can't execute MS-DOS "dir" command?.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found