home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / nShell / User's Guide / 10 Flow Of Control < prev    next >
Encoding:
Text File  |  1994-06-29  |  2.9 KB  |  68 lines  |  [TEXT/ttxt]

  1. 10 Flow Of Control
  2. ==================
  3.  
  4. Flow of Control statements, available in nShell-Pro(tm) control when and how other statements are executed.  That is, they control the flow of execution through a command line or through a script file.
  5.  
  6. When flow of control structures are typed into the command line, they must start and end within the same line:
  7.  
  8. if yesno "ready?" then hello endif
  9.  
  10. When flow of control structures are used in scripts, they may be broken into several lines:
  11.  
  12. if yesno "ready?"
  13. then
  14.     hello
  15. endif
  16.  
  17. if then else
  18. ------------
  19.  
  20. This structure has the following form:
  21.  
  22. if <commands1> then <commands2> [else <commands3>] endif
  23.  
  24. If the result of the last command of the set <commands1> completes without error (result = zero), then the <commands2> clause will be executed.
  25.  
  26. If the result of the last command of the set <commands1> produces an error (result <> zero), and an else-clause is present, that clause will execute.
  27.  
  28. while do done
  29. -------------
  30.  
  31. This structure has the following form:
  32.  
  33. while <commands1> do <commands2> done
  34.  
  35. In this structure, the <commands1> clause will always execute.  If the result of the last command of the set completes without error (result = zero), then the <commands2> clause will be executed.  Control will then return to the <commands1> clause, and the process will continue until the last command of this set produces an error (result <> 0).
  36.  
  37. until do done
  38. -------------
  39.  
  40. This structure has the following form:
  41.  
  42. until <commands1> do <commands2> done
  43.  
  44. In this structure, the <commands1> clause will always execute.  If the result of the last command of the set produces an error (result <> zero), then the <commands2> clause will be executed.  Control will then return to the <commands1> clause, and the process will continue until the last command of this set completes without error (result = 0).
  45.  
  46. Testing Conditions
  47. ------------------
  48.  
  49. A number of commands are included in this release to test conditions.  The commands below produce a successful result (zero) when the listed condition is true:
  50.  
  51.     .eq.    Equal
  52.     .ne.    Not Equal
  53.     .gt.    Greater Than
  54.     .lt.    Less Than
  55.     .ge.    Greater Than or Equal To
  56.     .le.    Less Than or Equal To
  57.  
  58. These operators are used to compare two parameters.  If both parameters are legal integers, an integer comparison is performed.  If one or both parameters are not integers, a string comparison is performed.
  59.  
  60. These comparison commands may be used to test the return value from a previous command.  It is important to note, however, that the comparison itself produces a return value and will overwrite "$?".  For this reason, the return code should be saved into a temporary variable before a series of comparisons are performed:
  61.  
  62.     command_x
  63.     set temp $?    # save the result
  64.     if .lt. $temp 0 then echo "A serious error has occurred." endif
  65.     if .eq. $temp 0 then echo "Everything is ok" endif
  66.     if .gt. $temp 0 then echo "command_x returned a value of $temp" endif
  67.  
  68.