shell: creating different arrays based on function argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell: creating different arrays based on function argument
# 1  
Old 03-19-2005
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..
# 2  
Old 03-20-2005
You probably can do this. I am not at a terminal now to play with any code but you can research this by looking at the man pages for eval and typeset , specifically, see the -A flag
# 3  
Old 03-20-2005
Try:
eval ${arrayName}[\$i]=\${line}
# 4  
Old 03-20-2005
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.
# 5  
Old 03-20-2005
Just tried it...it works for me...
Code:
#! /usr/bin/ksh

func(){
#take in 1st arg as the arrayname
arrayName=$1

let i=0
while read line
do
eval ${arrayName}[\$i]=\${line}
let i+=1
done
}

func "offsets"

echo offsets = ${offsets[*]}
exit 0
$ ./ar
one
two
three
^Doffsets = one two three
$

# 6  
Old 03-20-2005
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! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Linux/Shell script - How to compare 2 arrays based on patterns and get the differences

I have FILE 1 (This file has all master columns/headers) A|B|C|D|E|F|G|H|STATUS FILE 2 A|C|F|I|OFF_STATUS 3|4|5|4|Y 6|7|8|5|Y Below command give me all headers of FILE 2 into array2.txt file paste <(head -1 FILE2.txt | tr '|' '\n')>array2.txt So I would like to compare... (2 Replies)
Discussion started by: jmadhams
2 Replies

2. Shell Programming and Scripting

Creating script with multiple job arrays

Hello everyone, First of all this is my first post and im fairly new to working with Unix and creating scripts etc. so there will probably be wrong phrases used. Lets get to my questions. I have multiple scripts that submit Slurms/Jobs to the cluster starting like this and doing certain... (3 Replies)
Discussion started by: idbemad
3 Replies

3. Shell Programming and Scripting

Creating a text based game using shell script.

Hello guys I'm new to shell scripting and I need to make a game using shell script. I want to know if it is possible for me a total noob to shell scripting to make this game. The game concept is simple: First thing when you launch the script you get a menu in which you select if you want to... (3 Replies)
Discussion started by: Othmane
3 Replies

4. Shell Programming and Scripting

Strange third argument in shell function

In one of my shell function I found the following set_default_values () { prog=$1 PROC_DT=$2 RESET_ALL="${3-N}" #echo "Processing date as passed = , Program name = " ...... I understand the first and second arguments, but don't understand... (1 Reply)
Discussion started by: digioleg54
1 Replies

5. Programming

Creating a bash based restricted shell

Hello. I need to write a command line interface that can be invoked either directly from the shell (command sub-command arguments), or as a shell that can process sub-commands. i want to use bash auto completion for both scenarios. example: lets say my CLI module is called 'mycli' and there... (5 Replies)
Discussion started by: noamr
5 Replies

6. Shell Programming and Scripting

Need help in creating arrays using shell

Hi, I need help in creating a array in shell scirpt. I have a file which has following details. hostname devices device1 device 2 de abcdmhs10 1234 2343 2353 3343 3435 2343 bcdfmhs11 2343 2443 3434 8874 0343 3434 (5 Replies)
Discussion started by: jpkumar10
5 Replies

7. Shell Programming and Scripting

pass function as argument to a function

I have the following code : function1 () { print "January" } function2() { case $1 in January) print "Dzisiaj mamy styczen" ;; *) ;; } main() { (1 Reply)
Discussion started by: presul
1 Replies

8. UNIX for Dummies Questions & Answers

Creating a function for timestamp in shell script

I have the following code for installing a BSD application stack: #!/bin/sh # install dos2unix and unix2dos utilities echo `date "+%Y-%m-%d %H:%M:%S"` "unix2dos and dos2unix: installing..." >> ~/post_install.log pkg_add -r unix2dos # install xfce4 desktop environment echo `date "+%Y-%m-%d... (3 Replies)
Discussion started by: figaro
3 Replies

9. Shell Programming and Scripting

Joining two arrays and then creating a variable

Hello all... I'd like to create a variable from an array element from two arrays. In my search for answers I found this code for bash that joins two arrays and then started to work with it. I had got to work once and then foolishly without saving the code, I started to edit it for ksh and... (4 Replies)
Discussion started by: carlos25
4 Replies

10. Shell Programming and Scripting

Creating an unknown number of arrays

I need to create arrays like this: cnt=0 { while read myline; do if ];then firstpass="${myline##<meas>}" meas="${firstpass%%</meas>}" tempmeas="${meas%%;*}" MEAS$cnt=$tempmeas print $cnt print ${MEAS'$cnt'} ... (2 Replies)
Discussion started by: ajgwin
2 Replies
Login or Register to Ask a Question