Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

command line and quotes

by skerr1 (Sexton)
on Jun 02, 2006 at 04:16 UTC ( [id://553217]=perlquestion: print w/replies, xml ) Need Help??

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

(Solaris 2.6 with Korn Shell) I have a tool called foo. I want to execute it so that the quotes used in the command are retained. Is this possible? So my usage would be:

foo -s "1 2 3"

and I'd like @ARGV to have:

-s, "1 2 3"

As it is, @ARGV has:

-s, 1 2 3

is this just because of the shell? I can do this:

foo -s '"1 2 3"'

but who wants to do that right?

Thanks for helping me understand.

Regards,
Shannon Kerr

Replies are listed 'Best First'.
Re: command line and quotes
by GrandFather (Saint) on Jun 02, 2006 at 04:33 UTC

    TIAS. YMMV

    Different command shells handle quotes differently. The rules for Windows are quite different (in subtle ways) than those for *nix for example. However pretty much all command shells generate an argument array - an array of strings that corespond to the "arguments" passed on the command line. If the arguments contain various special characters they need to be quoted. But the kind shell removes the quotes for you. After all, all you really want are the arguments right?

    If you really need the quotes, put em back. If you need to distinguish between an argument that was quoted and one that wasn't, then you are pretty much stuffed unless you quote the quotes.


    DWIM is Perl's answer to Gödel

      Dang. Thought so. So I guess I could say that for tool foo, if there is an argument "-s", that I need to add quotes around the following argument. Guess that's the way to handle it.

      Thanks,
      Shannon Kerr
Re: command line and quotes
by Tanktalus (Canon) on Jun 02, 2006 at 04:26 UTC

    First off, please don't put your text inside the markers for your sig. Some people turn off sigs, and won't see anything here.

    Second, the way the shell operates is that the quotes are special characters. That's not using your program, that's using a shell. Learning to use the shell means that you'll learn to use backslashes as appropriate to get the meaning you want across. Whether that's a good thing or bad may depend on what you want to accomplish ;-)

    I'm kinda curious as to why you need quotes passed through, though.

      Thanks for the tip on the sig. My fault for not reading clearly. Fixed it now.

      Need the quotes because I'm passing @ARGV through a tcp socket. There won't always be quotes, so when they are there, I need to keep them so they can be passed.

      I could process @ARGV looking for the "-s" and add quotes around the following argument, but I'd like to avoid that.

      Thanks,
      Shannon Kerr
Re: command line and quotes
by Joost (Canon) on Jun 02, 2006 at 10:08 UTC
    You don't get that kind of information in your program - the shell takes it away before your program runs.

    Since your primary concern is passing the arguments on correcty, why not do:

    @ARGV = map { quotemeta($_) } @ARGV; print "@ARGV"; __OUTPUT__ > perl test.pl -s "1 2 3" \-s 1\ 2\ 3
    This depends on whether quotemeta does the kind of escaping you need, but it seems to be a reasonable start.

      For bash-like shells, quotemeta doesn't do the correct escaping, as it turns a newline to a backslash-newline pair, which bash will unescape to an empty string.

Re: command line and quotes
by greenFox (Vicar) on Jun 02, 2006 at 05:59 UTC

    Escaping your quotes *should* work every-where. Works for me on *nix, Cygwin and Dos-

    foo.pl -s \"1 2 3\"

    Ignore me I was smoking happy weed that just gives you a quote on the beginning of one argument and the end of the other, serve me right for testing by doing a print " [@ARGV]"...

    --
    Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-26 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found