Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: command line argument protection

  1. #1
    Join Date
    Jun 2009
    Location
    Georgetown, TX
    Beans
    260

    command line argument protection

    Is there a standard accepted way to "protect" arguments to commands in bash, so that they are passed to their command as intended rather than being interpretted as bash reserved words? I'm getting overwhelmed with contextual ambiguities in bash, grep, etc.. For example, it is not obvious to me what is the best way to pass a regular expression that contains literal spaces to grep (bash apparently interprets these before passing the regular expression to grep). Also, is quotation on the command line ALWAYS interpretted by bash rather than by the command to which it may be passed as part of an argument?
    Why is there something rather than nothing?

  2. #2
    Join Date
    Dec 2009
    Beans
    184
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: command line argument protection

    Are you familiar with escape characters? Otherwise, can you give an example of a command that doesn't behave as you'd want?

  3. #3
    Join Date
    Jun 2009
    Location
    Georgetown, TX
    Beans
    260

    Re: command line argument protection

    Quote Originally Posted by noah++ View Post
    Are you familiar with escape characters?
    Yes. Escaping is a form of quoting, that is, single character quoting, and the problem for me is that there is quoting for bash and then quoting for the argument to the command that I run from bash, and sometimes the way that I choose to handle this feels really sloppy. I have always eventually figured out how to do it with quoting, but, for lack of a better way of saying it, it just feels dangerous. I would just like to know if there is some argument protection policy that will ALWAYS work in bash, and could NEVER cause a problem for the command that takes the argument.

    Quote Originally Posted by noah++ View Post
    ... can you give an example of a command that doesn't behave as you'd want?
    grep, as in:
    Code:
    grep -v -e unwanted text myfilename
    I have written it in a form that emphasizes the problem to the human reader. It just looks ambiguous. Of course, I know that I could use any of the following three quoted versions
    Code:
    grep -v -e unwanted\ text myfilename
    grep -v -e "unwanted text" myfilename
    grep -v -e 'unwanted text' myfilename
    and then some, but it makes me nervous. That quoting is for bash's sake, but it is buried in the grep arguments, and my human mind is boggled when it gets only a little bit more complex than this. I realize my own limitations; I know that I know nothing: a little bit of knowledge is a dangerous thing.

    Maybe I just need to step away from the computer for a while.

    Thanks for responding.
    Why is there something rather than nothing?

  4. #4
    Join Date
    Dec 2009
    Beans
    184
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: command line argument protection

    My advice:
    1. Check out the xargs command.
    2. When in doubt, quote.

  5. #5
    Join Date
    Jun 2009
    Location
    Georgetown, TX
    Beans
    260

    Re: command line argument protection

    OK, here's an example of failed quoting:
    Code:
    user@computer:~$ find /my/dir/ -name foo* -exec 'cp {} /my/other/dir/. ;'
    find: missing argument to `-exec'
    The idea is that the -exec action of the find command has certain syntax that needs to be protected from bash, namely {} to represent the file that was found, and ; to represent the end of the commands to be executed.

    I will be investigating xargs now.

    BTW, escaping did work:
    Code:
    user@computer:~$ find /my/dir/ -name foo* -exec cp \{\} /my/other/dir/. \;
    I don't understand why it should complain with the single quotes but accept the escaping. At the risk of sounding lazy, I hate escaping; it forces me to remember arbitrary shell needs (and it looks so damn sloppy).
    Last edited by MichaelBurns; January 3rd, 2011 at 09:48 PM.
    Why is there something rather than nothing?

  6. #6
    Join Date
    Jun 2010
    Location
    asoko
    Beans
    834
    Distro
    Ubuntu

    Re: command line argument protection

    there are several kinds of quotes in bash that have differant meanings:
    " => treat as a single string, but intepret all vars and keywords
    ' => treat as a string literal. no expansion or interpretation
    ` (backqoute, on the tilde key) => interpret/expand

    http://wiki.bash-hackers.org/syntax/quoting

  7. #7
    Join Date
    Jun 2009
    Location
    Georgetown, TX
    Beans
    260

    Re: command line argument protection

    Quote Originally Posted by endotherm View Post
    there are several kinds of quotes in bash that have differant meanings:
    ...
    ' => treat as a string literal. no expansion or interpretation
    ...
    Yes, I know this, and that was precisely my intent, so I am still confused. I wanted -exec to expand and interpret, not bash. But instead, -exec apparently completely ignored my arguments.
    Why is there something rather than nothing?

  8. #8
    Join Date
    Jun 2010
    Location
    asoko
    Beans
    834
    Distro
    Ubuntu

    Re: command line argument protection

    I've seen that error before. I seem to recall having gotten around it by adding a plus sign ('+') to the end of the -exec line, but was never sure why that worked.

  9. #9
    Join Date
    Jan 2008
    Location
    Manchester UK
    Beans
    13,573
    Distro
    Ubuntu

    Re: command line argument protection

    Quote Originally Posted by MichaelBurns View Post
    OK, here's an example of failed quoting:
    Code:
    user@computer:~$ find /my/dir/ -name foo* -exec 'cp {} /my/other/dir/. ;'
    find: missing argument to `-exec'

    quote the -name parameter, and quote the result (ie '{}') and escape the ;

    so


    Code:
    find /my/dir/ -name 'foo*' -exec cp '{}' /my/other/dir/ \;

  10. #10
    Join Date
    Jun 2009
    Location
    Georgetown, TX
    Beans
    260

    Re: command line argument protection

    @endotherm&nothingspecial
    Thank y'all for taking time to reply. However, we're getting a bit off-track. The point is: what will always work? So far, we've learned that single quotes will not. I.e., we (I) have answered the question from the OP:
    ... is quotation on the command line ALWAYS interpretted by bash rather than by the command to which it may be passed as part of an argument?
    with an apparent "no". That was the purpose of my example. How to use the -exec action of the find command is simply a matter of trial-and-error. But, that's the point: trial-and-error bad; predictability good.

    It seems that escaping may be the only form of quoting that always "works". I just simply don't like this; the rules are too arbitrary for me; it goes beyond the simple concept of quoting to contextual quoting (i.e. it behaves differently depending on which character it quotes). Also, I think that there is something deeper going on sometimes. E.g. the command operated by the -exec action may need to see spaces, but these spaces cannot be protected from bash for some reason. So, perhaps bash itself is interpretting -exec and the words that follow! Hmm... that actually gives me an idea. I'll get back with my idea and results if it works.

    @noah++
    I appologize; I haven't gotten very far into my investigation of the xargs command. I will try to make a point of it later tonight.

    BTW, I realize that this thread is pretty damn boring, and so I do appreciate any participation whatsoever. That's also part of the point. I want to get past these boring discussion and move on to more interesting and complex bashfu.
    Why is there something rather than nothing?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •