![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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! |
|
||||
|
Quote:
Just use grep's count option -c instead. e.g. Code:
OrigNum[$aa]=$(grep -c ${Orig[$aa]} Ba3In2F12.prepos)
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
|
|
||||
|
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. |
|
||||
|
Quote:
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|