Score:0

Shell script not reading the file as expected

cn flag

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.

Score:1
gb flag

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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.