0

I have an array that reads like this:

# echo "${ARRAY[@]}"
1=Napp Slice
4=NNN Issue
3=GG Allow
2=CO-OP
1=Quit

And I want to loop through it to see if any contain the word Quit and if so, set that element to a variable. So I ran below:

for x in ${ARRAY[@]}; do
  if [ $x == *Quit* ]; then
    FOO=$x
    echo $FOO
  else
    break
  fi
done

I want returned to me 1=Quit but when I run it I get:

# ./myScript.ksh
1=Napp Slice
4=NNN Issue
3=GG Allow
2=CO-OP
1=Quit

How can I pull out just the element containing Quit?

I really tried to see if this question existed before posting, I am newer to shell-scripting. I am sure there is something very simple that I am missing but I can't seem to get it.

KSH syntax only please as that is all I have available to me.

EDIT: I still need to figure out how to delete the element once found

Thank you!

1 Answer 1

0

I'm getting errors when I try to run your script with ksh93, mainly because == does not work within [ ... ]. It is unclear to me what shell you're using. You are also executing break as soon as you don't match the pattern, which means you probably only ever run one iteration of your loop. It's unclear what outputs your array, but it is most likely not the code you show.

In any case, the following script does what you want in the ksh93 shell (and in bash as it turns out, and zsh):

array=(
        '1=Napp Slice'
        '4=NNN Issue'
        '3=GG Allow'
        '2=CO-OP'
        '1=Quit'
)

for element in "${array[@]}"; do
        if [[ "$element" == *Quit* ]]; then
                foo=$element
                printf '%s\n' "$foo"
                break
        fi
done

Note the use of [[ ... ]] with the pattern matching operator ==. Also, be careful to quote the expansion of ${array[@]} so that you get each element individually quoted and not split on spaces.

In the comments, you say you want to delete the element from the array. You can't do that if you loop over the elements of the array, but you may unset the element if you instead iterate over the array's indexes.

for index in "${!array[@]}"; do
        if [[ "${array[index]}" == *Quit* ]]; then
                foo=${array[index]}
                unset -v 'array[index]'
                printf '%s\n' "$foo"
                break
        fi
done

Note that this potentially gives you an array with a "missing" element somewhere and that numeric indexes no longer are consecutive.


The code below reproduces approximately what the second code block above does, but for sh, with some extra functionality added. It is provided here purely for our own amusement.

#!/bin/sh

set --  '1=Napp Slice'  \
        '4=NNN Issue'   \
        '3=GG Allow'    \
        '2=CO-OP'       \
        '1=Quit'

unset -v found

for element do
        shift
        case $element in (*Quit*) found=$element; continue; esac
        set -- "$@" "$element"
done

printf 'Element = %s\n' "${found-(was not found)}"
printf 'Remaining list:\n'
printf '\t%s\n' "$@"
3
  • 1
    Thank you @they! That did it :) Sorry for the lack of info, yes I am using ksh93 and it works like a charm Commented Oct 6, 2021 at 20:09
  • how would I then remove that element from the array? When I try to do it within the loop, I get error: "1=Quit: is not an identifier" Commented Oct 6, 2021 at 21:00
  • @ShelbyAnne See updated answer. Commented Oct 6, 2021 at 21:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.