home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Tools / nShell™ 1.0.3 / nShell / User's Guide / 09 Scripting < prev    next >
Encoding:
Text File  |  1994-08-13  |  2.0 KB  |  79 lines  |  [TEXT/ttxt]

  1. 09 Scripting
  2. ============
  3.  
  4. nShell(tm) scripts are simple text files.  They may be created and edited in any text editor, including the TeachText and SimpleText programs provided with your Macintosh.
  5.  
  6. To create a script, type the series of commands you wish to execute into a file and save it in your current working directory.  (The shell searches your "command search path" for scripts just as it does commands.)
  7.  
  8. You can put comments in scripts using the "#" character.
  9.  
  10. Simple Scripts
  11. --------------
  12.  
  13. As an example, consider the "echo" command.  Echo repeats a string to the shell window, as in:
  14.  
  15. % echo "this is me"
  16. this is me
  17.  
  18. If I were to take these lines:
  19.  
  20. # This is a comment
  21. echo "I am a script"
  22. echo "and I am fine."
  23.  
  24. and save them in a text file called "yak", I could type yak and get the following:
  25.  
  26. % yak
  27. I am a script
  28. and I am fine.
  29.  
  30. Scripts can call other scripts just as they can call commands.  If I were to take the lines:
  31.  
  32. # This time we do it twice
  33. yak
  34. echo "tell me again?"
  35. yak
  36.  
  37. and save them in a file called "two", I could type two and get the following:
  38.  
  39. % two
  40. I am a script
  41. and I am fine.
  42. tell me again?
  43. I am a script
  44. and I am fine.
  45.  
  46. Script Parameters
  47. -----------------
  48.  
  49. You can pass parameters to scripts just as you can with commands.  When a script is executed its parameters are stored in shell variables.  The variables used are $#, $0, $1, $2...
  50.  
  51. $# contains the number of passed parameters
  52. $0 contains the name of the script
  53. $1 contains the first parameter
  54. $2 contains the second parameter
  55. ...
  56.  
  57. We can demonstrate this with the simple script called "kitty":
  58.  
  59. echo $#
  60. echo $0
  61. echo $1
  62. echo $2
  63.  
  64. Running kitty, we get:
  65.  
  66. % kitty is fine
  67. 3
  68. kitty
  69. is
  70. fine
  71.  
  72. Variables in Scripts
  73. --------------------
  74.  
  75. In addition to shell parameters, the "set" "unset" and "env" commands may be used to create and modify variables within a script.
  76.  
  77. It is important to note that while a script may modify any variable, all changes are temporary and are discarded when the script terminates.
  78.  
  79.