I guess the 'last' acctualy is the 'nr_elements' or you have something missed.
If it is, let see what the 'last' has:
You have build correct string "\${\#$array_name[@]}"; asked to 'eval'uate it - have in result "${#<arr_name>[@]}"; and asking to execute it!
Try execute this line with correct array name: you will have :command not found!
Because it will produce a number of the array elements and a number is not a command by itself.
I did not clear understand what do you need: the correct number of element for any given array or the command itself into a variable?
If the array element number, you do not need to escape the characters in b1 and b2 (acctualy, I do not see why do you need them) :
I do not see any reason to build a command itself and after that execute it. It will required double execution, as you already figured it out.
So, you have an array name in a variable and would like to call a function that returns the aray elements number? A function need to be executed to get a result. So:
To get just a "echo $nr_elements" the nr_elements should be a variable, which you could set in a function. So
That what you was looking for?
Last edited by alex_5161; 11-06-2008 at 02:24 PM..
This is partially what I wanted, thank you for the tips.
What I need is to use the variable inside the function itself not outside.
For example:
The rest of the function continues with a loop through the array backwards(decrementing the number of lines variable), so that is why I need the variable to be set inside the function.
Just recall this last pointed by Jim try to send an array by value, instead of by reference (in shell - just the name.)
The point is that it could be done in correct way, but with appropriate call syntaxis:
Hello,
I think there's an easier way to do this but can't seem to recall but given an array of animals and an initial value is a random index in the array, here it's 3.
3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0... inifinite repeat
a quick brute force solution i came up with was two functions, i... (6 Replies)
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)
I have a question on how can I assign a output of a function to a variable which is executed in background.
Here is my example
$ cat sample_program.sh
#!/bin/ksh
exec_func () {
sleep 1
v=`expr $1 + 100`
print $v
}
export OUT_ARR
date
for i in 1 2
do
OUT_ARR=`exec_func $i` &... (1 Reply)
I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line
if
then... (8 Replies)
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)
Let me know if there is a way to pass array to a funtion in ksh script.
function isPresent
{
typeset member
member=$1
dbList=$2
echo '$1:' $1
echo '$2' $dbList
The array will be at the second position....something like this
isPresent 12 <array>
if
then
echo... (3 Replies)
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)
Please excuse my ineptitude for a bit as I've been spoiled for the past few months with only writing perl code instead of C.
So ok, I've been thinking about some code to change the crc32 values that are held within central directory headers of zip files.
Because I'm lazy I decided to just... (3 Replies)
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)