Variable substitution with arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable substitution with arrays
# 8  
Old 08-25-2017
My GNU bash, version 4.3.46(1)-release (x86_64-pc-linux-gnu) has typeset -n, my GNU bash, version 4.1.11(1)-release (amd64-portbld-freebsd9.0) doesn't.

@Kingzy: Unfortunately you don't tell us which code snippet led to the unsatisfying output. You may need to redefine ALL the arrays and unset any variables used before you rerun any of the scripts given in here to be sure that the start conditions are identical. One warning: it's a bad idea to use like
Code:
    animals="${animals["${i}"]}";

because the array will be modified sort of randomly depending on the i - value.
# 9  
Old 08-25-2017
Don: My apologies for the initial lack of info. I am also using MacOS Sierra.

I also tried running that script on my work shell.

Code:
[work@shell ~]$ ./loop.sh
./loop.sh: line 17: typeset: -n: invalid option
typeset: usage: typeset [-aAfFgilrtux] [-p] name[=value] ...
0
dog, animals
orange, fruits
juice, drinks
toronto, cities
canada, countries
1
dog,
orange,
juice,
toronto,
canada,
2
dog,
orange,
juice,
toronto,
canada,
3
dog,
orange,
juice,
toronto,
canada,
4
dog,
orange,
juice,
toronto,
canada,
[work@shell ~]$ bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Quote:
which just switches the inner and outer loops in the code I suggested before, changes the names of the arrays used as looping values, and adds code to add an empty line between groups. The above produces the output requested in post #4.
Thanks for the edit. I unfortunately do not have the option to rewrite the script my contrived example is based on from scratch. But I'll surely give ksh a try on another project! It seems to be more flexible than bash.

---------- Post updated at 05:45 PM ---------- Previous update was at 05:35 PM ----------

RudiC: Here is the code with typeset -n that I wasn't lucky with:

Code:
#!/bin/sh

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);

typeset -n j

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

It looks like neither shell at home or work has typeset -n Smilie
# 10  
Old 08-25-2017
Try again with the shebang in the first line set to #!/bin/bash or whatever the correct path be.
This User Gave Thanks to RudiC For This Post:
# 11  
Old 08-30-2017
RudiC: Thanks a lot! Changing the bash path did the trick, at least on my home machine.

I tried with the following 2 paths:

#/usr/local/Cellar/bash/4.3.33/bin/bash


#/usr/local/bin


Alas, it looks like the shell at my workplace doesn't have typeset. I tried running the script with the following path:

#/usr/local/bin
# 12  
Old 08-31-2017
Try whereis bash (or whereis ksh) or which bash or locate bash ...
# 13  
Old 08-31-2017
RudiC: Yup I had tried echo $PATH before. Here is the output with the other commands:

Code:
[abc@xyz ~]$ which bash
/usr/bin/bash
[abc@xyz ~]$ whereis bash
bash: /usr/bin/bash /usr/share/man/man1/bash.1.gz
[abc@xyz ~]$ echo $PATH
/usr/local/bin:/usr/bin
[abc@xyz ~]$ locate bash
/usr/bin/bash
/usr/bin/bashbug
/usr/bin/bashbug-64
/usr/share/doc/bash-4.2.46
[abc@xyz ~]$

NB: locate bash dumped other paths which I removed from my copy-paste.
# 14  
Old 08-31-2017
Only after writing my last post here i realised that you could construct a sort-of two-dimensional array out of delimited strings. Consider the following:

Code:
typeset array[1]="1-1:1-2:1-3"
typeset array[2]="2-1:2-2:2-3"
typeset array[3]="3-1:3-2:3-3"

typeset -i i=1
typeset -i j=1

# display row-wise
for i in 1 2 3 ; do
     for j in 1 2 3 ; do
          echo "${array[$i]}" | cut -d':' -f $j
     done
done

# display column-wise
for i in 1 2 3 ; do
     for j in 1 2 3 ; do
          echo "${array[$j]}" | cut -d':' -f $i
     done
done

You separate the columns by some delimiter (here ":") and use this as a means to create several entries into a single string.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question