![]() |
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 |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| file not found error during loop | earls | Shell Programming and Scripting | 3 | 05-09-2009 05:25 PM |
| random array index returning values not contained | VRoemer | High Level Programming | 5 | 01-20-2009 05:39 PM |
| Usermod returning syntax error | syndex | Shell Programming and Scripting | 6 | 07-02-2007 12:38 PM |
| How do I prevent cron from returning errors on a file not found? | goodmis | Shell Programming and Scripting | 6 | 02-06-2007 10:14 AM |
| variable not found with loop? | douknownam | Shell Programming and Scripting | 2 | 09-27-2004 10:59 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Loop on array variable returning error: 0 not found
I'm guessing i have a syntax error. I'm not sure it get's past the the while condition. I get an error 0 not found. Simple loop not sure what I'm doing wrong.
Code:
#!/usr/bin/ksh
set -A MtPtArray /u03 /u06
tUbound=${#MtPtArray[*]}
echo $tUbound
i=0
while ($i -lt $tUbound)
do
print ${MtPtArray[1]}
((i=$i+1))
# i=`expr $i+1`
echo $i
done
|
|
||||
|
scottn is correct. And there are several other problems with your script:
Code:
print ${MtPtArray[1]}
To avoid this risk use the "-" special option. It simply says that all the following will be text, not options at all: Code:
print - ${MtPtArray[1]}
Next thing is: you have an array and want to cycle through its elements, yes? Then do not use a fixed subscript, but the variable you set up to do the cycling: Code:
print - ${MtPtArray[$i]}
Another point is this line: Code:
((i=$i+1)) Code:
(( i=$i+1 )) Code:
(( i += 1 )) And, by the way: do yourself a favour and give your variables meaningful names. When you have several (long) loops concurrently and something is counted/cycled through in each of them you will pretty fast lose track between all the i's, j's, k's and l's. Such naming style is coming from FORTRAN/77, which allowed only for 6 characters in variables names. But this was 1977 and we have 2009. My personal "standard" is to use the same name as the array being cycled through with the extension "Cnt" for "counter": Code:
set -A MtPtArray /u03 /u06 ... MtPtArrayCnt=0 while [ $MtPtArrayCnt -lt $tUbound ] ; do ...etc... Another thing is quoting: whenever you can, quote and quote scrupulously. Once you write (bigger) scripts you will soon find out that it is very very easy to write scripts that work somehow, but pretty difficult to write scripts that run under even the most adverse of circumstances - or at least exit with a reasonable explanation. (if a script CountWoffles.sh exits without doing its purpose it is better if it does so with "Error 17: too many woffles to count. Aborting." then with "line 367: bad subscript") One of the keys to writing robust scripts is quoting. This will take care for variable contents you might not have expected. Suppose that one of your mountpoints contains blanks in the name - unlikely, yes, but not forbidden. Would your script in its present way still run correctly? Here is the script the way i would write it: Code:
#!/usr/bin/ksh
set -A MtPtArray "/u03" "/u06"
typeset -i tUbound=${#MtPtArray[*]}
typeset -i MtPtArrayCnt=0
print - "$tUbound"
while [ $i -lt $tUbound ] ; do
print - "${MtPtArray[$MtPtArrayCnt]}"
print - "$MtPtArrayCnt"
(( MtPtArrayCnt += 1 ))
done
I also moved the increment of the cycling variable to the end of the loop. This makes sure that all the usages of it throughout the loop are done with the same value (as was the case in your original - i bet this wasn't intended). I hope this helps. bakunin |
|
||||
|
Scottn, Thank you very much for the help!
I had tried the [] but was switching things around because i couldn't figure out why things were breaking. After your confirmation on the brackets and the script sample below. I realized i had no spaces after the [] brackets or parrens. I write vb scripts so these spacing issues in unix are new to me - but i'm not likely to forget the lesson! Bakunin, Thank you for all the additional insghts and help. With the two responses i was able to get it to work an write it better!!!!! Last edited by KME; 1 Week Ago at 10:39 AM.. Reason: update |
|
||||
|
These changes to the script made it work:
Code:
#!/usr/bin/ksh
set -A MtPtArray /u03 /u06
tUbound=${#MtPtArray[*]}
echo $tUbound
i=0
while ((i<tUbound)); do
print ${MtPtArray[i]}
((i=$i+1))
echo $i
done
Code:
#!/usr/bin/ksh
MtPtArray=(/u03 /u06)
for (( i=0; i<${#MtPtArray[*]}; i++ )); do
print ${MtPtArray[i]}
done
Code:
#!/usr/bin/ksh
MtPtArray=(/u03 /u06)
for i in "${MtPtArray[@]}"; do
echo $i
done
Last edited by Scrutinizer; 1 Week Ago at 11:48 AM.. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|