Sponsored Content
Top Forums Shell Programming and Scripting Assign Two Dimensional Array In Bash At Once Post 303017206 by Chubler_XL on Thursday 10th of May 2018 10:53:49 PM
Old 05-10-2018
Firstly bash doesn't support two dimensional arrays. How do you want to reference the values in this array?

Are you ok with:

Code:
${myarray[row * 10 + col]}

Where is the data to come from to populate this array, user input, file, constant data?

eg:

Code:
myarray=(
   0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 0,9
   1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9
   2,0 2,1 2,2 2,3 2,4 2,5 2,6 2,7 2,8 2,9
   3,0 3,1 3,2 3,3 3,4 3,5 3,6 3,7 3,8 3,9
   4,0 4,1 4,2 4,3 4,4 4,5 4,6 4,7 4,8 4,9
   5,0 5,1 5,2 5,3 5,4 5,5 5,6 5,7 5,8 5,9
   6,0 6,1 6,2 6,3 6,4 6,5 6,6 6,7 6,8 6,9
   7,0 7,1 7,2 7,3 7,4 7,5 7,6 7,7 7,8 7,9
   8,0 8,1 8,2 8,3 8,4 8,5 8,6 8,7 8,8 8,9
   9,0 9,1 9,2 9,3 9,4 9,5 9,6 9,7 9,8 9,9
)

echo "Element myarray[6,3]= " ${myarray[6 * 10 + 3]}


Last edited by Chubler_XL; 05-11-2018 at 12:04 AM..
This User Gave Thanks to Chubler_XL For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help for record (2 dimensional array.)

I am going to develop a address book using the shell scripting commands without sed, awk, .... I am thinking to apply the concept of 2 dimenstional array. Can I create a two dimensional array for the insertion/updation/deletion of record in unix. If yes then tell me plz or recommend me some... (1 Reply)
Discussion started by: murtaza
1 Replies

2. Shell Programming and Scripting

Multi Dimensional array in KSH

Is there any way to use multi dim. array in KSH ? (1 Reply)
Discussion started by: sinpeak
1 Replies

3. Shell Programming and Scripting

2 dimensional array in unix

I am trying to implementing two dimensinal array in ksh script.Would you pls help me out. I have a large size of file, File contains looks like ID SID VLAUE1 VALUE2 TOTALVALUE 1 a1 01 02 03 1 b1 02 05 07 ... (2 Replies)
Discussion started by: pritish.sas
2 Replies

4. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

5. Shell Programming and Scripting

Select multiple values from an Oracle database and assign it to two dimensional array

hi I have two tables in oracle DB and am using a joining query which will result in the output as follows. i need to assign it to a two dimensional array and use it for my further calculations. the way i tried is as follows. #!/bin/ksh export... (1 Reply)
Discussion started by: aemunathan
1 Replies

6. Programming

Return two dimensional array in c++

I am writing matrix multiplication and trying to return a two dimensional array from a function but I keep getting errors. Can someone please help me? here is my code (it is just the skeleton of my program): void main () { ... int *matmultiply (int, int, int, int , int , int ) ... } ... (4 Replies)
Discussion started by: saboture88
4 Replies

7. Shell Programming and Scripting

How to reference 2 dimensional array in awk?

Hello, all For a 1-dimensional array, such as myarr_1=1 myarr_1=2 myarr_1=3I know I can write a loop as below to show the array member one by one: for (i in myarr_1){print i, myarr_1}Now, suppose I have a two dimensional array such as: myarray_2=1 myarray_2=2 myarray_2=10 myarray_2=20My... (3 Replies)
Discussion started by: littlewenwen
3 Replies

8. Shell Programming and Scripting

Assign two dimensional array in bash at once

Hi, I have a 10*10 two dimensional array. How do I assign value to all it's 100 elements at once? I don't want to open two for loops and assign one by one. Thanks, Amit (2 Replies)
Discussion started by: amit14august
2 Replies

9. Shell Programming and Scripting

Multi Dimensional array

