Reputation: 149
Following is my example. I am trying to create a script that will do git clone + git pull. Now below i am trying to iterate 2nd array (A_modules) to check if any of repos folder is there or not, if not create it and then cd to it and do git pull. The problem is how do i use one of 1st array value as 2nd array name in variable to loop. on second for loop, the ${DIR[@]}; doesn't work !. Need to know how do i expand ${DIR[@]} to ${A_modules[@]}. I can do this easily with 3 for loop for 3 values in 1st array. but any smarter way to achieve this with the way i am trying.. Thanks a lot.
#!/bin/bash
BASE_FOLDER=/opt/xyz
declare -a DIR_TOP=(A_modules customers C_modules)
declare -a A_modules=(python_repo java_repo perl_repo)
declare -a customers=(repo1 repo2 repo3)
declare -a C_modules=(xy_repo1 yz_repo2)
#GIT clone.
cd ${BASE_FOLDER}
for DIR in ${DIR_TOP[@]}; do
if [ ! -d "${DIR_TOP[$DIR]}" ]; then
mkdir -p ${BASE_FOLDER}/${DIR}
fi
for REPO in ${DIR[@]}; do
cd ${BASE_FOLDER}/${DIR_TOP[$DIR]}/
if [ ! -d "${REPO]}" ]; then
mkdir ${REPO}
echo "Git cloning ${DIR_TOP[$DIR]} group repositorys: ${REPO} ..."
git clone --branch 8.0 https://${GIT_USER}@${GIT_SERVER}/r/${DIR_TOP[$DIR]}/${REPO}.git
fi
done
done
Upvotes: 2
Views: 106
Reputation: 8164
you should to use tempVar
and next evaluate with !
, review: bash-indirect-expansion-please-explain
#!/bin/bash
declare -a DIR_TOP=(A_modules customers C_modules)
declare -a A_modules=(python_repo java_repo perl_repo)
declare -a customers=(repo1 repo2 repo3)
declare -a C_modules=(xy_repo1 yz_repo2)
for DIR in ${DIR_TOP[@]}; do
echo "1: $DIR";
tempVar="$DIR[@]";
for REPO in ${!tempVar}; do
echo "2: $REPO";
done
done
Upvotes: 1