0

I'm stuck in a syntax lack; tried multiple ways but I somehow don't see the solution. I'm sure it's super simple, but I currently just don't get it.

Goal

Split the QUEUE_STRING into 2 parameters, check the first for equality with one of 2 strings, add the second in another commandline call

What I've got so far

#!/bin/sh
QUERY_STRING="setSize&10240000"
arr=${QUERY_STRING//&/ }
if [ $arr[0] = "status" ]; then
      echo "YES"
      ./myApp status
elif [ $arr[0] = "setSize" ] ; then
      echo "NO"
      ./myApp setSize $arr[1]
else 
      echo "ERROR"
fi

I just get "error" but I should receive "NO"

Thanks for your help

1
  • learn to use set -x (usually )right after the first line, so you actually see what your script is doing when executing it. This will always give you an insight on what might be wrong. Commented Sep 17, 2020 at 14:34

3 Answers 3

2

Here you go:

#!/bin/bash
set -x # you can comment off this line to disable debug output
QUERY_STRING="setSize&10240000"
arr=(${QUERY_STRING//&/ })
if [ "{$arr[0]}" = "status" ]; then
      echo "YES"
      ./myApp status
elif [ "${arr[0]}" = "setSize" ] ; then
      echo "NO"
      ./myApp setSize $arr[1]
else
      echo "ERROR"
fi

Edit

Here it is in sh

#!/bin/sh
set -x # you can comment off this line to disable debug output
QUERY_STRING="setSize&10240000"
arr=$(echo "$QUERY_STRING"|cut -d\& -f 1)
if [ "$arr" = "status" ]; then
      echo "YES"
      ./myApp status
elif [ "$arr" = "setSize" ] ; then
      echo "NO"
      ./myApp setSize "$(echo "$QUERY_STRING"|cut -d\& -f 2)"
else
      echo "ERROR"
fi
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, unfortunally /bin/bash is not available on my system (and can't be added). With /bin/sh it unfortunally doesn't work; complains about "unexpected (" in line 4
it does work in Bash, it will hardly work in sh
Saw your comment too late, edited mine. I can't work with bash, I need sh
You're leaving yourself open to filename expansion: QUERY_STRING="foo&*"; arr=(${QUERY_STRING//&/ }); declare -p arr -- turn off filename expansion before defining the array with set -f
0

The posted code assumes 'arr' is an array, is tries to access the 0-th element using $arr[0]. Both are coded incorrectly, and need to be fixed.

The assignment to arr should create an array. This can be done with "read -a"

read -a arr <<< "${QUERY_STRING//&/ }"

The check for the first element should use "${arr[0]}" and not $arr[0]:

if [ "${arr[0]}" = "status" ]; then
      echo "YES"
      ./myApp status
...

1 Comment

I unfortunally get a "./mq: line 5: syntax error: unexpected redirection" for read -a, as this is a bash function as well. I cannot use bash; POSIX sh is required
0

Splitting strings in shell is a fairly common idiom: use IFS and read:

If you're using bash, use a here-string:

IFS='&' read -r key value <<<"$QUERY_STRING"

If you want portability or you're using a plain POSIX shell, use a here-doc:

IFS='&' read -r key value <<END_INPUT
$QUERY_STRING
END_INPUT

Alternately, parameter substitition:

key=${QUERY_STRING%%&*}   # remove the longest suffix starting with '&'
value=${QUERY_STRING#*&}  # remove the shortest prefix ending with '&'

then a case statement is more compact than a cascading if

case "$key" in
  status)  echo YES; ;;
  setSize) echo NO; echo "$value" ;;
  *)       echo ERROR ;;
esac
NO
10240000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.