bash variables in array subscript

304 views
Skip to first unread message

Ben Guthro

unread,
Jul 18, 2012, 4:24:18 PM7/18/12
to ticktick...@googlegroups.com
Hello,

I am trying to make use of ticktick.sh to loop over some elements in a json array, and I'm having some issues, that I don't see addressed in the README, or examples. Hopefully, someone here will be able to help me out.

The script below shows how indexing with a variable seems to not work. My intent is to make use of this construct in a for loop, once I figure out the proper mechanism.

Perhaps there is a better way to do it that I'm not seeing.

Any insight is greatly appreciated.

#!/bin/bash -e

. ./TickTick/ticktick.sh

``
manifest = {
        "repos": [
        {
                "name": "test1",
                "src":  "test1.git"
        },
        {
                "name": "test2",
                "src": "test2.git"
        }
        ]
  }
``

# works
echo ``manifest.repos[0].name``

#broken
i=0
echo ``manifest.repos[$i].name``

Chris McKenzie

unread,
Aug 22, 2012, 8:27:50 PM8/22/12
to ticktick...@googlegroups.com
Hi Ben,


Short answer: Currently impossible.

Long answer: 
Not provably impossible.  Ticktick works by doing a single pass over the script and then rewriting it.  The reason is that the expansion simply will not work because the $i is expanded after manifest.repos becomes something internal and unwieldy, something like __tick_data_manifest_repos_0000000001_name or so (this is just from memory).  So basically it would be $___tick_data_manifest_repos_$i ... which won't work because the first variable will expand to nothing, the second will expand to 0, although wait, it gets worse.  It's actually ... $i_name which is again no variable, so they both expand to nothing and you get exactly that I'm thinking.

Basically what would be needed is for the expansion here to be some kind of miraculous bashismo variable substitution.  I'm not going to say "impossible" here, just "really hard".  I'll append your email and this response to the ticket and do some exploration. Thanks!

~chris.
Reply all
Reply to author
Forward
0 new messages