Sponsored Content
Top Forums Shell Programming and Scripting Adding an element to a bash array with a variable Post 302937958 by LMHmedchem on Wednesday 11th of March 2015 01:39:25 AM
Old 03-11-2015
Quote:
Originally Posted by Chubler_XL
This seems to be working fine for me:

Code:
$ CMD_STRING='-L 22 -s 0 -r -O -A i -N 100 -n'
$ CMD_STRING_LIST=(1 2 3)
$ CMD_STRING_LIST=("${CMD_STRING_LIST[@]}" "$CMD_STRING")
$ echo ${#CMD_STRING_LIST[@]}
4
$ echo ${CMD_STRING_LIST[3]}
-L 22 -s 0 -r -O -A i -N 100 -n

I wonder if your doing this all within a loop and loosing the changes due to Bash sub-shell: variables lost
Yes it is in a loop. I am trying to accumulate a list of commands to run later.

The post you linked to is interesting. I am sure I have done this kind of thing before, meaning accumulated data in an array from inside a loop. A loop is the most likely place where you would want to push allot of data into a structure. I do remember having some issues with the syntax before since I don't do it very often, but I don't remember it not working. Perhaps it matters where the array is declared. Is this like c++ where a variable declared in {} will not be in scope outside of the {}?

I will try to look for some other scripts where I have this method working.

LMHmedchem
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding array element in KSH

All, I would like to add the first 10 elements of an array. Here is how I am doing it now (only included first few add ops): #!/usr/bin/ksh ###Grab the array values out of a file### TOTAL=`awk '/time/' /tmp/file.out | awk '{print $4}'` set -A times $TOTAL SUM=$((${times} + times... (3 Replies)
Discussion started by: Shoeless_Mike
3 Replies

2. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies

3. Windows & DOS: Issues & Discussions

Array element & string variable

Hello, I am trying to assign $string = $a; #a= 10100 print " $string"; # showing 10100 I am using this $string as $result = index($string, $char, $offset); As above, is not capturing binary nos. in following format; $string = '10100'; so index is not working. kindly suggest. (5 Replies)
Discussion started by: shristi
5 Replies

4. Shell Programming and Scripting

How to assign an array element to a variable

Hi every one.. I'm new to shell scripting... I would like to assign a single array element to a variable... Is it possible to do it.... Could any body help me.... (3 Replies)
Discussion started by: kaushik_87
3 Replies

5. Shell Programming and Scripting

Creating bash array name from variable

Hi gurus, I need to create arrays from variables, via a loop. The issue I have is with the array name creation. How do I use a variable to define an array? I want to do something like declare -a $H where $H is my loop variable. I then need to add items to each array I've created,... (3 Replies)
Discussion started by: melias
3 Replies

6. Shell Programming and Scripting

Adding Content to Variable (bash)

is this possible? its kind of like incrementing the value of a number in a variable. but in this case, instead of the value of the variable being a number, it's just contents/strings/characters/alpha-numeric etc. NOT a number. For instance: VAR=Tommy for all in $(blah blah) do ... (2 Replies)
Discussion started by: SkySmart
2 Replies

7. Shell Programming and Scripting

Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance. let me be specific. here's my code: # URL port Variables port2195=`nc -z $url2195 2195` port2196=`nc -z $url2196 2196`... (5 Replies)
Discussion started by: hungryd
5 Replies

8. UNIX for Advanced & Expert Users

Array Element

This question is for someone that's more familiar with Array Element. I need to know if the maximum array element that can be assigned is 1024 and if its so, Is there a workaround solution when the counter exceeded 1024? param_array="$param_nam" counter=$counter+1 #to avoid space... (3 Replies)
Discussion started by: cumeh1624
3 Replies

9. UNIX for Beginners Questions & Answers

Unset array element and save to file in Bash

#!/bin/bash X=(2H 4S 10D QC JD 9H 8S) How do I unset the 10D from this array and save it to a file? Please use CODE tags as required by forum rules! (5 Replies)
Discussion started by: cogiz
5 Replies

10. UNIX for Beginners Questions & Answers

Adding to an array in an external file, and adding elements to it.

I have an array in an external file, "array.txt", which contains: char *testarray={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};I want to be able to add an element to this array, and have that element display, whenever I call it, without having to recompile... (29 Replies)
Discussion started by: ignatius
29 Replies
foreach(n)						       Tcl Built-In Commands							foreach(n)

__________________________________________________________________________________________________________________________________________________

NAME
foreach - Iterate over all elements in one or more lists SYNOPSIS
foreach varname list body foreach varlist1 list1 ?varlist2 list2 ...? body _________________________________________________________________ DESCRIPTION
The foreach command implements a loop where the loop variable(s) take on values from one or more lists. In the simplest case there is one loop variable, varname, and one list, list, that is a list of values to assign to varname. The body argument is a Tcl script. For each element of list (in order from first to last), foreach assigns the contents of the element to varname as if the lindex command had been used to extract the element, then calls the Tcl interpreter to execute body. In the general case there can be more than one value list (e.g., list1 and list2), and each value list can be associated with a list of loop variables (e.g., varlist1 and varlist2). During each iteration of the loop the variables of each varlist are assigned consecutive values from the corresponding list. Values in each list are used in order from first to last, and each value is used exactly once. The total number of loop iterations is large enough to use up all the values from all the value lists. If a value list does not contain enough elements for each of its loop variables in each iteration, empty values are used for the missing elements. The break and continue statements may be invoked inside body, with the same effect as in the for command. Foreach returns an empty string. EXAMPLES
This loop prints every value in a list together with the square and cube of the value: set values {1 3 5 7 2 4 6 8} ;# Odd numbers first, for fun! puts "Value Square Cube" ;# Neat-looking header foreach x $values { ;# Now loop and print... puts " $x [expr {$x**2}] [expr {$x**3}]" } The following loop uses i and j as loop variables to iterate over pairs of elements of a single list. set x {} foreach {i j} {a b c d e f} { lappend x $j $i } # The value of x is "b a d c f e" # There are 3 iterations of the loop. The next loop uses i and j to iterate over two lists in parallel. set x {} foreach i {a b c} j {d e f g} { lappend x $i $j } # The value of x is "a d b e c f {} g" # There are 4 iterations of the loop. The two forms are combined in the following example. set x {} foreach i {a b c} {j k} {d e f g} { lappend x $i $j $k } # The value of x is "a d e b f g c {} {}" # There are 3 iterations of the loop. SEE ALSO
for(n), while(n), break(n), continue(n) KEYWORDS
foreach, iteration, list, looping Tcl foreach(n)
All times are GMT -4. The time now is 09:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy