0

how to assign to a variable in Bash an array element such that the array is index itself by a variable (a most trivial thing in any language but seems special in bash?)

I tried many variants such as:

let temp1=list1[list1Index]

or

 temp1=${list1[$list1Index]}

none of them work obviously. No error but nothing is displayed by echo $temp1 What can I do?

here is the full code (with dummy names for paths):

#! /bin/bash
pathFrontalGray='/mnt/c/Users/dummyName/'

list1=('x' 'y' 'z' 'a' 'b' 'c')
prefix1='dummyName'
echo $pathFrontalGray
touch fileNamesGray.txt
ls $pathFrontalGray > fileNamesGray.txt
fileGray='fileNamesGray.txt'
list1Index=0
i=0
while IFS= read -r line
do
    let i=i+1
    echo $i
    #means if NOT divisibe by 9  -> if <expr evaluated to 0> -> equiv if false
    if (($i % 9));then
        let list1Index=list1Index+1
    fi

    temp1=$list1[$((list1Index))]
    echo $temp1
    #mv $pathFrontalGray$line $prefix1${list1[$list1Index]}
    # display $line or do somthing with $line
    #printf '%s\n' "$line"
done <"$fileGray"
SheppLogan
  • 322
  • 3
  • 18
  • `$array[$((index))]`? – daniu Oct 19 '17 at 21:23
  • 2
    Your second approach, `"${arr[$idx]}"`, should work. Show us your array. – randomir Oct 19 '17 at 21:24
  • 1
    `list1[list1Index]` actually works as well, if it's a non-associative array and `list1Index` the name of is a variable containing an integer as a value. Which is to say -- (1) no, it's not "obvious", and (2) we **really** need a [mcve] included in the question so others can reproduce the claimed behavior. – Charles Duffy Oct 19 '17 at 21:28
  • 1
    To let anyone interested test that the behavior inquired about *really does work*, some code that can be copied-and-pasted to test: `list1Index=2; list1=( "FAIL" "BAD" "GOOD" "NOPE" ); echo "${list1[list1Index]}"` – Charles Duffy Oct 19 '17 at 21:30
  • here it is my full code ->see edit up – SheppLogan Oct 19 '17 at 21:32
  • what a horrible syntax.... but well. – SheppLogan Oct 19 '17 at 21:33
  • it's half working the echo displays x but with [index] displayed too which should not – SheppLogan Oct 19 '17 at 21:35
  • ok Charles Duffy but to really you need to assign to a variable because that is the problem: it seems to be hard to get it to simply assign an array indexed by a variable to a variable – SheppLogan Oct 19 '17 at 21:36
  • Try `let index=$((index+1))` (although I don't see why that wouldn't work in the array access). – daniu Oct 19 '17 at 21:52
  • does not work sorry, could you try with my code ? – SheppLogan Oct 19 '17 at 21:58
  • Could you try to edit that code to comply with the **M**inimal part of [mcve]? I can't see a good reason for half the lines in there; code included in a question should be *the shortest possible thing* that someone else can use to generate the same error. – Charles Duffy Oct 19 '17 at 22:12
  • 2
    Oh. Actually, though, `temp1=$list1[$((list1Index))]` is obviously broken. The correct syntax is `temp1=${list1[list1Index]}`, or if you prefer `temp1=${list1[$list1Index]}` or `temp1=${list1[$((list1Index))]}` -- but regardless, you can't leave out the curly braces. – Charles Duffy Oct 19 '17 at 22:13
  • ...which is to say, this is arguably duplicative (well, a fully-contained subset) of [When do we need curly braces around shell variables?](https://stackoverflow.com/questions/8748831/when-do-we-need-curly-braces-around-shell-variables) – Charles Duffy Oct 19 '17 at 22:16
  • indeed this: #! /bin/bash list1Index=2; list1=( "FAIL" "BAD" "GOOD" "NOPE" ); temp1=${list1[list1Index]} works. therefore there must be a (very strage) problem specific to my code. echo $temp1 – SheppLogan Oct 19 '17 at 22:30
  • I already pointed out the bug. http://shellcheck.net/ finds it too; you might consider taking *all* its advice into consideration. – Charles Duffy Oct 19 '17 at 23:25

1 Answers1

0

I found the problem. I m sorry in fact

curr1=${list1[index1]}

works fine. The problem was that the echo of that got lost in lots of other stuff displayed. Sorry for the inconvenience. I guess I was a bit tired...

SheppLogan
  • 322
  • 3
  • 18