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 being cmlist01, cmlist02, cmlist03, etc. I don't have any problem creating the variables, but I seem to be doing something wrong in the syntax because if I use ${#cmlist01[@]} it gives me 11, but if I try to resolve the variable substituion name (listnbr), it resoves to cmlist01, but the ${#listnbr[@]} always returns 1. Is it possible to do what I am trying, or do you have to always hardcode the actual array name (instead of resoving it on the fly)? Help with the syntax please!
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 being cmlist01, cmlist02, cmlist03, etc. I don't have any problem creating the variables, but I seem to be doing something wrong in the syntax because if I use ${#cmlist01[@]} it gives me 11, but if I try to resolve the variable substituion name (listnbr), it resoves to cmlist01, but the ${#listnbr[@]} always returns 1. Is it possible to do what I am trying, or do you have to always hardcode the actual array name (instead of resoving it on the fly)? Help with the syntax please!
cmlist01 has a b c d e
cmlist02 has f g h i
cmlist03 has j k l
For the total in cmlist01, I should see 5 items.
For the total in cmlist02, I should see 4 items.
For the total in cmlist03, I should see 3 items.
As a result, I could do:
print "There are ${#cmlist01[@]} items in $listnbr:"
which prints "There are 5 itmes in cmlist01."
However, I want to do all of this in a for loop and have it derive the cmlistnn from the variable $listnbr (which is does just fine as evidenced in the output from the print statement). However, if I try to get the count (number of items in the array) using listnbr (instead of hardcoding cmlistnn in each statement), all I ever get is 1.
If I set the variable listnbr=cmlist01, I should see 5 items, but am only seeing 1 when I do:
print "There are ${#listnbr[@]} items in $listnbr:"
which prints "There are 1 itmes in cmlist01."
The main part of the code that I need this for is:
cmlist01 has a b c d e
cmlist02 has f g h i
cmlist03 has j k l
For the total in cmlist01, I should see 5 items.
For the total in cmlist02, I should see 4 items.
For the total in cmlist03, I should see 3 items.
As a result, I could do:
print "There are ${#cmlist01[@]} items in $listnbr:"
which prints "There are 5 itmes in cmlist01."
However, I want to do all of this in a for loop and have it derive the cmlistnn from the variable $listnbr (which is does just fine as evidenced in the output from the print statement). However, if I try to get the count (number of items in the array) using listnbr (instead of hardcoding cmlistnn in each statement), all I ever get is 1.
If I set the variable listnbr=cmlist01, I should see 5 items, but am only seeing 1 when I do:
print "There are ${#listnbr[@]} items in $listnbr:"
which prints "There are 1 itmes in cmlist01."
The main part of the code that I need this for is:
Maybe you just can't do what I'm trying to do...
This User Gave Thanks to cfajohnson For This Post:
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)
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:
m0='<hello>'
m0='<there>'
m0='<fred>'
v0='<goodbye>'
v0='<again>'
v0='<john>'
in my code I calculate the value of the variable to output and if I echo it, I... (2 Replies)
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)
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)
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)
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)
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)
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)
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)
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)