1

We are writing a bash script which basically does the job of python dictionary. Below is the code siffet we are using and the expected output.

#!/bin/bash
declare -A serviceTag
serviceTag["source"]="ccr"
declare -A services
services+=( ["dataservice"]="latest" )

serviceTag+=( ["services"]=services )

echo "$serviceTag"

The expected output is

{"source":"ccr","services":{"datasetvice":"latest"}}

But what we are getting is

ccrservices

Can somebody help us in what mistake we are doing here and how can we achieve this using bash and its code?

Regards, Kanthu

  • That and services is just the string services - you need array expansion for that as well. – muru Mar 26 at 11:23
  • Sorry, I didnt get it. Can you pls suggest me the code changes? – Kanthu Canty Mar 26 at 11:30
  • printf '{"%s":"%s","%s":"%s":{"%s":"%s"}}\n' "${!serviceTag[@]}" "${serviceTag[@]}" "${!services[@]}" "${services[@]}" – Jetchisel Mar 26 at 11:31
  • 1
    @jet I reopened it, you can post that as an answer – muru Mar 26 at 11:31
  • Using $avar instead of ${avar[@]} with an array avar refers to its first entry, not to the whole array, and you are NOT getting that output (ccrservices) from the code you have posted. But, as already said, bash's arrays are unidimensional, so that will not work, anyways. – mosvy Mar 27 at 2:02
1

According to the GNU project's Bash reference manual, Bash's arrays are one-dimensional, whether they be indexed or associative. That means you can't nest them. Sorry to be the bearer of bad news, but I don't think what you're trying to do is possible.

| improve this answer | |
  • Thanks Thomas for the clarification. – Kanthu Canty Mar 27 at 4:04
0

Not sure but you can try.

printf '{"%s":"%s","%s":"%s":{"%s":"%s"}}\n' "${!serviceTag[@]}" "${serviceTag[@]}" "${!services[@]}" "${services[@]}" 
| improve this answer | |
  • This is ending up with {"ccrservices":"ccrservices","latest":"latest":{"":""}} – Kanthu Canty Mar 26 at 11:41
  • How can I access this this with key value pair.. ?? – Kanthu Canty Mar 26 at 11:42
  • really, my output is what you're looking for. – Jetchisel Mar 26 at 11:46
  • Yes, your suggested printf I am trying and ended up wih {"0":"ccrservices","0":"latest":{"":""}} output. But this not we are looking. We are expecting {"source":"ccr","services":{"datasetvice":"latest"}}. – Kanthu Canty Mar 26 at 12:07
  • The output you posted is what I get on this side. – Jetchisel Mar 26 at 12:09
0

This isnt a task thats appropriate for shell scripting. Do yourself a favor, and use a programming language for this task. For example with PHP:

<?php
$services = ['dataservive' => 'latest'];
$serviceTag['source'] = 'ccr';
$serviceTag['services'] = $services;
# example 1
print_r($serviceTag);
# exmaple 2
echo json_encode($serviceTag), "\n";

Result:

Array (
   [source] => ccr
   [services] => Array (
      [dataservive] => latest
   )
)
{"source":"ccr","services":{"dataservive":"latest"}}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.