I have an array of names. Each one of the name, has a number represented to it. For example A has an ID 8, B has an ID 2. What I am after is a for loop that when the array is in position 1, a particular variable is set to the value of position 1 in array 2 declare -a arr=("A" "B" "C"... (6 Replies)
Discussion started by: nms
6 Replies

10. Shell Programming and Scripting

Multi Dimensional array in bash

Hi, I'm developing a script which contains a multi dimensional array, however for some reason the array is not iterating. When executing the script, services are listed as arguments from argument 2. Ex voice data sms. service=${@:2}; for services in $service do ... (2 Replies)
Discussion started by: nms
2 Replies
MAKEDUMPFILE.CONF(5)					Linux System Administrator's Manual				      MAKEDUMPFILE.CONF(5)

NAME
makedumpfile.conf - The filter configuration file for makedumpfile(8). DESCRIPTION
The makedumpfile.conf is a configuration file for makedumpfile tool. makedumpfile.conf file contains the erase commands to filter out desired kernel data from the vmcore while creating DUMPFILE using makedumpfile tool. makedumpfile reads the filter config and builds the list of memory addresses and its sizes after processing filter commands. The memory locations that require to be filtered out are then poi- soned with character X (58 in Hex). FILE FORMAT
The file consists of module sections that contains filter commands. A section begins with the name of the section in square brackets and continues until the next section begins. "["<ModuleName>"]" <FilterCommands> where "[" is the character [ "]" is the character ] <ModuleName> is either 'vmlinux' or name of a Linux kernel module. <FilterCommands> is a list of one or more filter commands as described in the section FILTER COMMANDS of this manual page. The section name indicates a kernel module name (including vmlinux) where the symbols specified in subsequent erase commands belong to. The unnamed section defaults to [vmlinux] section. However, a user can also explicitly define [vmlinux] section. The sections help makedumpfile tool to select appropriate kernel or module debuginfo file before processing the subsequent erase commands. Before selecting appropriate debuginfo file, the module name is validated against the loaded modules from the vmcore. If no match is found, then the section is ignored and makedumpfile skips to the next module section. If match is found, then makedumpfile will try to load the corresponding module debug- info file. If module debuginfo is not available then, makedumpfile will skip the section with a warning message. FILTER COMMANDS
filter command A filter command is either an erase command or a loop construct. Each erase command and loop construct must start with a new line. Each filter command describes data in the dump to be erased. Syntax: <EraseCommands>|<LoopConstruct> where <EraseCommands> Described in the subsection erase command of this manual page. <LoopConstruct> Described in the subsection Loop construct of this manual page. erase command Erase specified size of a kernel data referred by specified kernel/module symbol or its member component. The erase command syntax is: erase <Symbol>[.member[...]] [size <SizeValue>[K|M]] erase <Symbol>[.member[...]] [size <SizeSymbol>] erase <Symbol>[.member[...]] [nullify] where <Symbol> A kernel or module symbol (variable) name that is part of global symbols /proc/kallsyms. <SizeValue> A positive integer value as a size of the data in bytes to be erased. The suffixes 'K' and 'M' can be used to specify kilobytes and Megabytes respectively where, K means 1024 bytes and M means 1024 ^ 2 = 1048576 bytes. The suffixes are not case sensitive. <SizeSymbol> A simple expression of the form <Symbol>[.member[...]] that denotes a symbol which contains a positive integer value as a size of the data in bytes to be erased. <Symbol>[.member[...]] A simple expression that results into either a global kernel symbol name or its member components. The expression always uses '.' operator to specify the member component of kernel symbol or its member irrespective of whether it is of pointer type or not. member[...] Member or component of member in <Symbol>. The erase command takes two arguments 1. kernel symbol name or its member components and 2. size of the data referred by argument (1) OR nullify keyword. The second argument size OR nullify is optional. The unit for size value is in bytes. If size option is not specified then the size of the first argument is determined according to its data type using dwarf info from debuginfo file. In case of 'char *' data type, the length of string pointed by 'char *' pointer is determined with an upper limit of 1024. The size can be specified in two forms 1. a integer value as explained above (<SizeValue>) and 2. a simple expression in the form of <Symbol>[.member[...]]] that results into base type (integer) variable. If the specified <Symbol> is of type 'void *', then user needs to provide either size or nullify option, otherwise the erase command will not have any effect. The nullify option only works if specified <Symbol> is a pointer. Instead of erasing data pointed by the specified pointer nullify erases the pointer value and set it to '0' (NULL). Please note that by nullifying the pointer values may affect the debug ability of created DUMP- FILE. Use the nullify option only when the size of data to be erased is not known. e.g. data pointed by 'void *'. Let us look at the makedumpfile.conf file from the example below which was configured to erase desired kernel data from the kernel module with name mymodule. At line 1 and 3, the user has not specified size option while erasing 'array_var' and 'mystruct1.name' symbols. Instead the user depends on makedumpfile to automatically determine the sizes to be erased i.e 100 bytes for 'array_var' and 11 bytes for 'mys- truct1.name'. At line 2, while erasing the 'mystruct1.buffer' member the user has specified the size value 25 against the actual size of 50. In this case the user specified size takes the precedence and makedumpfile erases only 25 bytes from 'mystruct1.buffer'. At line 4, the size of the data pointed by void * pointer 'mystruct1.addr' is unknown. Hence the nullify option has been specified to reset the pointer value to NULL. At line 5, the 'mystruct2.addr_size' is specified as size argument to determine the size of the data pointed by void * pointer 'mystruct2.addr'. Example: Assuming the following piece of code is from kernel module 'mymodule': struct s1 { char *name; void *addr1; void *addr2; char buffer[50]; }; struct s2 { void *addr; long addr_size; }; /* Global symbols */ char array_var[100]; struct s1 mystruct1; struct s2 *mystruct2; int foo() { ... s1.name = "Hello World"; ... } makedumpfile.conf: [mymodule] erase array_var erase mystruct1.buffer size 25 erase mystruct1.name erase mystruct1.addr1 nullify # Assuming addr2 points to 1024 bytes erase mystruct1.addr2 size 1K erase mystruct2.addr size mystruct2.addr_size EOF Loop construct A Loop construct allows the user to traverse the linked list or array elements and erase the data contents referred by each element. for <id> in {<ArrayVar> | <StructVar> via <NextMember> | <ListHeadVar> within <StructName>:<ListHeadMember>} erase <id>[.MemberExpression] [size <SizeExpression>|nullify] [erase <id>...] [...] endfor where <id> Arbitrary name used to temporarily point to elements of the list. This is also called iteration variable. <ArrayVar> A simple expression in the form of <Symbol>[.member[...]] that results into an array variable. <StructVar> A simple expression in the form of <Symbol>[.member[...]] that results into a variable that points to a structure. <NextMember> Member within <StructVar> that points to an object of same type that of <StructVar>. <ListHeadVar> A simple expression in the form of <Symbol>[.member[...]] that results into a variable of type struct list_head. <StructName> Name of the structure type that can be traversed using HEAD variable <ListHeadVar> and contains a member named <ListHeadMember>. <ListHeadMember> Name of a member in <StructName>, of type struct list_head. <MemberExpression> A simple expression in the form of [.member[...]] to specify a member or component of an element in <ArrayVar>, <StructVar> or <StructName>. <SizeExpression> Size value in the form of <SizeValue>, <id>[.MemberExpression] or <Symbol>[.member[...]]. The for loop construct allows to iterate on list of elements in an array or linked lists. Each element in the list is assigned to iteration variable <id>. The type of the iteration variable is determined by that of the list elements. The entry specified after 'in' terminal is called LIST entry. The LIST entry can be an array variable, structure variable/pointer or a struct list_head type variable. The set of erase commands specified between for and endfor, will be executed for each element in the LIST entry. If the LIST entry specified is an array variable, then the loop will be executed for each array element. The size of the array will be determined by using dwarf information. If the LIST entry specified is a structure variable/pointer, then a traversal member (<NextMember>) must be specified using 'via' terminal. The for loop will continue until the value of traversal member is NULL or matches with address of the first node <StructVar> if it is a circular linked list. If the LIST entry is specified using a struct list_head type variable, then within terminal must be used to specify the structure name <StructName> that is surrounding to it along with the struct list_head type member after ':' which is part of the linked list. In the erase statement <id> then denotes the structure that the list_head is contained in (ELEMENT_OF). The below example illustrates how to use loop construct for traversing Array, linked list via next member and list_head. Example: Assuming following piece of code is from kernel module 'mymodule': struct s1 { struct *next; struct list_head list; char private[100]; void *key; long key_size; }; /* Global symbols */ struct s1 mystruct1; static LIST_HEAD(s1_list_head); struct s1 myarray[100]; void foo() { struct s1 *s1_ptr; ... ... s1_ptr = malloc(...); ... ... list_add(&s1_ptr->list, &s1_list_head); ... } makedumpfile.conf: [mymodule] # erase private fields from list starting with mystruct1 connected via # 'next' member: for mys1 in mystruct1 via next erase mys1.private erase mys1.key size mys1.key_size endfor # erase private fields from list starting with list_head variable # s1_list_head. for mys1 in s1_list_head.next within s1:list erase mys1.private erase mys1.key size mys1.key_size endfor # erase private fields from all elements of the array myarray: for mys1 in myarray erase mys1.private erase mys1.key size mys1.key_size endfor EOF In the above example, the first for construct traverses the linked list through a specified structure variable mystruct1 of type struct s1. The linked list can be traversed using 'next' member of mystruct1. Hence a via terminal has been used to specify the traversal member name 'next'. The second for construct traverses the linked list through a specified struct list_head variable s1_list_head.next. The global symbol s1_list_head is a start address of the linked list and its next member points to the address of struct list_head type member 'list' from struct s1. Hence a within terminal is used to specify the structure name 's1' that can be traversed using s1_list_head.next variable along with the name of struct list_head type member 'list' which is part of the linked list that starts from s1_list_head global symbol. The third for construct traverses the array elements specified through a array variable myarray. SEE ALSO
makedumpfile(8) makedumpfile v1.4.3 29 February 2012 MAKEDUMPFILE.CONF(5)
All times are GMT -4. The time now is 01:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy