Find the answer to your Linux question:
Results 1 to 7 of 7
Hello, I need help with dynamic variable which get number and string from array. Code: _0_USERS=(11 taaa 15 20 22) grupa=0 for ((n=0; n<3;n=$[n+1]));do echo " $[_${grupa}_USERS[$n]] _${grupa}_USERS[$n]" done Resault ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    3

    [BASH] Arrays and dynamic variable

    Hello,
    I need help with dynamic variable which get number and string from array.

    Code:
    _0_USERS=(11 taaa 15 20 22)
    grupa=0
    for ((n=0; n<3;n=$[n+1]));do
    echo "
    $[_${grupa}_USERS[$n]]
    _${grupa}_USERS[$n]"
    done
    Resault is:
    Code:
    11
    _0_USERS[0]
    
    0
    _0_USERS[1]
    
    15
    _0_USERS[2]
    As you can see, everything is ok until it meet a string than it put 0 instead "taaa". If i put only strings into array still get 0s

  2. #2
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    $[_${grupa}_USERS[$n]]

    For some reason, that can't be evaluated into a string. I've never used brackets like this, so I am not too sure why. Honestly, this code is awful looking, and you should re think the way you're doing things. What exactly are you trying to accomplish?

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I agree with likwid that this script is very poor-looking. It can probably be written better.

    In any event, the problem here is that $[...] notation invokes arithmetic mode. Strings become 0 in arithmetic mode, hence the 0. What you actually want is something like:
    Code:
    ${_0_$grupa_USERS[$n]}
    (that is, ${...} syntax instead).
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined!
    Join Date
    Nov 2007
    Posts
    3
    Quote Originally Posted by Cabhan View Post
    I agree with likwid that this script is very poor-looking. It can probably be written better.

    In any event, the problem here is that $[...] notation invokes arithmetic mode. Strings become 0 in arithmetic mode, hence the 0. What you actually want is something like:
    Code:
    ${_0_$grupa_USERS[$n]}
    (that is, ${...} syntax instead).

    ./a: line 4: ${_0_$grupa_USERS[$n]}: bad substitution

    _0_USERS=(11 u 15 20 22)
    grupa=0
    for ((n=0; n<3;n=$[n+1]));do
    echo ${_0_$grupa_USERS[$n]}
    done
    This is only an example of problem. I dont care about "looking", i just want it to work. Now i had to refer to another array with names and that works

  5. #5
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    You need to use the eval command:

    Code:
    eval echo \${_${grupa}_USERS[$n]}
    Vic

  6. #6
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    You will care about how it looks when you go back in a year to add/tweak some code.

    I suggest thinking about the problem to see if you can avoid this situation. There are lots of roads to the same point, some shorter, and less windy than others.

  7. #7
    Just Joined!
    Join Date
    Nov 2007
    Posts
    3
    Quote Originally Posted by likwid View Post
    You will care about how it looks when you go back in a year to add/tweak some code.

    I suggest thinking about the problem to see if you can avoid this situation. There are lots of roads to the same point, some shorter, and less windy than others.
    I wont use it in my code, a just need a solution to my problem... im not a programmer, i just writting for my self to use it in my network to change transfers.

    Quote Originally Posted by vsemaska View Post
    You need to use the eval command:

    Code:
    eval echo \${_${grupa}_USERS[$n]}
    Vic
    THX it works !!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...