# Processing arguments in a string

**URL:** <https://community.unix.com/t/processing-arguments-in-a-string/367148>\
**Category:** Shell Programming and Scripting\
**Created:** [June 22, 2017, 10:22am UTC](https://community.unix.com/t/processing-arguments-in-a-string/367148 "2017-06-22T10:22:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![user052009](https://community.unix.com/user_avatar/community.unix.com/user052009/32/9785_2.png) [@user052009](https://community.unix.com/u/user052009)\
**Post date:** [June 22, 2017, 10:22am UTC](https://community.unix.com/t/processing-arguments-in-a-string/367148/1 "2017-06-22T10:22:35Z")

</div>

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

```nohighlight
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

```nohighlight
while [-n "$1"]; do

```

witth

```nohighlight
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.).

---

<div class="post-metadata">

**Author:** ![chebarbudo](https://community.unix.com/user_avatar/community.unix.com/chebarbudo/32/1211_2.png) [@chebarbudo](https://community.unix.com/u/chebarbudo)\
**Post date:** [June 22, 2017, 10:38am UTC](https://community.unix.com/t/processing-arguments-in-a-string/367148/2 "2017-06-22T10:38:18Z")

</div>

Hi general\_franco,

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

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

```

Regards  
Santiago

---

<div class="post-metadata">

**Author:** ![user052009](https://community.unix.com/user_avatar/community.unix.com/user052009/32/9785_2.png) [@user052009](https://community.unix.com/u/user052009)\
**Post date:** [June 22, 2017, 4:38pm UTC](https://community.unix.com/t/processing-arguments-in-a-string/367148/3 "2017-06-22T16:38:08Z")

</div>

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

```nohighlight
set -- $OPTPARMS

```

before the while loop should work, right?

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [June 22, 2017, 4:47pm UTC](https://community.unix.com/t/processing-arguments-in-a-string/367148/4 "2017-06-22T16:47:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![user052009](https://community.unix.com/user_avatar/community.unix.com/user052009/32/9785_2.png) [@user052009](https://community.unix.com/u/user052009)\
**Post date:** [June 22, 2017, 5:09pm UTC](https://community.unix.com/t/processing-arguments-in-a-string/367148/5 "2017-06-22T17:09:12Z")

</div>

Will the command

```nohighlight
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?

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [June 22, 2017, 5:33pm UTC](https://community.unix.com/t/processing-arguments-in-a-string/367148/6 "2017-06-22T17:33:01Z")

</div>

Yes, try and report back...

---

<div class="post-metadata">

**Author:** ![user052009](https://community.unix.com/user_avatar/community.unix.com/user052009/32/9785_2.png) [@user052009](https://community.unix.com/u/user052009)\
**Post date:** [June 23, 2017, 4:04am UTC](https://community.unix.com/t/processing-arguments-in-a-string/367148/7 "2017-06-23T04:04:51Z")

</div>

yes, that works! 🙂  
Thanks
