LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to call a array dynamically into a script (https://www.linuxquestions.org/questions/programming-9/how-to-call-a-array-dynamically-into-a-script-892752/)

subbuyt 07-20-2011 10:24 AM

How to call a array dynamically into a script
 
Hi All,
I have a script which takes an array as an input to the file.
ex: test.ksh -l <array_value>

Every time I dont get the same array name.
EX: test.ksh -l x ; where x="a b"
test.ksh -l y ; where y="c d"

I store the value I get in l in varaible myLvalue

Now indise my Script I want to run a for loop for the value I get in -l.

If I write the for loop as below. I'm getting value x.
However I'm expecting the loop to run for a and b.

for getLvalue in $myLvalue
do
echo $getLvalue #I'm expecting a and b to print in sequence
done.

Please help.

David the H. 07-20-2011 11:23 AM

What you posted is pretty much meaningless to me. I can't follow what you're saying at all. I doubt you're going to get any real help unless you show us some actual code, some real input values, and some background information on what the script is supposed to be doing vs. what it's actually doing.

And please use [code][/code] tags around your code, to preserve formatting and to improve readability.

AnanthaP 08-23-2011 01:14 AM

I think it is a positional parameter "shift" or $# command for positional parameters.

Quote:

Now indise my Script I want to run a for loop for the value I get in -l.
Start from $3 ($2 being -l and $3 being "where").
Follows the pseudo-code
Code:

K=3
Do until K <= $#
  Write $0 $1 $2 $k "\n"
  k=k+1
End Do


grail 08-23-2011 01:34 AM

Well my first question back would be, what does -l do? I searched through 2 man pages for ksh and could find only one
reference which was to switch uppercase letters to lower (not sure I could see the point here) so I figure it must
be something else you are trying to do?

Next, you say you are passing in an array and yet you say that x="a b" which is a string with a space in it?

Next, assuming that you store the value x in myLvalue, then you would at least need to do an indirect call to see what is stored in x.

Finally, You say you wish to loop over an array and yet you refer to your variable as a common variable, ie not of the format ${myLvalue[@]} ... or similar, so how is this looping over an array?

Nylex 08-23-2011 01:37 AM

The OP hasn't been back here since they posted, so they might not return. This thread was only bumped by a spammer..

Edit: In case anyone's wondering, the spammer's post has been removed.

grail 08-23-2011 09:24 AM

Sorry ... my bad ... didn't look at that :redface:

Ramurd 09-08-2011 03:02 AM

I bet the threat is dead and I'm actually replying to some advertisers now :-) But the issue posted by the OP made me post, really!

One of the problems with calling your script like this:

Code:

./test.ksh -l x ; where x="a b"
The part after the semicolon is not going to be parsed to your script; The semicolon is parsed by the shell first, and has the special meaning. Read up on that.

Your script indeed makes no sense to me, right now. I have no clue what you may want to accomplish and why you would want to do it this way... write a scripting language into a scripting language?

You could change things a bit around; assuming -l is the parameter for "array name", you could use -w for your "where" clause:
Code:

./test.ksh -l x -w x="a b"
With getopts you can parse your parameters and their values; but why you'd explicitly want to have your variable name in a variable itself is beyond me...

acid_kewpie 09-08-2011 06:33 AM

closed to stop spamming


All times are GMT -5. The time now is 03:51 AM.