Variable substitution in array printing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable substitution in array printing
# 1  
Old 09-03-2014
Variable substitution in array printing

Hi folks,

A really dumb question as I've wasted far too long trying to get this to work.... (on RH bash)

I have an array:

Code:
m0[1]='<hello>'
m0[2]='<there>'
m0[3]='<fred>'

v0[1]='<goodbye>'
v0[2]='<again>'
v0[3]='<john>'

in my code I calculate the value of the variable to output and if I echo it, I get m0 or v0, but if I try and print the variable, I either get 3 as an output, or bad variable substitution, or no output.

Code:
 # $oc evaluates to m0 or v0

eval "printf '%s\n' \"\${#${oc}[@]}\""  (displays 3 as output)
#printf -- '%s\n' "${m0[@]}"  (displays correctly (when uncommented) but was just a test)
printf -- '%s\n' "${oc[@]}"  (no output)
printf -- '%s\n' "${$oc[@]}"  (bad variable substitution)

Thanks in advance - and apologies for the very basic question.
# 2  
Old 09-03-2014
If you want to print it your will need to again use eval:
Code:
eval "printf '%s\n' \"\${${oc}[@]}\""

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 09-03-2014
Beware that, if your array contains an element like `rm -Rf ~/` eval will happily execute that code and delete your home directory. It evaluates all shell syntax, not just variables, but things in backticks and $( ) and > and any other possible syntax which might cause a malfunction, create unintended files, and/or blow up with syntax errors.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help with array substitution

Hello all, I have following piece of code which is working fine if executed standalone - date=$1 set -A max_month 0 31 28 31 30 31 30 31 31 30 31 30 31 eval $(echo $date|sed 's!\(....\)\(..\)\(..\)!year=\1;month=\2;day=\3!') (( year4=year%4 )) (( year100=year%100 )) (( year400=year%400 ))... (7 Replies)
Discussion started by: ektubbe
7 Replies

2. UNIX for Dummies Questions & Answers

printing array elements

Is there a way to print multiple array elements without iterating through the array using bash? Can you do something like... echo ${array}and get all those separate elements from the array? (2 Replies)
Discussion started by: jrymer
2 Replies

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

4. Shell Programming and Scripting

Substitution in a file dont work with an Array in filename

Hi. I´ve a script that should substitude the 8th line in a file called xxx.num6. The "xxx" is set by an array filled with this command: j=0 for Par in *.sys ; do Par=`echo $Par | sed 's/\(.*\).sys/\1/'` ; Par2="$Par" ; echo "${Par2}" j=$((j + 1)); done Now i try... (0 Replies)
Discussion started by: Lock3
0 Replies

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

6. Shell Programming and Scripting

Array reference - bad substitution

I've created a series of arrays named as follows: row1 row2 row3 . . . row10 Each has 4 elements. I'm trying to echo the array elements out in a for loop. Here's what I have: for ((i=1;i<=10;i++)) do for ((j=1;j<=4;j++)) do eval out=${row`echo $i`} echo -n $out (3 Replies)
Discussion started by: swankgd
3 Replies

7. Shell Programming and Scripting

Tricky array substitution in functions

Hello, Please tell me if there is a better way to get the number of elements from an array that is passed to a function. This is what works on Solaris 8 (ksh) but it looks odd: loop_array() { array_name=$2 b1='\${\#' b2='}' nr_elements=`eval echo... (6 Replies)
Discussion started by: majormark
6 Replies

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

9. Shell Programming and Scripting

Array Printing Inline

Dear friends , The output file of below script Pls#!/bin/sh awk '{ bo = substr($0,13,3) slm = substr($0,150,8) slo = substr($0,175,7) inc = substr($0,97,10)/100 busi = substr($0,83,10) mth = substr($0,39,2) yer = substr($0,35,4) ... (2 Replies)
Discussion started by: vakharia Mahesh
2 Replies

10. Shell Programming and Scripting

Array help (variable substitution).

I am using an array (clmlist01). I have 61 of these and have 4 or more references to each one in a block of code that I do not want to have to hardcode. With that being said, I am creating a varible and going through a for loop to create the actually name of each array. The arrays would end up... (3 Replies)
Discussion started by: dsimpg1
3 Replies
Login or Register to Ask a Question