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 > UNIX for Dummies Questions & Answers
.
google unix.com



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

Reply
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 2 Weeks Ago
KME KME is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 7
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 (permalink)  
Old 2 Weeks Ago
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,033
I don't think this is ksh syntax:

Code:

 while ($i -lt $tUbound)
Try:

Code:

 while [ $i -lt $tUbound ]
  #3 (permalink)  
Old 2 Weeks Ago
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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 (permalink)  
Old 1 Week Ago
KME KME is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 7
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
  #5 (permalink)  
Old 1 Week Ago
Scrutinizer Scrutinizer is online now
Registered User
  
 

Join Date: Nov 2008
Posts: 584
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; 1 Week Ago at 11:48 AM..
Sponsored Links
Reply

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 03:24 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