BASH: loop-reading a list creating a dynamic array

I have a list formatted as below. If I replace the list by, say, "aaa bbb ccc", everything works fine. Anyone an idea?

list=$(echo -e "1.1 1.2 mgmt")

n=0
for i in $list; do
   let "array1_$i[$n]=$(date "+%N")"
   ((++n))
done

n=0
for i in $list; do
   var=array1_$i[$n]
   echo ${!var}
   ((++n))
done

thanks in advance for any ideas...

Answers


You can change the shebang line to

#!/bin/bash -xv

to see what bash tries to run. In fact, you are trying to create a variable named array1_1.1, which is not a valid variable name: dots are not allowed.


. is not a valid character in an identifier.

man bash:

       name   A word consisting only of alphanumeric characters and underscores, and
              beginning with an alphabetic character or an underscore.  Also
              referred to as an identifier

Need Your Help

Distinguishing between 'destination of udp packet unreachable' and 'received packet with payload of length 0' in Objective C

objective-c sockets callback udp

Currently, when I send a udp packet to an unreachable destination (e.g. an unbound local port), I get an event that I can't seem to distinguish from receiving a zero-length udp packet.

Invite user(s) to event with notification - works manually but not with Graph API

facebook facebook-graph-api facebook-events

I'm currently experiencing some problems with the Graph API when inviting users to an event. The users are being invited successfully, but they do not receive a notification of the invite.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.