Issue with array in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with array in shell
# 1  
Old 01-31-2012
Issue with array in shell

So i have used arrays for a while now but I just notice in one of my arrays when using an array with 9+ items in it that after running a for loop on the array it was replacing the first array with what ever was last and I cant figure out why.


Here is my code.

Code:
#!/bin/bash
vm=( 0 1 2 3 4 5 6 7 8 9 10 )

echo before for loop array=${vm[@]}
for vm in ${vm[@]}
do
	echo $vm
done
echo after for loop array=${vm[@]}




before for loop array=0 1 2 3 4 5 6 7 8 9 10
0
1
2
3
4
5
6
7
8
9
10
after for loop array=10 1 2 3 4 5 6 7 8 9 10

Thanks in advance I am thinking its something stupid and i'm over looking it.

SP
# 2  
Old 01-31-2012
Quote:
Originally Posted by snptuning

Code:
#!/bin/bash
vm=( 0 1 2 3 4 5 6 7 8 9 10 )

echo before for loop array=${vm[@]}
for vm in ${vm[@]}
do
    echo vm
done
echo after for loop array=${vm[@]}

Code:
for x in ${vm[@]}
do
    echo x
done

# 3  
Old 01-31-2012
You shouldn't use the same variable name in the for loop as the array

Try this instead:

Code:
#!/bin/bash
vm=( 0 1 2 3 4 5 6 7 8 9 10 )

echo before for loop array=${vm[@]}
for vm_element in ${vm[@]}
do
	echo $vm_element
done
echo after for loop array=${vm[@]}

# 4  
Old 01-31-2012
Yeah I just figured that out. I was mistaken and thought that a $vm would be differnt then ${vm} I knew it was stupid thanks for the help. Sorry to waste everyones time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk array next issue

Hi, Here's an example of what I my issue, a file list.txt, which looks like: a b c d e I have been trying to use the following code to simply print the list using awk and next: awk 'FNR==NR{a=$1;next}{print a}' list.txt this give no output, even though I would expect it to type out... (5 Replies)
Discussion started by: hexy
5 Replies

2. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

3. Shell Programming and Scripting

Array Issue In Bash

Hi, I have the following code which is giving error mentioned below. Please can you support on this. Also suggest how can we access all the items against single vasservicename in circlename array,i.e, vasservicename say MTSTV will be available to all circles which are mentioned in the array... (2 Replies)
Discussion started by: siramitsharma
2 Replies

4. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

5. Programming

Javascript issue: array.push

I have a code snippet here that is supposed to vary values of certain parameter values: <script type="text/javascript"> // dynamic array of which its ultimate size is unknown // an array of arrays, each consisting of one variation var variations = ; // parameters of how to create a... (0 Replies)
Discussion started by: figaro
0 Replies

6. Shell Programming and Scripting

Array usage issue with AWk

Hi friends, I m trying to write small script in awk alone. And I have tried below logic for one of my automation - taking the first column of the file in array and will read in for loop for each time when i grep the array value in the same file I should get complete output whichever is matching... (3 Replies)
Discussion started by: Shahul
3 Replies

7. Shell Programming and Scripting

Scripting array issue

Hi guys, I'm a scripting noob in need of some help :) I am creating a script that checks the filesystems and will alert based upon if the percent full is greater than the threshold set. The problem that I am having is that when I set the 'filesystem' variable, all of the output is treated as... (12 Replies)
Discussion started by: tank126
12 Replies

8. Solaris

V880 and T3 array issue

We're trying to install a third T3 array onto our V880. The other two T3's are connected to a qlogic fibre card. We can see this connection fine when we do a luxadm probe -p command. The full device paths are shown etc. ie: the first T3's logical name is c2t1d0 and the second one is c3t1d0. ... (3 Replies)
Discussion started by: mjl927
3 Replies

9. Shell Programming and Scripting

Dynamic Array Issue

Could one of you, please, provide some input regarding my problem below and it is as follows: I have 2 files that I need to make sure are identical before processing: First, I sort both files Second, I do a diff file1 file2 > File 3 This provides me with the difference. Now, I need to... (6 Replies)
Discussion started by: ddedic
6 Replies

10. Shell Programming and Scripting

Unix Korn Shell Array Issue (SunOS)

Hello, I'm currently messing around with arrays for the first time in scripting (Unix Korn Shell). All I'm trying to do right now before I make things complicated is read through and print out to screen whether the read file is or is not a directory. Here is my directory: ls -l total... (5 Replies)
Discussion started by: Janus
5 Replies
Login or Register to Ask a Question