0

I'm having a problem with declaring array from STDOUT in Buildspec, which is part of Codebuild in AWS. Buildspec using Pure Bourne Shell, so I can't use this line in code declare -a available_envs=($(eb list --all)) or available_envs=($(eb list --all))

some build command...

eb list --all # list all created env in Elastic Beanstalk

declare -a available_envs=($(eb list --all)) # <<< here is a problem because I can't create array this way.
echo ${available_envs[@]} # <<< checking if variabla have all available env 

if [[ ${available_envs[*]} =~ 'develop' ]]; then
    echo 'Develop environment is allready created.'
    else
        echo "Creating Develop environment..."
        eb create develop
fi

some build command...

Thank you in advance.


Edit:

Output of command eb list --all

develop
production
stage

Solution: Thx @faho for change of view.

I trim output a little before grep command

some build command...
envs=$(eb list --all | tr -d " \t\r" | tr -d [*] | grep "develop")
echo ${envs}

if [ "${envs}" = "develop" ]; then
        echo 'Develop environment is already created.'
        else
                echo "Creating Develop environment..."
fi
some build command...
4
  • Please edit your question to include an example of the output the eb list --all command produces. You can edit the output to disguise sensitive information. Commented Aug 2, 2022 at 12:16
  • sh doesn't have arrays so you'll need to find some other way of representing the data. In finding a solution, questions that would arise will include "can the data items contain spaces or other non-printing characters?" Commented Aug 2, 2022 at 12:18
  • @SottoVoce post is edited. I forgot to include it. Thx for reminder. Commented Aug 2, 2022 at 12:39
  • @roaima Thank you for comment. I know that sh can't create arrays this way, but I'm missing the solution for quite a few day now. That is why I post this question. Commented Aug 2, 2022 at 12:59

1 Answer 1

0

Posix sh has neither arbitrary array variables nor [[ .. ]] conditionals. It has the positional arguments, $@, but setting that (with set) won't help you much because your comparison here won't work either.

So this requires a different way of thinking. You need to figure out what you want to do with this output and act accordingly.

As your code currently stands, you could just do

if eb list --all | grep -q develop; then

If you need the output again and want to not call eb list --all again, you could save it to a variable as one string and then use it later, like

envs=$(eb list --all)

if printf '%s\n' "$envs" | grep -q develop; then

(note: grep -q develop will match any line that has the substring "develop". If that's not wanted you might want grep -q '^develop$')

1
  • I don't have enough points for like. After I gather enough points I will come back and give full validation. Once more, thank you for your help :) Commented Aug 3, 2022 at 10:43

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.