Loop on array variable returning error: 0 not found


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Loop on array variable returning error: 0 not found
# 1  
Old 11-06-2009
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

# 2  
Old 11-06-2009
I don't think this is ksh syntax:

Code:

 while ($i -lt $tUbound)

Try:

Code:

 while [ $i -lt $tUbound ]

# 3  
Old 11-07-2009
scottn is correct. And there are several other problems with your script:

Code:
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:

Code:
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:

Code:
print - ${MtPtArray[$i]}


Another point is this line:

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

Code:
(( i=$i+1 ))

or, even shorter:

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

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
# 4  
Old 11-09-2009
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; 11-09-2009 at 10:39 AM.. Reason: update
# 5  
Old 11-09-2009
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

Alternatively you could use a for loop:
Code:
#!/usr/bin/ksh
MtPtArray=(/u03 /u06)
for (( i=0; i<${#MtPtArray[*]}; i++ )); do
  print ${MtPtArray[i]}
done

You could also do this to enumerate the array:
Code:
#!/usr/bin/ksh
MtPtArray=(/u03 /u06)
for i in "${MtPtArray[@]}"; do
  echo $i
done


Last edited by Scrutinizer; 11-09-2009 at 11:48 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh from a ksh returning not found message

Script name is test.ksh I know that that the ssh command is working properly, this can be verified by the value returned in respond variable. It is unique to the remote server _____________________________________________________ respond=$(ssh $remoteHost find... (3 Replies)
Discussion started by: Adagio
3 Replies

2. Shell Programming and Scripting

Variable not found error in while loop

I am unable to use the value of a variable. while ] do LstFldDataPart =`head -n "$LstFldDataPartCntr" "${FeedFileDir}/${FeedFileBadRecs}" | tail -1 | tr -d '\n'` echo $LstFldDataPart JndLstFldDataPart="${JndLstFldDataPart}${LstFldDataPart}" LstFldDataPartCntr=... (3 Replies)
Discussion started by: TomG
3 Replies

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

4. Shell Programming and Scripting

[Solved] FOR loop / IF statement returning error

The code at the bottom is a simplified example of what we have. If I use the following: && echo "echo failed" $? returns 1 When I use if ; then echo "echo failed" ; fi $? returns 0 Does anyone know what's wrong with this? Using AIX 6.1 and KSH for NUM in 1 2 3 do ... (5 Replies)
Discussion started by: jfxdavies
5 Replies

5. UNIX for Dummies Questions & Answers

Variable not found error

Hi, I get a "FILEPATH: not found" error on the 3rd line and the line where it is within the case. Any idea as to why I'm getting this error time and time again? Oh and FILEPATH will store the directory of the file. I appreciate any help! IAM=`basename $0` RC=0 FILEPATH = ""... (1 Reply)
Discussion started by: MIA651
1 Replies

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

7. Shell Programming and Scripting

while loop error. (command not found)

can any1 please tell me what is problem with following code: i=1; cat test| while read CMD; do Var$i=$CMD; or Var$i=$(echo $CMD) ; let i++ doneI keep getting error : line 4: Var1=sometext: command not found (2 Replies)
Discussion started by: kashif.live
2 Replies

8. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

9. Shell Programming and Scripting

file not found error during loop

Hi, I have a strange problem and I'm sure its because I'm doing something stupid. When I loop through all of the files in a directory they cannot be found. Any help would be greatly appreciated. Thanks, -E $ touch a.txt $ touch b.txt $ ls a.txt b.txt $ for f in `ls` ; do... (3 Replies)
Discussion started by: earls
3 Replies

10. Shell Programming and Scripting

variable not found with loop?

I cannot seem to pass the value of the variable assigned in the "else" statment below to an "if" statement outside of the loop. When I debug using "set -x", I find that the value of "$A" is empty. Is there something I'm missing here? Code is below: do if ] then echo "Good Match"... (2 Replies)
Discussion started by: douknownam
2 Replies
Login or Register to Ask a Question