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.

 
#!/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

I don't think this is ksh syntax:

 while ($i -lt $tUbound)

Try:

 while [ $i -lt $tUbound ]

scottn is correct. And there are several other problems with your script:

print ${MtPtArray[1]}

If you use "print" this way you run the risk of a variable containing somthing which resembles to options to "print". For instance, store "-foobar-" to MtPtArray[1] and try the command above with that.

To avoid this risk use the "-" special option. It simply says that all the following will be text, not options at all:

print - ${MtPtArray[1]}

This line will pass my example text without any problem.

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:

print - ${MtPtArray[$i]}

Another point is this line:

((i=$i+1))

The "((" needs spaces around to work, like the "[" and "[[". The reason for this is that these were originally commands (if you search in /usr/bin you will find an executable named "[" still, which is identical to "/usr/bin/test"). Hence the line should be:

(( i=$i+1 ))

or, even shorter:

(( 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":

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:

#!/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

You will notice that i changed the "echo"s to "print"s. Is "print" better? I think so, but for most purposes probably not. The simple reason is: whatever you do, be consistent with yourself. If you use "print", then use it throughout the script, if you use "echo" then stick to that. Don't change in the middle of the script for no good reason.

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!!!!!

These changes to the script made it work:

#!/usr/bin/ksh
set -A MtPtArray /u03 /u06
tUbound=${#MtPtArray[*]}
echo $tUbound
i=0
while ((i<tUbound)); do
  print ${MtPtArray}
  ((i=$i+1))
  echo $i
done

Alternatively you could use a for loop:

#!/usr/bin/ksh
MtPtArray=(/u03 /u06)
for (( i=0; i<${#MtPtArray[*]}; i++ )); do
  print ${MtPtArray}
done

You could also do this to enumerate the array:

#!/usr/bin/ksh
MtPtArray=(/u03 /u06)
for i in "${MtPtArray[@]}"; do
  echo $i
done