#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Westerville, ohio
    Posts
    2
    Rep Power
    0

    Bash Script - recreate the command line arguments


    I have the following command that is run at the command line of a linux box:

    new.sh import -m "testing of Subversion" /local/idnow/perl/ldap htp://test.com

    (Note the original URL is blocked.) Here, new.sh is simply a wrapper script for the svn (subversion) program. In my bash script, I am doing various things with the arguments, but in the end, I want to rerun the command replacing "new.sh" with svn. In other words:

    svn import -m "testing of Subversion" /local/idnow/perl/ldap htp://test.com

    the problem is that I am getting this error on the "svn" line:
    svn: Try 'svn help' for more info
    svn: Too many arguments to import command


    However, when I run the "svn" command directly at the command line (ie, by me typing it in, and not the bash script), the command works perfectly. So the problem does not lie in the arguments. I believe the problem lies with how I am recreating the command line.

    I cannot simple run the command using the following: svn $@. The reason is that the quotations are removed and svn complains. makes sense to me. So I manipulated the quoted section adding \" before and after it. From that I can tell, that is the problem. Here is the section when I add the quotations into the original command line:

    whitespace="[[:space:]]"
    for i in "$@"
    do
    if [[ $i =~ $whitespace ]]
    then
    i=\"$i\" # adding the quotation marks for later use
    fi
    cLine="$cLine $i" #both of these statements reconstruct the original command line
    done


    When I am finally ready to run the subversion command, I simply do this:
    svn $cLine

    And that is where I get the error.

    Any ideas on what I am doing wrong? Hopefully, there is a way to do what i want to do.

    Thanks,
    frank
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    Why not just use $@?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Westerville, ohio
    Posts
    2
    Rep Power
    0
    Originally Posted by SimonJM
    Why not just use $@?
    Same problem. The quotes are dropped so that the command line looks like:

    svn import -m testing of Subversion /local/idnow/perl/ldap htp://test.com
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Perhaps use the eval keyword.
    Code:
    #!/bin/bash
    
    whitespace="[[:space:]]"
    for i in "$@"
    do
      if [[ $i =~ $whitespace ]]
      then
        i=\"$i\" # adding the quotation marks for later use
      fi
      cLine="$cLine $i" #both of these statements reconstruct the original command line
    done
    
    echo Try 1
    ./a.out $cLine
    echo Try 2
    eval ./a.out $cLine
    Results in
    Code:
    $ ./foo.sh hello world "hey there"
    Try 1
    0:./a.out
    1:hello
    2:world
    3:"hey
    4:there"
    Try 2
    0:./a.out
    1:hello
    2:world
    3:hey there
    With eval, you get a single 3rd argument, with a space, and without quotes.

    a.out was just this compiled, to see the actual arguments.
    Code:
    #include <stdio.h>
    int main ( int argc, char *argv[] ) {
      for ( int i = 0 ; i < argc ; i++ ) {
        printf("%d:%s\n", i, argv[i] );
      }
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo