Results 1 to 4 of 4

Thread: Bash Script: Help with preserving complex command line aruments

  1. #1
    Join Date
    Apr 2007
    Beans
    173

    Bash Script: Help with preserving complex command line aruments

    Hi,
    I'm pulling my hair out on this one... I have a script and it needs to be able to execute itself passing in the command line arguments it was invoked with, plus a few others.

    Some arguments are of the form --parameter="complex value".

    My problem is that the quotes around "complex value" get stripped when the script invokes itself, resulting in the parameters being parsed incorrectly.

    I've written a test script to demonstrate the problem:

    Code:
    #!/bin/bash
    echo "---------------------------start of script"
    echo "Received argument list:$@"
    
    for i in "$@"
    do 
        echo "Parameter found:$i"
        case $i in
            --param=*)
                PARAM=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
                ;;
            --recalled)
                RECALLED=TRUE
                ;;
        esac
    done
    
    if [ -n "$PARAM" ]
    then
        echo "Parameter is >$PARAM<"
    fi
    
    if [ $RECALLED ]
    then
        echo "Recalled"
    else
        echo "Not Recalled, calling $0 again"
        command="$@"
        echo "Sending argument list:$command"
        /bin/bash -c "$0 --recalled $command"
    fi
    echo "-----------------------------end of script"
    Executing this script gives this output:

    Code:
    $ /tmp/test --p1 --param="complex string"
    ---------------------------start of script
    Received argument list:--p1 --param=complex string
    Parameter found:--p1
    Parameter found:--param=complex string
    Parameter is >complex string<
    Not Recalled, calling /tmp/test again
    Sending argument list:--p1 --param=complex string
    ---------------------------start of script
    Received argument list:--recalled --p1 --param=complex string
    Parameter found:--recalled
    Parameter found:--p1
    Parameter found:--param=complex
    Parameter found:string
    Parameter is >complex<
    Recalled
    -----------------------------end of script
    -----------------------------end of script
    As you can see, on the first invocation the --param="complex string" is successfully parsed on the first call but is seen as separate arguments on the second call.

    I've tried so many ways to do this now, I hope I am missing something obvious. Any solution to this would be greatly appreciated if there's anyone that knows how to solve this...

  2. #2
    Join Date
    Jan 2007
    Location
    $here ? $here : $there
    Beans
    3,717
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Bash Script: Help with preserving complex command line aruments

    Have you tried passing "complex value" as 'complex value'? The single quotes should never be seen by the shell.
    Don't try to make something "fast" until you are able to quantify "slow".

  3. #3
    Join Date
    Apr 2007
    Beans
    173

    Re: Bash Script: Help with preserving complex command line aruments

    Yes, same result:

    Code:
    /tmp/test --p1 --param='complex string'
    ---------------------------start of script
    Received argument list:--p1 --param=complex string
    Parameter found:--p1
    Parameter found:--param=complex string
    Parameter is >complex string<
    Not Recalled, calling /tmp/test again
    Sending argument list:--p1 --param=complex string
    ---------------------------start of script
    Received argument list:--recalled --p1 --param=complex string
    Parameter found:--recalled
    Parameter found:--p1
    Parameter found:--param=complex
    Parameter found:string
    Parameter is >complex<
    Recalled
    -----------------------------end of script
    -----------------------------end of script

  4. #4
    Join Date
    Oct 2005
    Location
    Vienna, Austria
    Beans
    265
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Bash Script: Help with preserving complex command line aruments

    What seems to work is
    Code:
    [... quite some code above ...]
    
    if [ $RECALLED ]
    then
        echo "Recalled"
    else
        echo "Not Recalled, calling $0 again"
        $0 --recalled "$@"
    fi
    
    [... some lines to follow ...]
    This is a little simpler than your code (no explicit shell invocation, no separate assembly of the command). However, it seemed that the use of too many double quotes destroyed the inteded quoting-structure.

    The above code behaves pretty much as expected (see bash-man-page):
    "$@" is equivalent to "$1" "$2" ...
    Best,
    schilcha

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
  •