Example-Script.html 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <html lang="en">
  2. <head>
  3. <title>Example Script - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="User-Manual.html#User-Manual" title="User Manual">
  9. <link rel="prev" href="Security.html#Security" title="Security">
  10. <link rel="next" href="Files.html#Files" title="Files">
  11. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  12. <meta http-equiv="Content-Style-Type" content="text/css">
  13. <style type="text/css"><!--
  14. pre.display { font-family:inherit }
  15. pre.format { font-family:inherit }
  16. pre.smalldisplay { font-family:inherit; font-size:smaller }
  17. pre.smallformat { font-family:inherit; font-size:smaller }
  18. pre.smallexample { font-size:smaller }
  19. pre.smalllisp { font-size:smaller }
  20. span.sc { font-variant:small-caps }
  21. span.roman { font-family:serif; font-weight:normal; }
  22. span.sansserif { font-family:sans-serif; font-weight:normal; }
  23. --></style>
  24. </head>
  25. <body>
  26. <div class="node">
  27. <a name="Example-Script"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Files.html#Files">Files</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="Security.html#Security">Security</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="User-Manual.html#User-Manual">User Manual</a>
  32. <hr>
  33. </div>
  34. <h3 class="section">1.8 Example Script</h3>
  35. <p><a name="index-script-120"></a><a name="index-shell-script-121"></a>It is recommended that the application developer (or the compiler)
  36. package virtual machine code applications as shell scripts with the
  37. <code>avram</code> command line embedded in them. This style relieves the user
  38. of the need to remember the appropriate virtual machine options for
  39. invoking the application, which are always the same for a given
  40. application, or even to be aware of the virtual machine at all.
  41. <p><a name="index-g_t_0040code_007bcat_007d-122"></a><a name="index-g_t_0040code_007bdefault_002dto_002dstdin_007d-command-line-option-123"></a>Here is a script that performs a similar operation to the standard
  42. <a name="index-Unix-124"></a>Unix <samp><span class="command">cat</span></samp> utility.
  43. <pre class="example"> #!/bin/sh
  44. #\
  45. exec avram --force-text-input --default-to-stdin "$0" "$@"
  46. sKYQNTP\
  47. </pre>
  48. <p class="noindent">That is, it copies the contents of a file whose
  49. name is given on the command line to standard output, or copies
  50. standard input to standard output if no file name is given. This
  51. script can be marked executable
  52. <a name="index-executable-files-125"></a>(with <samp><span class="command">chmod</span></samp>) and run by any user
  53. <a name="index-g_t_0040code_007bchmod_007d-126"></a><a name="index-paths-127"></a>with the directory of the <code>avram</code> executable in his or her
  54. <code>PATH</code> environment variable, even if <code>avram</code> had to be
  55. installed in a non-standard directory such as
  56. <a name="index-non_002dstandard-installation-128"></a><samp><span class="file">~/bin</span></samp>.
  57. <p>The idea for this script is blatantly lifted from the <samp><span class="command">wish</span></samp>
  58. <a name="index-g_t_0040code_007bwish_007d-129"></a>manpage. The first line of the script invokes a shell to process
  59. what follows. The shell treats the second line as a comment and
  60. ignores it. Based on the third line, the shell invokes <code>avram</code>
  61. with the indicated options, the script itself as the next argument, and
  62. whatever command line parameters were initially supplied by the user
  63. as the remaining arguments. The rest of the script after
  64. that line is never processed by the shell.
  65. <p>When <code>avram</code> attempts to load the shell script as a virtual
  66. machine code file, which happens as a result of it being executed by
  67. the shell, it treats the first line as a comment and ignores it. It
  68. also treats the second line as a comment, but takes heed of the
  69. trailing backslash, which is interpreted as a comment continuation
  70. character. It therefore also treats the third line as a comment and
  71. ignores it. Starting with the fourth line, it reads the virtual code,
  72. which is in a binary data format encoded with printable characters,
  73. and evaluates it.
  74. </body></html>