![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Creating an unknown number of arrays | ajgwin | Shell Programming and Scripting | 2 | 05-23-2008 03:49 PM |
| Passing more than one argument in a function | jisha | Shell Programming and Scripting | 1 | 04-23-2008 06:05 AM |
| Pass argument to function | Knotty | UNIX for Dummies Questions & Answers | 1 | 04-08-2007 08:26 AM |
| creating email based on an output | saswerks | Shell Programming and Scripting | 1 | 02-14-2005 06:57 PM |
| Passing Argument to Function | AkumaTay | UNIX for Dummies Questions & Answers | 2 | 10-18-2001 06:24 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
bash shell: creating different arrays based on function argument
hi, I was wondering if there was a good way to create an array within a function, where the name is based on a passed argument? I tried this:
_____________________________ func(){ #take in 1st arg as the arrayname arrayName=$1 let i=0 while read line do arrayName[$i]=${line} let i+=1 done } func "offsets" _______________________________ So I was trying to name the array "offsets", but the way in which I have it just renames the array to whatever is in the first line of the file. I want to be able to call the function multiple times and set different array names. adding a dollar sign to just reference the name doesn't work: $arrayName[$i]=${line} thanks for any help. Last edited by nix21; 03-19-2005 at 03:30 PM.. |
|
||||
|
Perderabo: I gave it a try but it just placed the name of the array (offsets) in the first postion and did not put anything else in the array.
google: I'll take a look at those. I tried eval a couple times, but nothing so far. I did come up with a workaround. I just changed the original code I posted, by starting the variable 'i' at 1 instead of 0. This allows the name of the array to be placed at the 0 postion and the data from 1...n. I just have to be aware in the rest of the script that the data will always start at 1. I would still like to find another way, without having to use the little workaround though. |
|
||||
|
yeah your right it works. I have no idea what the problem was, I copied the specific line in and it didn't work with everything else the same. But then copied in the whole function and it worked fine. maybe a null character or something.
thanks, its much appreciated! ![]() |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|