For loop; how to construct a array with variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop; how to construct a array with variables
# 1  
Old 04-10-2013
Linux For loop; how to construct a array with variables

Hi everybody!!

Here is the thing; I have a trouble in this simple situation, I'm trying to write an array with all the arguments of a command. I mean, if I have:
Code:
./mycommand.sh aa bb cc dd

I need to take an array like this:
myarray=(aa bb cc dd)

So I use a simple for loop like this:

Code:
for i in `seq 1 $#`;
do
myarray[i]=THISISMYQUESTION
done

I don't know how to construct the expression to take the i variable with its growing values (1, 2, 3, ... n) and become it in the variable who represents the argument ($1, $2, $3, ... $n) and save the arguments values into the array (aa, bb, cc, ... whatever).
If I do
Code:
myarray[i]=$i

of course I'll have myarray=(1 2 3 4), not the needed array myarray=(aa bb cc dd)

Can someone help me?

Thanks

Andres
# 2  
Old 04-10-2013
array

Hey!!!

For your question,

Code:
for i in `seq 1 $#`;
do
myarray[i]=${i}
done

Here $i represents the arguments which is taken one by one and assigned to myarray in an index of argument possition.

You don't need to loop the arguments to create array, check this out,

Code:
#! /usr/bin/env bash

declare -a myarr

myarr=($@)
echo "${myarr[0]}"

For your information,

Just assume, if you assign the array like below,
Code:
myarray[0]='aa'
myarray[1]='bb'

You output array should be myarray=('aa' 'bb') and these 0,1 represent the indexes which represents the position of array element.

Cheers,
RangaSmilie
# 3  
Old 04-10-2013
Hi Rangarasan. Thanks a lot.

Code:
myarr=($@)

This works great!! Thanks!!

But This doesn't work as I need, see:

Code:
for i in `seq 1 $#`;
do
   echo ${i}
done

This is the output
Code:
$ ./wrapper.sh aa bb cc
1
2
3

I still can't use the argument's variables like $1, $2, $3, ...$n into the for loop.

Regards,

Andrés
# 4  
Old 04-10-2013
try this:

Code:
for i in {1..$#}
do
   eval echo $`echo $i`
done

This User Gave Thanks to stuckinboredom For This Post:
# 5  
Old 04-11-2013
Thanks a lot stuckinboredom !!
The code
Code:
eval echo $`echo $i`

works fine!!

Regards,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Construct Array from String seperated by different whitespaces

My string variable which gets the output from the result of a database query has values as below: line="2019-09-11 15:17:55 CR1234 anonymous Deployed DR_only Back_APP" I wish to construct an array (my_array) which should have entries as below. Note: 1. The first... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. UNIX for Beginners Questions & Answers

Bash array variables are changed in loop runtime

I am trying to check whether particular host and port are responding or not. I am using below script to check. but node_port array that i am using in loop is getting replaced with previous iteration value. Script and output is given. Please help me to understanding why node_port values are... (5 Replies)
Discussion started by: tmalik79
5 Replies

3. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. Shell Programming and Scripting

array variables

Hi, There are total 100 files in the backup directory with the name format as below: prod_bkp_140611_13_30_05.txt prod_bkp_140611_14_30_05.txt prod_bkp_140611_15_30_05.txt prod_bkp_140611_16_30_05.txt How to use array variables (in perl) to get list of names of the above files ? (13 Replies)
Discussion started by: milink
13 Replies

6. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

7. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

8. Shell Programming and Scripting

Getting variables into a array.

Hi , Im trying to monitor 2 instancesof a process on our solaris server and trying to do a notification if the returned process size is greater than 500M. Since there are two variables returned I want to make use of arrays to check each and every variable that is stored. the issue that im facing... (2 Replies)
Discussion started by: vivsiv
2 Replies

9. Shell Programming and Scripting

declare, assign variables using array, counter, loop

using bash Tru64... converting DCL to shell... any tips to make this work would be greatly appreciated. Below is my failed attempt to assign command line input to variables by first declaring an array. I use a counter to create unique variables in a loop through the array. I need to call... (3 Replies)
Discussion started by: egkumpe
3 Replies

10. Shell Programming and Scripting

Array variables

hi, how to store array values in unix? and how to access array values? can somebody help? kavitha (2 Replies)
Discussion started by: kavitha
2 Replies
Login or Register to Ask a Question