Array in loop is acting up


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array in loop is acting up
# 1  
Old 08-07-2008
Array in loop is acting up

Hello!

I have a question about loops and arrays. I'm trying to go through this:

for aa in 01 02 03
OrigNum[$aa]=$(grep ${Orig[$aa]} Ba3In2F12.prepos | wc -l)
OrigNum[$aa]=$((${OrigNum[$aa]} - 1))
echo ${OrigNum[$aa]}

etc

It gets stuck on the second line. The error reads:

./asdf: line 30: syntax error near unexpected token `OrigNum[$aa]=$(grep ${Orig[$aa]} Ba3In2F12.prepos | wc -l)'
./asdf: line 30: ` OrigNum[$aa]=$(grep ${Orig[$aa]} Ba3In2F12.prepos | wc -l)'


I take it all out and do this:

aa=01
OrigNum[$aa]=$(grep ${Orig[$aa]} Ba3In2F12.prepos | wc -l)
OrigNum[$aa]=$((${OrigNum[$aa]} - 1))
echo ${OrigNum[$aa]}

And it works! The other lines are the exact same. Is there something special I must do in a for loop? Thanks in advance!
# 2  
Old 08-07-2008
Quote:
OrigNum[$aa]=$(grep ${Orig[$aa]} Ba3In2F12.prepos | wc -l)
First the pipe to wc is redundant.
Just use grep's count option -c instead.
e.g.
Code:
OrigNum[$aa]=$(grep -c ${Orig[$aa]} Ba3In2F12.prepos)

The syntax error is probably cause because your loop variable $aa
which acts as an array index has a leading zero.
Also remember that array indeces start counting from 0.
Try something like this instead (works for bash)
Code:
for ((i=0; i<${#Orig[*]}; i++)); do
     OrigNum[$i]=$(grep -c ${Orig[$i]} Ba3In2F12.prepos)
done

# 3  
Old 08-07-2008
Or your for statement isn't properly:

Code:
for aa in 1 2 3
do
  OrigNum[$aa]=$(grep -c ${Orig[$aa]} Ba3In2F12.prepos)
  OrigNum[$aa]=$((${OrigNum[$aa]} - 1))
  echo ${OrigNum[$aa]}
done

Regards
# 4  
Old 08-07-2008
Thanks! I'll go try these suggestions out. For Frank, yeah, I realize, but there's a bunch of code below that I didn't want to include. Thanks though!
# 5  
Old 08-07-2008
Thanks guys, you fixed the problem!

A new one though, how do you compare arrays? The whole thing reads:

for aa in 01 02 03
do
OrigNum[$aa]=$(grep -c ${Orig[$aa]} Ba3In2F12.prepos)
OrigNum[$aa]=$((${OrigNum[$aa]} - 1))
for bb in 01 02 03
do
for cc in 01 02 03
do
echo "${Orig[$bb]}" "${OrigNum[$bb]}"
echo "${Element[$cc]}" "${Numel[$cc]}"

if [ "${OrigNum[$bb]}"=="${Numel[$cc]}" ]; then
echo "SUCCESS!"
sed s/${Orig[$bb]}/${Element[$cc]}/ <Ba3In2F12.prepos>Ba3In2F12.preposterous
mv Ba3In2F12.preposterous Ba3In2F12.prepos
fi
done
done
done

I have an array called Numel[01-03], but when it compares, it always gives a success!!! I don't get it. Thanks! And sorry if these questions are elementary, I'm very new.
# 6  
Old 08-07-2008
Quote:
if [ "${OrigNum[$bb]}"=="${Numel[$cc]}" ]; then
String equality is tested with a single equals sign. (see man test)
But I guess since your arrays seem to hold counts as values
that you wish to test for arithmetic equality, so try round double parens instead
e.g.
Code:
 if (( ${OrigNum[$bb]} == ${Numel[$cc]} )); then

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash for loop array

Hi there, A bit new to bash and am having an issue with a for loop. I look for filenames in a specified directory and pull the date string from each meeting a certain criteria, and then would like to make a directory for each date found, like this: search 20180101.gz 20180102.gz 20180103.gz... (5 Replies)
Discussion started by: mwheeler12
5 Replies

2. 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

3. Shell Programming and Scripting

Loop over array in batches

given an array of filenames files=(*) how can i loop over these in batches of four, or even better, work always with four files simultaneously. i want do something with these files, always four of them simultaneously, and if one is ready the next should start. one idea, but definitely not... (2 Replies)
Discussion started by: dietmar13
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 with do while and if loop

Hi All, I am trying to run a do while for an array. And in the do while, I'm trying to get a user response. Depending on the the answer, I go ahead and do something or I move on to next element in the array. So far I can read the array, but I can't get the if statement to work. Any suggestions... (5 Replies)
Discussion started by: nitin
5 Replies

6. Programming

linux c, loop through 2d array?

I have a 2 d array that I need to loop through and check for a specific value in each variable... Im a little confiused on how to do this... #include stdio.h #include string.h main () { char arrary; memset(array,' ',sizeof(array)); for ????? { if ( array == ' ' ) { do... (2 Replies)
Discussion started by: trey85stang
2 Replies

7. Shell Programming and Scripting

Array and Loop Problem

I've got this problem, if I modify an array in the loop and print it, everything is fine as long as I stay in the loop. But, when I print it outside the loop, nothing happens... How can I solve this problem? Here I prepared a sample for you to see my problem; zgrw@Rain:~$ cat test asd 123... (4 Replies)
Discussion started by: zgrw
4 Replies

8. Shell Programming and Scripting

bash-function with array acting bizarre, bug?

Hello, basically what this script is supposed to do is showing a list of hosts that is given a number, that you will be able to choose from a list. A check is made to verify that the chosen number is within the array and this is where things go bad and I don't know why, bizarre. I've spent... (5 Replies)
Discussion started by: gand
5 Replies

9. UNIX for Dummies Questions & Answers

Reading from while loop into an array

Hi I have something like cat $HOME/all_dirs | while read ln_old_dirs do if then echo "$ln_all_old_dirs" fi done As you know that the variable ln_all_old_dirs is not accessable from outside the... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

10. Shell Programming and Scripting

Why for loop is acting weird

Hey i have a small script in which i check if a file with that pattern exists or not. If present then i go ahead with further processing. In the present situation i have only one file with that name and for loop is reading twice. Here is the script. And the output of debug mode. Please help.... (5 Replies)
Discussion started by: dsravan
5 Replies
Login or Register to Ask a Question