Sponsored Content
Top Forums Shell Programming and Scripting Variable substitution with arrays Post 303002463 by Kingzy on Thursday 24th of August 2017 04:59:36 PM
Old 08-24-2017
Variable substitution with arrays

Hi all,

I have a script with the following gist:

Code:
declare -a index=(0 1 2 3 4);

declare -a animals=(dog cat horse penguin cow);

declare -a fruits=(orange apple grapes peach mango);

declare -a drinks=(juice milk coffee tea coke);

declare -a cities=(toronto paris london glasgow sydney);

declare -a countries=(canada france england scotland australia);

declare -a all=(animals fruits drinks cities countries);

for i in "${index[@]}" ; do
	echo ${i};
	animals="${animals["${i}"]}";
	echo $animals;
	fruits="${fruits["${i}"]}";
	echo $fruits;
	drinks="${drinks["${i}"]}";
	echo $drinks;
	cities="${cities["${i}"]}";
	echo $cities;
	countries="${countries["${i}"]}";
	echo $countries;
done

And this is the output:

Code:
0
dog
orange
juice
toronto
canada
1
cat
apple
milk
paris
france
2
horse
grapes
coffee
london
england
3
penguin
peach
tea
glasgow
scotland
4
cow
mango
coke
sydney
australia

Because my actual script has more arrays to loop through and I'll be adding more as time goes on, I came up with an 'all' array that has the array names in it. Then I attempted to loop through each array as such:

Code:
for i in "${index[@]}" ; do
	echo ${i};
	for j in "${all[@]}" ; do
		echo ${!j[${i}]};
	done
done

But this time I'm only getting items at index 0, as per the output below:

Code:
0
dog
orange
juice
toronto
canada
1





2





3





4





abc@xyz$


I've been struggling with this one for a while now, and I'm not entirely sure what I'm trying to accomplish is possible with Bash. Any help is appreciated!


Thanks!
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Substitution in a variable

Hey All, I'm trying to clean up a variable using sed but It dosn't seem to work. I'm trying to find all the spaces and replace them with "\ " (a slash and a space). For Example "Hello World" should become "Hello\ World". But it does nothing. If I put it directly into the command line it works... (3 Replies)
Discussion started by: spragueg
3 Replies

2. UNIX for Dummies Questions & Answers

variable substitution

Hi everyone, I have a simple question to ask : In a script that I'm writting, I need to create variables on-the-fly. For instance, for every iterartion of the following loop a var_X variable should be generated : #!/bin/ksh a="1 2 3" for i in $a do var_${i}=$i echo "${var_$i}" done ... (1 Reply)
Discussion started by: ck-18
1 Replies

3. Shell Programming and Scripting

Variable Substitution

I have run into a wall with my iptables firewall scripting. I am blocking all of the private side IP addresses on the WAN interface on systems running NAT. However, if the system is not running NAT and needs to allow access to the local LAN on the WAN interface, I need to block all but one of... (2 Replies)
Discussion started by: garak
2 Replies

4. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

5. UNIX for Dummies Questions & Answers

Variable substitution

Hi, That might be pretty simple. How can I generate a variable name and get their value ? Thanks a lot. Something like: >CUSTOMER_NF=26 > object=CUSTOMER > echo ${object}_NF CUSTOMER_NF > echo ${${object}_NF} ksh: ${${object}_NF}: 0403-011 The specified substitution is... (7 Replies)
Discussion started by: Leo_NN
7 Replies

6. Shell Programming and Scripting

variable substitution

file1.ksh #!/bin/ksh test5_create="I am a man" # test5 will be dynamic and the value will be passed from command line a=${1}_create echo $a # i need the output as "I am a man" ./file1.ksh test5 # i run the script like this any suggessions guys... (1 Reply)
Discussion started by: giri_luck
1 Replies

7. Shell Programming and Scripting

bash: combine arrays with weird substitution/references

Hi all. I'm trying to finish a bash script with the following elements: ARRAY="blah $ITEM blah blah" ARRAY="blah blah $ITEM blah bluh" #ARRAY="...." # ...the ARRAY elements represent a variable but defined # syntax and they're all hard-coded in the script. #(...) ITEMS='1.0 2.3... (2 Replies)
Discussion started by: yomaya
2 Replies

8. Shell Programming and Scripting

Variable Substitution

Hi , I have a variable as follows, Temp=`cat ABC.txt | cut -c5-` This will yeild a part of the date. say , 200912. I would like to substitute this variable's value in a filename. eg: File200912F.zip when i say File$TempF.zip , it is not substituting. Any help ? Thanks in... (2 Replies)
Discussion started by: mohanpadamata
2 Replies

9. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

10. Shell Programming and Scripting

Variable substitution

Hi, I have to write a shell script in which I have to substitute a variable within a variable. For example, var1=aaa var2=file.$var1.txt The output should be, echo $var2 file.aaa.txt Can someone please help me in getting this. I tried using eval, but it didnt work. I might be using it... (2 Replies)
Discussion started by: grajp002
2 Replies
expack(1)						      General Commands Manual							 expack(1)

NAME
expack - manipulate Flex files SYNOPSIS
expack -a FLEXFILE FILE... expack -c FLEXFILE FILE... expack -x FLEXFILE [INDEX] expack -i MANIFEST expack -l FLEXFILE DESCRIPTION
expack is a command-line utility that lets you extract and create Flex files. Flex files are archives (which means that they act as con- tainers for other files, somewhat like directories) used by Ultima 7 to store most of its data. Note that the names of the original files are not stored in the Flex -- files are only retrievable by their index. Modes of operation -a Add --NOT IMPLEMENTED YET. FUTURE ENHANCEMENT-- This command adds one or more FILEs to the end of FLEXFILE. -c Create This command takes one or more FILEs and bundles them into FLEXFILE, overwriting it in the process. -x Extract Lets you extract the files contained within FLEXFILE. If INDEX is given only the file at this position will be unpacked, otherwise all files are extracted. The created files will be named index.u7o. -i Input Similar to "Create", this command creates FLEXFILE from a number of input files. However, "Input" takes the names of the input files from MANIFEST instead of the command line. MANIFEST must be a list of file names, one per line. The first name is that of the Flex file that is to be created, the rest are the input files used in building it. -l List This command lists the contents of a FLEXFILE. It gives you the number of files contained within the Flex file, as well as the length of each file. EXAMPLES
expack -a animals.flx bee.wav Adds bee.wav to the Flex file animals.flx. expack -a animals.flx hamster.wav snake.wav donkey.wav Adds hamster.wav, snake.wav, and donkey.wav to the Flex file animals.flx. expack -c animals.flx duck.wav cat.wav cow.wav dog.wav Creates a new Flex file animals.flx containing duck.wav, cat.wav, cow.wav, and dog.wav. expack -x text.flx 17 Extracts the file at position 17fromtext.flxinto17.u7o. expack -x text.flx Extracts all files from text.flx into files named 0.u7o, 1.u7o, 2.u7o, etc. expack -i wavlist With wavlist reading: jmsfx.flx ant.wav bee.wav cat.wav will create a new Flex file jmsfx.flx consisting of ant.wav, bee.wav, andcat.wav. expack -l text.flx Lists the number and lenghts of files contained in text.flx. BUGS
The "Add" command (-a) does not work yet. AUTHOR
This manpage was written by Robert Bihlmeyer based on a text by Marc Winterrowd. It may be freely redistributed and modified under the terms of the GNU General Public License version 2 or higher. Exult 2002-03-23 expack(1)
All times are GMT -4. The time now is 01:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy