0

Below is the script trying to be executed:

read n
for ((i=1;i<=$n;i++))
do
file=createserver"$i".json
echo $file
instanceid[$i-1]=$(jq -r '.instance.id' ./$file)
echo $instanceid
done

$file displays correct value. However, $instanceid is not. The output what I get is as below:

createserver1.json
a5a485df-b2e8-4467-9144-d012d96d0305
createserver2.json
**a5a485df-b2e8-4467-9144-d012d96d0305**

The one in bold above is not as expected. The two files from where the instance id is captured as present and its content is as follows:

{"instance":{"id":"a5a485df-b2e8-4467-9144-d012d96d0305","os":"Ubuntu 18.04 LTS x64"}}
{"instance":{"id":"4a8ebe11-be1c-45f9-a82c-c41113ecd3a5","os":"Ubuntu 18.04 LTS x64"}}

When the content is different in the files, why the script takes the contents from the first file itself always. Am I missing something? Please guide.

serverstackqns
  • 722
  • 2
  • 16
  • 39

1 Answers1

1

two ways you can try:

read n
for ((i=1;i<=$n;i++))
do
file=createserver"$i".json
echo $file
instanceid[$i-1]=$(jq -r '.instance.id' ./$file)
echo ${instanceid[$i-1]}
done

or

read n
for ((i=1;i<=$n;i++))
do
file=createserver"$i".json
echo $file
instanceid[$i-1]=$(jq -r '.instance.id' ./$file)
done
for i in ${!instanceid[@]}; do echo ${instanceid[$i]}; done
ruakn
  • 11
  • 1