LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 07-20-2011, 10:24 AM   #1
subbuyt
LQ Newbie
 
Registered: Jul 2011
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 07-20-2011, 11:23 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 08-23-2011, 01:14 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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
 
Old 08-23-2011, 01:34 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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?
 
Old 08-23-2011, 01:37 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.

Last edited by Nylex; 08-23-2011 at 11:02 AM.
 
Old 08-23-2011, 09:24 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Sorry ... my bad ... didn't look at that
 
Old 09-08-2011, 03:02 AM   #7
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
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...
 
Old 09-08-2011, 06:33 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
closed to stop spamming
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash - read array dynamically Garcia16 Programming 16 09-26-2010 01:50 AM
local dynamically allocated array in fortran otoomet Programming 5 10-21-2009 08:28 AM
Dynamically set array length in Fortran77? halfpower Programming 2 12-15-2005 08:03 AM
In C, using qsort for dynamically allocated array ntmsz Programming 7 08-23-2005 10:33 AM
dynamically expanding an array in c++ a1ghagh05t Programming 4 03-07-2004 09:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration