Menu

Dipesh Majumdar

Blog and Paintings

variable substitution in for loop with curly brackets

Requirement - 

I want to calculate the no. of AEM_INSTANCES which I am passing as an argument to a shell script. Suppose the name of the script is script.sh and I want to pass 5 AEM Instances... the usage will be: ./script.sh 5

#!/bin/bash
#set -x TOTAL_AEM_INSTANCE=${1}
echo ${TOTAL_AEM_INSTANCE}
for i in {1..${TOTAL_AEM_INSTANCE}}
do echo $i AEM_TYPE=publish${i}
echo ${AEM_TYPE} done

Unfortunately the variable substitution is not possible like above...

So the Solution is with sequence as shown below - 

#!/bin/bash
#set -x
TOTAL_AEM_INSTANCE=${1}
echo ${TOTAL_AEM_INSTANCE}
for i in `seq ${TOTAL_AEM_INSTANCE}`
do echo $i
AEM_TYPE=publish${i}
echo ${AEM_TYPE} done

Go Back

Comment