Processing arguments in a string

Hi

The following code works when reading the arguments from the command line but fails when I try to read from a string. So this works

while [ -n "$1" ]; do
  case $1 in
        -dbversion) if [ "`echo $2 | grep -e '^-[a-z]'`" ]; then { echo "ERROR: missing value for '$1' (seen '$2')"; usage; exit 1; } else { shift; INPUT_VERSION=$1;   } fi ;;
        -type) if [ "`echo $2 | grep -e '^-[a-z]'`" ]; then { echo "ERROR: missing value for '$1' (seen '$2')"; usage; exit 1; } else { shift; INPUT_TARGET_TYPE=$1;   } fi ;;
        -host) if [ "`echo $2 | grep -e '^-[a-z]'`" ]; then { echo "ERROR: missing value for '$1' (seen '$2')"; usage; exit 1; } else { shift; INPUT_HOST=$1;   } fi ;;
  *)    echo "ERROR: unknown argument '$1'"; exit 1;;
  esac
  shift
done

but when I replace the line

while [ -n "$1" ]; do

witth

OPTPARMS="-dbversion 11.2.0.4.0 -type backup -host testhost"
 while [ -n $OPTPARMS ]; do
 

it only sees the "ERROR: unknown argument"

Can somebody please tell me what the problem is?
Many thanks

Please ignore the old code (back ticks, echo etc.).

Hi general_franco,

If you want to manually force arguments, you can use :

set -- -dbversion 11.2.0.4.0 -type backup -host testhost
while [ -n "$1" ]; do

Regards
Santiago

1 Like

Thanks Santiago. I need to pass in a variable into the while loop. The values in $OPTPARMS will change every run.
So

set -- $OPTPARMS

before the while loop should work, right?

If you use OPTPARMS for passing the info, you need to use it in the case statement as well...

Will the command

set -- $OPTPARMS

not push the values in $OPTPARMS back onto the input stack?

So the code in the first post will stay the same if I use this cmd before it?

Yes, try and report back...

yes, that works! :slight_smile:
Thanks