an arrays

Forum rules
Before you post please read how to get help
ckonn
Level 2
Level 2
Posts: 50
Joined: Wed Oct 01, 2014 7:03 pm

an arrays

Postby ckonn » Tue May 02, 2017 1:51 pm

Hello,

I would like to ask is it correct to compare a variable that has a value of a string with an array that has many elements as strings, like in the lines:

Code: Select all

echo "how are you today?"
declare -a ans=("ok" "good" "very well" "excellent" "super")
read condition
if [[ "${condition}" = "${ans}" ]];
then
       echo "glad to know you are $condition"
else
       echo "everything will be ok!"
fi

thanks in advance
linux mint 17.3 mate

User avatar
Flemur
Level 13
Level 13
Posts: 4670
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: an arrays

Postby Flemur » Tue May 02, 2017 3:34 pm

I'm definitely no expert, but your script seem to recognize the "ok" answer only.
Also, in a terminal

Code: Select all

$ declare -a ans=("ok" "good" "very well" "excellent" "super")
$ echo ${ans}
ok
Mint 18.3 Xfce/fluxbox/pulse-less
Xubuntu 17.10/fluxbox/pulse-less
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?

Habitual
Level 13
Level 13
Posts: 4866
Joined: Sun Nov 21, 2010 8:31 pm
Location: 0.0.0.0

Re: an arrays

Postby Habitual » Tue May 02, 2017 3:46 pm


lmuserx4849
Level 5
Level 5
Posts: 503
Joined: Wed Dec 17, 2014 2:55 am

Re: an arrays

Postby lmuserx4849 » Wed May 03, 2017 3:18 am

ckonn wrote:...
I would like to ask is it correct to compare a variable that has a value of a string with an array that has many elements as strings, like in the lines:
...


The short answer is no - If no index is supplied, array element 0 is assumed.

set -x and declare -p are your debugging friends :-)

You have a couple of options:
1. Expand the array and use a pattern
2. Use a function to loop through the array
3. Use an associative array, where condition is the key

You might find http://mywiki.wooledge.org/BashFAQ helpful. Lots of examples and doc.

See example below:

Code: Select all

#set -x
declare -a ans=("ok" "good" "very well" "excellent" "super")
declare -A aans=(["ok"]= ["good"]= ["very well"]= ["excellent"]= ["super"]=)  # associate array
declare -l condition             # give condition the lower case attribute so if OK is entered it becomes ok

# declare -p ans aans
printf -- '%s ' "${ans[@]}"; printf -- '\n'

### 1. Make condition a pattern and match against the entire expanded array
printf -- '----- test 1 -----\n'
read -r -p 'How are you today? ' condition      # read -p option is a prompt. No need for the separate echo.
if [[ -n "${condition}" && "${ans[@]}" == *${condition}* ]]; then         # notice the *condition*
  printf -- "%s found\n" $condition
else
  printf -- "%s not found\n" $condition
fi

### Create a function to loop through the array, break when found
printf -- '----- test 2 -----\n'
function condition_exists() {
  local -- rc=1                               # default not found
  for element in "${ans[@]}"; do
    if [[ ${condition} == "${element}" ]]; then
      rc=0
      break
    fi
  done
  return $rc
}

read -r -p 'How are you today? ' condition
if condition_exists; then
  printf -- "%s found\n" $condition
else
  printf -- "%s not found\n" $condition
fi

### Use an associative array where the key is the expected condition
printf -- '----- test 3 -----\n'
read -r -p "How are you today? " condition
if [[ -n "${condition}" && -n "${aans[$condition]+set}"  ]]; then
  printf -- "%s found\n" $condition
else
  printf -- "%s not found\n" $condition
fi
Last edited by lmuserx4849 on Sat May 06, 2017 3:43 am, edited 1 time in total.


Return to “Scripts & Bash”