The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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
How to use array values after the loop. Devesh5683 Shell Programming and Scripting 1 05-13-2008 07:38 PM
Passwd Changing Acting Strange rockusa SUN Solaris 2 12-10-2007 08:41 PM
Array Declaration and For Loop 33junaid Shell Programming and Scripting 4 09-15-2007 04:16 PM
Why for loop is acting weird dsravan Shell Programming and Scripting 5 09-13-2006 12:07 PM
declare, assign variables using array, counter, loop egkumpe Shell Programming and Scripting 3 08-09-2004 10:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-07-2008
RisingSun RisingSun is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
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 (permalink)  
Old 08-07-2008
buffoonix buffoonix is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 145
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 (permalink)  
Old 08-07-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
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 (permalink)  
Old 08-07-2008
RisingSun RisingSun is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
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 (permalink)  
Old 08-07-2008
RisingSun RisingSun is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
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 (permalink)  
Old 08-07-2008
buffoonix buffoonix is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 145
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0