Home > Enterprise >  Create if statement to iterate over a list of names and create name if not in list?
Create if statement to iterate over a list of names and create name if not in list?

Time:11-11

I am working with an Azure shared image gallery and trying to write a bash if statement to iterate through the list of image definition names and if that image definition name is not there, create it elif, etc...

I have a variable set as:

defs=$(az sig image-definition list --resource-group $MyRG --gallery-name $mySIG --query [*].name) \
echo "$defs"

What I'm attempting to do is create an if statement that will iterate through this list of image definition names in my Azure compute gallery, and create a specified name if it does not exist. My original assumption was something like if [$defs != x but not sure how to go about setting x, as it would be a user input for someone wanting to create a new definition.

Sorry if my question is unclear. If there's more info I can provide please let me know.

The problem I'm facing is that I understand bash somewhat but not in conjunction with how exactly I am attempting to apply it to my Azure image definitions issue.

CodePudding user response:

You can use read to ask a user for input.

$ read -p "please enter a definition: " x
please enter a definition: def4
$ echo $x
def4

Then, assuming $defs is a space separated list of definitions, could do something like this:

# list of defs..
defs="def1 def2 def3"

# ask user to enter a definition
read -p "please enter a definition: " x

# set this to false until def is found
def_exists=false

# iterate over list of defs and see if user value is found
for i in ${defs}; do
  echo $i
  if [[ ${i} == "${x}" ]]; then
    echo "found it"
    def_exists=true
  fi
done

# if user def exists, good
if $def_exists; then
  echo "definition exists already.. nothing to do"

# otherwise...
else
  # do something
  echo "$x not found.. do stuff.."
fi

Example where def is in existing list:

please enter a definition: def1
def1
found it
def2
def3
definition exists already.. nothing to do

Example where def is not in existing list:

please enter a definition: def4
def1
def2
def3
def4 not found.. do stuff..

This is what my defs variable holds, and what the commenter above was asking you to provide so we can see what type of data is being referenced.

$ typeset -p defs
declare -- defs="def1 def2 def3"

If the data/ definitions have spaces, etc.. could use arrays or we can look at other options. If you post some examples of the definitions, like are they a single word? multiple words with spaces in them, etc. Hope this helps!

CodePudding user response:

You can use Global Parameters --query and --output to query the list from the output of az sig image-version list in Azure CLI. Please refer to :tyeset

For example, here is bash script with Azure CLI

# convert to array
defs_array=($defs)
#declare -p defs_array
# the length of array
length=${#defs_array[@]}
read -p "please enter a name: " x
for i in ${defs_array[@]}
do
if [[ ${i} == $x ]]; then
    echo "found"
    break
else
   if [[ ${i} == ${defs_array[-1]} ]];then
      echo "not found"
   fi 
fi
done
  • Related