home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / answers / unix-faq / shell / rc < prev    next >
Encoding:
Internet Message Format  |  1993-12-07  |  43.1 KB

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!gatech!howland.reston.ans.net!vixen.cso.uiuc.edu!sdd.hp.com!saimiri.primate.wisc.edu!news.doit.wisc.edu!oldp.astro.wisc.edu!alan
  2. From: alan@oldp.astro.wisc.edu (Alan Watson)
  3. Newsgroups: comp.unix.shell,comp.answers,news.answers
  4. Subject: rc shell Frequently Asked Questions
  5. Followup-To: poster
  6. Date: 6 Dec 1993 21:32:06 GMT
  7. Organization: Department of Astronomy, University of Wisconsin -- Madison
  8. Lines: 1086
  9. Approved: news-answers-request@MIT.Edu
  10. Expires: 04 Jan 1994
  11. Message-ID: <2e08cm$l44@news.doit.wisc.edu>
  12. NNTP-Posting-Host: oldp.astro.wisc.edu
  13. Summary: Answers to Frequently Asked Questions about rc -- a clean,
  14.          portable, and freely-available shell for UNIX systems.
  15. Originator: alan@oldp.astro.wisc.edu
  16. Xref: senator-bedfellow.mit.edu comp.unix.shell:13925 comp.answers:2917 news.answers:15555
  17.  
  18. Archive-name: unix-faq/shell/rc
  19. Comp-unix-shell-archive-name: rc-FAQ
  20. Version: 2.13
  21. Last-modified: 06 Dec 1993
  22.  
  23. This document attempts to answer some Frequently Asked Questions about
  24. rc, a clean, portable, and freely-available shell for UNIX systems (see
  25. 1.1 for a very brief overview of rc).  It in no way replaces either the
  26. formal documentation for rc (see 1.2) or the mailing list archives (see
  27. 2.2).
  28.  
  29. This document draws heavily on the collective wisdom of the rc mailing
  30. list; the quality of the correspondence is such that often all that was
  31. required was a summary of one or two messages.  The rc mailing list
  32. archive is recommend reading for newcomers to rc (see 2.2).
  33. Contributors are acknowledged at the end of this document.
  34.  
  35. Corrections, comments, and suggestions concerning this document are
  36. welcome (see 3.2).
  37.  
  38. NOTE: Neither the editor nor any of the contributors to this document
  39. are responsible for any consequences resulting from the advice in this
  40. document.
  41.  
  42. Table of Contents
  43.  
  44. 1.      Implementations of rc
  45. 1.1     What is rc?
  46. 1.2     Where is rc documented?
  47. 1.3     How do I obtain rc?
  48. 1.4     Are there any patches to rc?
  49. 1.5     Can I compile rc on a ... machine with ... operating system and
  50.         ... compiler?
  51. 1.6     Should rc be dynamically or statically linked?
  52. 1.7     To whom do I report bugs?
  53. 1.8     Does anyone have an implementation of /dev/fd for my UNIX?
  54.  
  55. 2.      The rc mailing list
  56. 2.1     How do I subscribe to the rc mailing list?
  57. 2.2     How do I obtain the rc mailing list archive?
  58.  
  59. 3.      The rc FAQ list
  60. 3.1     How do I obtain the current version of the rc FAQ list?
  61. 3.2     To whom do I make suggestions about the rc FAQ list?
  62.  
  63. 4.      Does rc have ... ?
  64. 4.1     Does rc have job control?
  65. 4.2     Does rc have command-line editing?
  66. 4.3     Does rc have a history mechanism?
  67. 4.4     Does rc have file name completion?
  68. 4.5     Does rc have home directory expansion?
  69. 4.6     Does rc check for new mail?
  70. 4.7     Does rc have hierarchical lists?
  71. 4.8     Does rc have cartesian products?
  72. 4.9     Does rc cache the location of executables in the path?
  73. 4.10    Does rc have C-style \ escapes?
  74. 4.11    Does rc have ... built in?
  75. 4.12    Does rc have echo built in?
  76. 4.13    Does rc have test built in?
  77. 4.14    Does rc have true, false, and `:' built in?
  78.  
  79. 5.      Using rc as a login shell
  80. 5.1     How do I make rc my login shell?
  81. 5.2     How do I use rc in a heterogeneous cluster?
  82. 5.3     What problems occur with rc on Apollos?
  83. 5.4     What problems occur with rc on SYSV-derived systems?
  84. 5.5     What problems occur with rc on NeXTs?
  85. 5.6     How do I keep down the size of my .rcrc file?
  86. 5.7     How do I speed up the execution of my .rcrc file?
  87.  
  88. 6.      Interactions between rc and the rest of UNIX
  89. 6.1     How do I get each new interactive rc to do ...?
  90. 6.2     How do I get rc to do ... in each new xterm?
  91. 6.3     How do I get rc to read .rcrc when I use rsh?
  92. 6.4     How do I get at or batch to run an rc script?
  93.  
  94. 7.      Programming in rc
  95. 7.1     What makes rc such a good scripting language?
  96. 7.2     What are the trade-offs between functions and scripts?
  97. 7.3     What can I do if I have deficient sh, awk, test, ... ?
  98. 7.4     Where do I obtain the GNU tools and other software?
  99. 7.5     How do I perform arithmetic in rc?
  100. 7.6     How do I find the first occurrence of an element in a list?
  101. 7.7     How do I use ranges in array subscripts in rc?
  102. 7.8     How do I perform the equivalent of read in rc?
  103.  
  104. 1.      Implementations of rc
  105.  
  106. 1.1     What is rc?
  107.  
  108.         rc is a shell designed by Tom Duff of Bell Labs to replace the
  109.         venerable Bourne sh in Plan 9.  (Plan 9 is an experimental
  110.         operating and networking system developed by Bell Labs.)  The
  111.         design of rc is extremely clean, in that precisely those
  112.         features of Bourne's sh that have caused problems over the years
  113.         (the slightly awkward flow control syntax, the Byzantine quoting
  114.         rules, the failure to export all variables into the environment)
  115.         were reworked, and the temptation to add superfluous features
  116.         was strongly resisted.
  117.  
  118.         Byron Rakitzis has written a version of rc that is portable
  119.         between many version on UNIX and is largely compatible with
  120.         Duff's design.  Rakitzis' implementation has been successfully
  121.         used on UNIX platforms ranging from PCs running Linux to Cray
  122.         Y-MPs running Unicos.
  123.  
  124.         Most of this document deals with issues that have arisen from
  125.         use of Rakitzis' rc, and most of the example code will follow
  126.         his minor modifications to rc.  Henceforth, a plain `rc' refers
  127.         to Rakitzis' rather than Duff's rc.
  128.  
  129. 1.2     Where is rc documented?
  130.  
  131.         rc is documented in its man page, distributed with the source
  132.         (see 1.3).
  133.  
  134.         The Plan 9 manual pages, including those on Duff's rc, are
  135.         available as research.att.com:/dist/plan9man/manual.ps.Z by
  136.         anonymous FTP.  The full manual is almost 600 pages long, but
  137.         the manual pages for Duff's rc are physical pages 142 to 147
  138.         (logical pages 116 to 121).
  139.  
  140.         An early version of Duff's rc is described in the paper `rc -- a
  141.         Shell for Plan 9 and UNIX Systems', written for a UKUUG meeting.
  142.         Duff has kindly allowed the paper to be distributed by anonymous
  143.         FTP as vaz.tamu.edu:/pub/rc/plan9.ps.  It provides an excellent
  144.         introduction and tutorial for Duff's rc, and describes the
  145.         motivations for the differences between Duff's rc and Bourne's
  146.         sh.
  147.  
  148.         When reading Duff's paper and the Plan 9 manual pages, bear in
  149.         mind that Rakitzis' implementation of rc differs in detail from
  150.         Duff's (these differences are documented in the rc man page).
  151.  
  152.         Another valuable source of information on rc is the rc mailing
  153.         list archive (see 2.2).
  154.  
  155. 1.3     How do I obtain rc?
  156.  
  157.         The sources are available by anonymous FTP in the directory
  158.         viz.tamu.edu:/pub/rc.  Be sure to get the most recent version.
  159.         At the time this document was last updated, the most recent
  160.         version of rc was v1.4 announced on 21 May 1992, avalailable as
  161.         viz.tamu.edu:/pub/rc/rc-1.4.tar.Z.
  162.  
  163.         rc is distributed on very liberal terms, although it has not
  164.         been placed in the public domain (see the file COPYRIGHT
  165.         distributed with the sources).
  166.  
  167.         Duff's rc can be obtained by acquiring either a Plan 9 or a v10
  168.         UNIX system from Bell Labs.  Plan 9 can be licensed by academic
  169.         institutions, which should contact:
  170.  
  171.             Neera Kuckreja
  172.             RM 2C557
  173.             ATT Bell Laboratories
  174.             Murray Hill
  175.             NJ 07974
  176.             USA
  177.             +1 908 582-3855
  178.             neera@research.att.com
  179.  
  180. 1.4     Are there any patches to rc?
  181.  
  182.         Although the current version of rc is remarkably mature, a small
  183.         number of bugs have been found since its release and patches
  184.         have been posted to the rc mailing list (see 2.2).  Other
  185.         patches posted to the mailing list include work-arounds for
  186.         broken compilers or libraries and assorted features.
  187.  
  188.         An up-to-date set of patches extracted from the mailing list
  189.         archive, together with a script to apply those pathes, is
  190.         available as oldp.astro.wisc.edu:/pub/rc/patches.tar.Z by
  191.         anonymous FTP.
  192.  
  193. 1.5     Can I compile rc on a ... machine with ... operating system and
  194.         ... compiler?
  195.  
  196.         rc is written in portable ANSI C, but it can be compiled by many
  197.         older C compilers after passing the sources through unproto (get
  198.         ftp.win.tue.nl:/pub/unix/unproto4.shar.Z by anonymous FTP).
  199.  
  200.         If you use gcc, use v1.39 or later.
  201.  
  202.         rc has been successfully compiled on systems with the following
  203.         combinations of architectures, operating systems, and compilers,
  204.         although this list is probably not exhaustive:
  205.  
  206.             DEC VAX        Ultrix v3.x            gcc v1
  207.             DEC MIPS       Ultrix v4.[23]         gcc v2
  208.             DEC MIPS       Ultrix v4.[23]         cc          [a]
  209.             DEC AXP        OSF/1 v1.2             cc          [a][b]
  210.             DEC AXP        OSF/1 v1.2             cc -std1    [b]
  211.  
  212.             Sun SPARC      Solaris v2             gcc v2      [b][c]
  213.             Sun SPARC                             acc         [b][c]
  214.             Sun SPARC      SunOS v4.1.[123]       cc          [a][b][c]
  215.             Sun SPARC      SunOS v4.1.[123]       gcc v2      [b][c]
  216.             Sun SPARC      BSD                                [b][c]
  217.             Sun 68K        SunOS v4.1.1           gcc v2      [b][c]
  218.             Sun 68K        SunOS v3.5             gcc v1      [b][c]
  219.  
  220.             SGI Iris       Irix v[45].x           gcc v2
  221.  
  222.             IBM RS/6000    AIX v3.1.5             cc
  223.             IBM RS/6000    AIX v3.2.[013]         cc
  224.  
  225.             HP PA-RISC     HP-UX v8.0             gcc v2      [d][e]
  226.             HP PA-RISC     HP-UX v9.0             gcc v2      [d]
  227.             HP PA-RISC     OSF/1 v1.0.2           cc
  228.  
  229.             i386/i486      SCO-ODT SVR3           gcc v2      [d][e]
  230.             i386/i486      SCO-ODT SVR3           cc          [d][e]
  231.             i386           Minix v1.5                         [d]
  232.             i386/i486      NetBSD                 gcc v[12]
  233.             i386/i486      Linux                  gcc v2      [d]
  234.             i386/i486      386BSD r0.0            gcc         [j]
  235.             i386/i486      SCO Xenix SVR2.3x                  [f]
  236.  
  237.             NeXT           NeXT OS v2.x           gcc v[12]   [g]
  238.             NeXT           NeXT OS v3.0           gcc v2      [g]
  239.  
  240.             Apollo 68K     DomainOS v10.4 (BSD)   cc          [h]
  241.             Apollo 10000   DomainOS v10.4 (BSD)   cc          [h]
  242.  
  243.             Sony MIPS      NEWS/OS                cc          [a]
  244.             Sony 68K       NEWS/OS v4.0.1         cc          [a]
  245.  
  246.             Convex C1      ConvexOS v10.1         cc
  247.  
  248.             Pyramid 9810   OSx 5.1a (BSD)         cc -Xa      [i]
  249.  
  250.             Cray Y-MP      Unicos 6.1.7           scc         [e][j]
  251.  
  252.             Sequent        Dynix v3.2.0 (BSD)     gcc v2
  253.  
  254.             Apple 68K      A/UX v3.0              cc
  255.  
  256.             Encore MIPS    Umax v4.3              gcc v2
  257.  
  258.             Acorn ARM      iX v1.21a              cc
  259.  
  260.             Motorola 88K   System V R32V3         gcc v2
  261.             Motorola 88K   System V R32V3         cc          [a][k]
  262.  
  263.         Notes:
  264.  
  265.             [a] Sources pre-processed with unproto.
  266.             [b] See 1.6.
  267.             [c] Sun's yacc has a memory leak -- use another
  268.                 implementation such as GNU bison (see 7.4).
  269.             [d] Requires intervention by hand to get mksignal to work,
  270.                 because of of the non-standard signal.h file.
  271.             [e] See 4.13.
  272.             [f] See 5.4.
  273.             [g] See 5.5.
  274.             [h] See 5.3
  275.             [i] This compiler has no stdarg.h, but vararg.h sufficed.
  276.             [j] Minor modifications required to prototypes.
  277.             [k] Greenhills cc, used without any optimization.
  278.  
  279. 1.6     Should rc be dynamically or statically linked?
  280.  
  281.         As far as one can generalize, statically linked executables fork
  282.         faster than dynamically linked executables.  This suggests that
  283.         shells, which fork a great deal, should probably be statically
  284.         linked (i.e., you should not link them with shared libraries).
  285.         rc makes so little use of libc functions that static linking
  286.         results in only a small increase in the size of the executable
  287.         on disc.
  288.  
  289.         Versions of UNIX which support shared libraries include SVR4,
  290.         SunOS, Solaris, OSF/1, and Irix.
  291.  
  292. 1.7     To whom do I report bugs?
  293.  
  294.         Byron Rakitzis (byron@netapp.com) or the rc mailing list
  295.         (rc@hawkwind.utcs.toronto.edu).  Please check the documentation
  296.         before reporting bugs, and check the mailing list archive in
  297.         case the bug is already known.
  298.  
  299. 1.8     Does anyone have an implementation of /dev/fd for my UNIX?
  300.  
  301.         /dev/fd pseudo-devices allow a more reliable implementation of
  302.         non-linear pipelines (i.e., <{...}  and >{...}) than named
  303.         pipes, but they are not present in all versions of UNIX.
  304.  
  305.         An implementation of Ultrix v4.2 and later was posted to the
  306.         mailing list in March 1993 (see 2.2).
  307.  
  308.         An implementation for SunOS v4.1.1 is also available (although
  309.         it may well work with v4.1.2 and v4.1.3) by anonymous FTP as
  310.         gemini.tuc.noao.edu:/pub/devfd.shar.
  311.  
  312.         An implementation for NeXT OS 2.1 is available as by anonymous
  313.         FTP as ftp.sys.utoronto.edu:/pub/rc/devfd-next.shar.gz.
  314.  
  315.         You will, of course, need to rebuilt your kernel to install
  316.         these pseudo-devices.
  317.  
  318. 2.      The rc mailing list
  319.  
  320. 2.1     How do I subscribe to the rc mailing list?
  321.  
  322.         Send mail to
  323.  
  324.             rc-request@hawkwind.utcs.toronto.edu.
  325.  
  326.         Chris Siebenmann maintains the mailing list, and can be reached
  327.         at the above address in the event of problems with the mailing
  328.         list or to unsubscribe.
  329.  
  330.         The rc mailing list represents hundreds of user-years of
  331.         experience with rc.  Clearly, it is a courtesy to the list and
  332.         to your own advantage to check the mailing list archive before
  333.         posting to the list, to benefit from the experience and mistakes
  334.         of those before you, and to prevent topics being continually
  335.         rehashed.
  336.  
  337. 2.2     How do I obtain the rc mailing list archive?
  338.  
  339.         The archive can be obtained by anonymous FTP.  At the moment, it
  340.         is split chronologically between the original and current
  341.         maintainers of the mailing list.
  342.  
  343.         Period              File
  344.         Jun 91 to Dec 91    viz.tamu.edu:/pub/rc/rc-list.1991.Z
  345.         Jan 92 to May 92    viz.tamu.edu:/pub/rc/rc-list
  346.         May 92 to present   ftp.sys.utoronto.ca:/pub/rc/rc-list
  347.  
  348. 3.      The rc FAQ list
  349.  
  350. 3.1     How do I obtain the current version of the rc FAQ list?
  351.  
  352.         The current version of the rc FAQ list is available by anonymous
  353.         FTP as oldp.astro.wisc.edu:/pub/rc/rc-FAQ.  This FAQ is also
  354.         archived automatically and is available by anonymous FTP as
  355.         rtfm.mit.edu:/pub/usenet/comp.unix.shell/rc-FAQ.
  356.  
  357. 3.2     To whom do I make suggestions about the rc FAQ list?
  358.  
  359.         To its editor, Alan Watson (alan@oldp.astro.wisc.edu), or the rc
  360.         mailing list if you think your suggestion warrants a wider
  361.         audience.
  362.  
  363. 4.      Does rc have ... ?
  364.  
  365. 4.1     Does rc have job control?
  366.  
  367.         No.  This has been discussed at length in the rc mailing list.
  368.  
  369.         Now that personal workstations with window systems are common
  370.         place, the case for job control is less compelling.  However, if
  371.         you have to use rc on a traditional terminal, you may wish to
  372.         obtain the screens program from a GNU archive (see 7.4), the sm
  373.         program that was posted to alt.sources in mid-1992 (see 7.4), or
  374.         investigate shell escape mechanisms in vi and shell mode in
  375.         emacs.
  376.  
  377. 4.2     Does rc have command-line editing?
  378.  
  379.         If you wish.  Hooks are provided for the readline or editline
  380.         libraries which provide command-line editing.  The default is no
  381.         command-line editing.
  382.  
  383.         The readline library can be obtained from a GNU archive (see
  384.         7.4).
  385.  
  386.         The editline library is similar to the readline library, but is
  387.         a fraction of the size and offers fewer features.  The sources
  388.         are available as ftp.sys.utoronto.edu:/pub/rc/editline.shar by
  389.         anonymous FTP).
  390.  
  391.         Alternatively, you can use fep, ile, atty, or Rk to implement
  392.         editing on a per-terminal basis.  These have been discussed in
  393.         on the mailing list at one time or another, and modifications to
  394.         ile have been posted there.
  395.  
  396. 4.3     Does rc have a history mechanism?
  397.  
  398.         Yes.  While, rc does not have a built-in csh-like history
  399.         mechanism, rather it is able to write interactive commands to a
  400.         file (see the history special variable in the man page),
  401.         allowing a history mechanism similar to the v8 `=' command to be
  402.         implemented as an external program (one such implementation is
  403.         supplied with the source of rc).
  404.  
  405.         In Plan 9, `:' was the history program (the name was probably
  406.         changed from the `=' used in v8 because `=' is a special
  407.         character in rc, and needs quoting if it refers to a command).
  408.         However, this form of history was apparently used so little that
  409.         the ability to write commands to a file was removed from Duff's
  410.         rc.  The history program distributed with the rc source is
  411.         invoked with `-'.
  412.  
  413.         Both editline and readline have command recall.
  414.  
  415. 4.4     Does rc have file name completion?
  416.  
  417.         If you wish.  Plain rc does not have file name completion (as it
  418.         obtains its command input with read), but the readline library
  419.         does.
  420.  
  421. 4.5     Does rc have home directory expansion?
  422.  
  423.         No.  This has been hotly debated on the rc mailing list, and a
  424.         patch to implement this was posted to the rc mailing list in
  425.         November 1992 (see 2.2 and 1.4).
  426.  
  427. 4.6     Does rc check for new mail?
  428.  
  429.         No.  However, you can make it do so with the following prompt
  430.         function:
  431.  
  432.             fn prompt { mail -e && echo You have mail. }
  433.  
  434.         Alternatively, consider using biff or xbiff.
  435.  
  436. 4.7     Does rc have hierarchical lists?
  437.  
  438.         No.  Each element of an rc list must be a string, and cannot be
  439.         another list.  Whenever a list appears inside another list, it
  440.         is expanded so that the outer list contains its elements.
  441.         Similarly, whenever two lists are adjacent, they are
  442.         concatenated to form a single list (with one exception).
  443.  
  444.         In rc terms, the arguments to a command form a list, even though
  445.         they may not have explicit `(' and `)' delimiters around them.
  446.         Hierarchical lists do not correspond well to the `char **argv'
  447.         model for arguments to UNIX commands, although simple lists do.
  448.  
  449.         The single exception to these rules are the arguments to the `~'
  450.         built-in command, which rc interprets as two lists -- a subject
  451.         list and a pattern list.  Normal expansion and concatenation is
  452.         performed on the two lists separately.  A consequence of this is
  453.         that the following command:
  454.  
  455.                 if ( ~ $1 () '' ) ...
  456.  
  457.         does not check if $1 is either the null list or the null string,
  458.         as normal list concatenation occurs on the pattern list leading
  459.         to a pattern list with one element -- the null string.
  460.  
  461. 4.8     Does rc have cartesian products?
  462.  
  463.         No.  If you really need them, they can be obtained in the
  464.         following manner:
  465.  
  466.             ; x = (a b c)
  467.             ; y = (1 2 3)
  468.             ; product = ()
  469.             ; for ( elem in $x ) product = ( $product $elem^$y )
  470.             ; echo $product
  471.             a1 a2 a3 b1 b2 b3 c1 c2 c3
  472.  
  473.         rc's concatenation operator works in a distributive manner if
  474.         one of the lists has only a single element and in a pairwise
  475.         manner if both lists have the same number of elements:
  476.  
  477.             ; echo a^( x y z )
  478.             ax ay az
  479.             ; echo ( a b c )^( x y z )
  480.             ax by cz
  481.  
  482. 4.9     Does rc cache the location of executables in the path?
  483.  
  484.         No.  This has been discussed on the mailing list.  As far as one
  485.         can generalize, searching the path is not a performance
  486.         bottleneck on most modern UNIX systems.
  487.  
  488. 4.10    Does rc have C-style \ escapes?
  489.  
  490.         No.  One of the most attractive features of rc is its single
  491.         quoting rule.
  492.  
  493.         If you need to embed control characters in your strings,
  494.         investigate tr and printf (see 7.3 if your system lacks the
  495.         latter).
  496.  
  497. 4.11    Does rc have ... built in?
  498.  
  499.         It is not by accident that rc has fewer built-in commands than
  500.         most shells other than the original Bourne shell.  The
  501.         underlying philosophy, eloquently summarized by Boyd Roberts, is
  502.         that a shell is there to run other programs, not to have other
  503.         programs built into it.  By having a well-defined role, rc has
  504.         remained small and simple, properties that lead to better
  505.         performance, fewer bugs, and a more thorough understanding of
  506.         the shell by its users.
  507.  
  508.         Most of the rc built-in commands perform functions that are
  509.         essential for a shell and cannot be performed by external
  510.         programs (e.g., cd, eval, limit, umask, wait).
  511.  
  512.         rc does include hooks for users to add new built-in commands,
  513.         and these have been used to add test, access, kill, read, and
  514.         even sync and haltsys commands (see the mailing list and the
  515.         examples file for more details).  Such additions are unsupported
  516.         and potentially unportable.
  517.  
  518. 4.12    Does rc have echo built in?
  519.  
  520.         If you wish.  While most of rc's built-in commands could not
  521.         easily be replaced by external commands, echo is the one
  522.         exception.  The justification is that echo is used so much that
  523.         providing it as a built-in command results in a significant gain
  524.         in performance, although this opinion is by no means universally
  525.         held.  By default, echo is built in.
  526.  
  527.         It has also been noted that rc's echo is more portable that
  528.         /bin/echo, as the v7 and SYSV implementations of echo differ in
  529.         the method that must be used to suppress the final newline.
  530.         (However, rc's echo differs from both, as it uses `--' to
  531.         indicate that all other arguments are to be echoed literally.)
  532.  
  533.         People who dislike the notion of a built-in echo command have
  534.         the option of excluding it from the compilation.  Alternatively,
  535.         the built-in echo can be subverted with the following function,
  536.         although this will not defeat an explicit call to `builtin
  537.         echo'.
  538.  
  539.             fn echo { /bin/echo $* }
  540.  
  541.         See also 7.3.
  542.  
  543. 4.13    Does rc have test built in?
  544.  
  545.         No.  You can use the built-in `~' command to match strings, and
  546.         fall back on /bin/expr or /bin/test for numeric comparisons and
  547.         /bin/test for querying the file system.
  548.  
  549.         If you are using rc on a system that lacks a stand-alone
  550.         /bin/test and /bin/[ and has test built into sh, the following
  551.         serves as an interim replacement:
  552.  
  553.             #! /bin/sh
  554.             `basename $0` ${1+"$@"}
  555.             # end-of-file
  556.  
  557.         This applies to Unicos and HP-UX v8.  Note that trip.rc, the
  558.         script provided with the rc sources to check basic functionality
  559.         after compilation, will fail unless a suitable test command
  560.         exists in the path.
  561.  
  562.         SCO UNIX implements /bin/test in precisely this way.  You can
  563.         almost certainly get better performance by using the GNU
  564.         implementation of /bin/test (see 7.4), although this is
  565.         obviously more important on an i386 machine than on a Cray.
  566.  
  567.         It is possible to get at the file system obliquely through the
  568.         globbing mechanism and the builtin cd command, and in some
  569.         circumstances these can be of use (see, for example, 5.7).  Such
  570.         speed hacks need to be carefully justified.
  571.  
  572.         See also 7.3.
  573.  
  574. 4.14    Does rc have true, false, and `:' built in?
  575.  
  576.         No.  The following functions are just as fast:
  577.  
  578.             fn true  { return 0 }
  579.             fn false { return 1 }
  580.             fn : { return 0 }
  581.  
  582.         Note that an indefinite loop in rc does not require the use of
  583.         an explicit `true' command:
  584.  
  585.             while () {
  586.                 ...
  587.             }
  588.  
  589. 5.      Using rc as a login shell
  590.  
  591. 5.1     How do I make rc my login shell?
  592.  
  593.         On most systems, you or the system administrator will have to
  594.         add a line to /etc/shells containing the full path to rc.  This
  595.         will need to be done on each machine on which you wish to use
  596.         rc.
  597.  
  598.         Next, use the chsh command to specify your new preference.  If
  599.         your machine is running Yellow Pages (YP), you will have to do
  600.         this on the YP server, otherwise you will have to do this on
  601.         each machine individually.  (To determine if your machine is
  602.         using YP, type `ypwhich -m passwd' that will either print the
  603.         name of your YP server or give an error if your machine is not
  604.         running YP.)  Often, you will also need to change your password
  605.         (using the yppasswd command) on the YP server to propagate the
  606.         new /etc/passwd entry contain your selected shell; changing your
  607.         password to itself will normally suffice.
  608.  
  609.         An alternative method is to invoke rc from your .profile file or
  610.         its equivalent, but this has so many drawbacks that I cannot
  611.         recommend it.
  612.  
  613. 5.2     How do I use rc in a heterogeneous cluster?
  614.  
  615.         There are two issues: finding the correct executable when you
  616.         login, and getting the correct path in scripts run using the #!
  617.         kernel hack.
  618.  
  619.         If you have root privileges on every machine on the cluster, by
  620.         far the simplest solution is the place a copy of rc in /bin and
  621.         add /bin/rc to /etc/shells on each machine.  Use `#! /bin/rc' in
  622.         shell scripts.
  623.  
  624.         Otherwise, use a sh wrapper around rc.  Create a shell script
  625.         called, say, rc-login, and make this your login shell.  The
  626.         purpose of this script is to set SHELL and PATH to point
  627.         appropriately according to the machine, and then to exec rc.
  628.  
  629.         The following script is suitable for adaptation:
  630.  
  631.             #! /bin/sh
  632.  
  633.             # determine OS
  634.             TMP=/tmp/$$
  635.             TMP=$TMP sh -c 'exec >$TMP ; echo OS=`uname -m`'
  636.             . $TMP
  637.             rm -f $TMP
  638.  
  639.             # determine location of rc executable
  640.             case $OS in
  641.             RISC)
  642.                PATH=$HOME/bin/mips:$PATH
  643.                SHELL=$HOME/bin/mips/rc
  644.                ;;
  645.             sun4c)
  646.                PATH=$HOME/bin/sparc:$PATH
  647.                SHELL=$HOME/bin/sparc/rc
  648.                ;;
  649.             alpha)
  650.                PATH=$HOME/bin/alpha:$PATH
  651.                SHELL=$HOME/bin/alpha/rc
  652.                ;;
  653.             *)
  654.                echo 1>&2 "WARNING: unknown architecture"
  655.                SHELL=/bin/sh
  656.                ;;
  657.             esac
  658.  
  659.             # determine if this is a login shell
  660.             case $SHELL in
  661.             */rc)
  662.               case $0 in
  663.               -*)
  664.                  FLAG=-l
  665.                  ;;
  666.               esac
  667.               ;;
  668.             esac
  669.  
  670.             export PATH
  671.             export SHELL
  672.  
  673.             exec $SHELL $FLAG ${1+"$@"}
  674.  
  675.             # end-of-file
  676.  
  677.         The dance to capture the output of uname is necessary because of
  678.         a bug whereby redirection does not work in the original v7 sh if
  679.         stdout is closed.  (This bug lives on in the Ultrix /bin/sh.)
  680.  
  681.         The easiest way to manage shell scripts is to keep a master copy
  682.         in a central directory, say $home/bin/share, and have slave
  683.         scripts in machine-specific directories which source the master
  684.         script.  For example, for a command called `foo' create the
  685.         following in each machine-specific directory:
  686.  
  687.             #! /full/path/to/machine/specific/rc
  688.             . $home/bin/share/foo $*
  689.             # end-of-file
  690.  
  691.         See 5.3 if your cluster contains Apollos.
  692.  
  693. 5.3     What problems occur with rc on Apollos?
  694.  
  695.         Apollos require the login shell to be a binary, so you cannot
  696.         use a sh script wrapper around rc.
  697.  
  698. 5.4     What problems occur with rc on SYSV-derived systems?
  699.  
  700.         Many SYSV-derived sh and ksh are `restricted' if the final
  701.         component of SHELL contains the letter `r'.  A restricted shell
  702.         will not allow you to cd, perform output redirection, set PATH,
  703.         or specify absolute paths to executables, meaning that many
  704.         shell scripts written in sh or ksh will fail.  This is also
  705.         applies to BSD-derived systems that also ship with a SYSV-
  706.         derived shell (for example, /bin/sh5 on Ultrix).
  707.  
  708.         One solution is to link rc to a file that does not contain the
  709.         letter `r', and use that file as your SHELL.  You can achieve
  710.         this automatically by placing the following commands in your
  711.         .rcrc:
  712.  
  713.             rcdir = `{ dirname $SHELL } {
  714.                 test -f $rcdir/RC || ln $rcdir/rc $rcdir/RC
  715.                 SHELL = $rcdir/RC
  716.             }
  717.  
  718.         Make sure when you install a new version of rc that you remove
  719.         the previous RC.
  720.  
  721.         This behaviour is also a potential problem if you use a sh
  722.         wrapper around rc as your login shell (see 5.2 and 6.3).  Make
  723.         sure that the name of the wrapper does not contain the letter
  724.         `r'.
  725.  
  726. 5.5     What problems occur with rc on NeXTs?
  727.  
  728.         The NeXT window system assumes that all shells are `job control'
  729.         shells and will place themselves in a new process group.
  730.         Contrary to this assumption, rc is not a job control shell and
  731.         so does not do this.  The result is that a signal to one (such
  732.         as an interrupt) will be sent to all.
  733.  
  734.         The `newpgrp' builtin-in command was provided to deal with this
  735.         problem.  You can use it to ensure that each new interactive rc
  736.         belongs to a new process group by using a prompt function to run
  737.         code in each new interactive instance of rc (see 6.1 and 6.2).
  738.         Other more elegant solutions (such as a wrapper around rc) are
  739.         precluded by the uncooperative and inflexible nature of the NeXT
  740.         window system.
  741.  
  742.         See also 6.1 and 6.2.
  743.  
  744. 5.6     How do I keep down the size of my .rcrc file?
  745.  
  746.         Given the nature of rc, the natural place to define functions is
  747.         in the .rcrc file.  However, as the number of functions grows,
  748.         it becomes more awkward to maintain them all in a single file.
  749.         One solution is to break out the functions into separate files
  750.         and automatically load them within .rcrc:
  751.  
  752.             fnlib = $home/fnlib
  753.             fn load { ~ $#* 1 && builtin . $fnlib/$1 }
  754.             builtin cd $fnlib
  755.             name = () for ( name in * ) load $name
  756.             builtin cd
  757.  
  758.         See also 5.7.
  759.  
  760. 5.7     How do I speed up the execution of my .rcrc file?
  761.  
  762.         If you have lots of functions, you may wish to use lazy function
  763.         loading:
  764.  
  765.             fnlib = $home/fnlib
  766.             fn load { ~ $#* 1 && builtin . $fnlib/$1 }
  767.             builtin cd $fnlib
  768.             name = () for ( name in * ) fn $name { load $0 && $0 $* }
  769.             builtin cd
  770.  
  771.         Often, though, a more substantial amount of time is spent
  772.         testing directories before they are added to a path: rc does not
  773.         have a built-in test command, so each test implies a fork and an
  774.         exec.  In some circumstances, you can replace the test with code
  775.         similar to the following:
  776.  
  777.             path = ()
  778.             dir = () for ( dir in ... )
  779.                 builtin cd $dir >[1=] >[2=] && path = ( $path $dir )
  780.             builtin cd
  781.  
  782.         See also 5.6 and 4.13.
  783.  
  784. 6.      Interactions between rc and the rest of UNIX
  785.  
  786. 6.1     How do I get each new interactive rc to do ...?
  787.  
  788.         Rather than set the `-l' flag and place the code in .rcrc, you
  789.         can use a prompt function like the following:
  790.  
  791.             fn prompt {
  792.                 if ( ! ~ $ipid $pid ) {
  793.                     ipid=$pid
  794.                     ...
  795.                 }
  796.             }
  797.  
  798.         This will run ... at the start of each new interactive instance
  799.         of rc, but not in non-interactive instances of rc.
  800.  
  801.         See also 6.2 and 5.5.
  802.  
  803. 6.2     How do I get rc to do ... in each new xterm?
  804.  
  805.         Since xterm puts WINDOWID into the environment, you can use a
  806.         prompt function like the following:
  807.  
  808.             fn prompt {
  809.                 if ( ! ~ $wid $WINDOWID && ~ $TERM xterm* ) {
  810.                     wid=$WINDOWID
  811.                     ...
  812.                 }
  813.             }
  814.  
  815.         See also 6.1.
  816.  
  817. 6.3     How do I get rc to read .rcrc when I use rsh?
  818.  
  819.         rshd does not place a `-' in the first character of argv[0] when
  820.         it execs a shell, so .rcrc is not run and this can cause
  821.         problems.
  822.  
  823.         One solution is to have a false login shell called login-shell
  824.         that explicitly supplies a -l flag to rc, i.e., login-shell
  825.         contains:
  826.  
  827.             #! /path/to/rc
  828.             exec /path/to/rc -l $*
  829.             # end-of-file
  830.  
  831.         See also 5.2.
  832.  
  833. 6.4     How do I get at or batch to run an rc script?
  834.  
  835.         at and batch only accept sh or csh scripts, so you need to
  836.         package your rc script in a sh wrapper.  The following two
  837.         functions accomplish this.  Note that they do not completely
  838.         emulate the standard commands, as they will not accept a file
  839.         name on the command line, but rather only read commands from
  840.         stdin.  A simple further enhancement might be to mail the stdout
  841.         and stderr of the job back to the user.
  842.  
  843.             fn at {
  844.                 if ( ~ $#* 0 ) {
  845.                     echo >[2=1] \
  846.                         'usage: at [ time [day] | -r job ... | -l [job ...] ]'
  847.                     return 1
  848.                 }
  849.                 switch ( $1 ) {
  850.                 case -r
  851.                     builtin at $*
  852.                 case -l
  853.                     builtin at $* >[2=1]
  854.                 case *
  855.                     {
  856.                         echo 'SHELL='^$SHELL
  857.                         echo 'export SHELL'
  858.                         echo 'sed ''s/^-//'' <<''+'' | '^$SHELL
  859.                         sed 's/^/-/'
  860.                         echo '+'
  861.                     } | SHELL = /bin/sh builtin at $*
  862.                 }
  863.             }
  864.  
  865.             fn batch {
  866.                 if ( ! ~ $#* 0 ) {
  867.                     echo >[1=2] 'usage: batch'
  868.                     return 1
  869.                 }
  870.                 {
  871.                     echo 'SHELL='^$SHELL
  872.                     echo 'export SHELL'
  873.                     echo 'sed ''s/^-//'' <<''+'' | '^$SHELL
  874.                     sed 's/^/-/'
  875.                     echo '+'
  876.                 } | SHELL = /bin/sh builtin batch
  877.             }
  878.  
  879. 7.      Programming in rc
  880.  
  881. 7.1     What makes rc such a good scripting language?
  882.  
  883.         Brian Kernighan and Rob Pike wrote of the original v7 Bourne
  884.         shell:
  885.  
  886.             The UNIX shell isn't typical of command interpreters:
  887.             although it lets you run commands in the usual way, because
  888.             it is a programming language it can accomplish much more.
  889.  
  890.         Their comments apply equally to rc, perhaps more so.
  891.  
  892.         The two aspects of rc that contribute most to its utility as a
  893.         scripting language are its simple quoting rule coupled with the
  894.         absence of rescanning and the fact that the language is small,
  895.         simple, and well defined.
  896.  
  897.         The former allows one to write scripts with complete control
  898.         over the lexical aspects of the process: when and if globbing,
  899.         pattern matching, and list formation take place.  Tom Duff wrote
  900.         that rc is `not a macro processor'.  The lack or rescanning,
  901.         together with improvements to backquote substitutions, allowed
  902.         him to collapse Bourne's four types of quoting down to one,
  903.         eliminating all of their complex interactions.
  904.  
  905.         The latter means that the whole language can be readily
  906.         understood by the user.  Its grammar is well defined, in sharp
  907.         contrast to sh.  (Indeed, a skeleton yacc grammar is included as
  908.         an appendix to rc's manual page.)  The language is small and
  909.         well defined; there are, to quote John Mackin, no `dark
  910.         corners'.  Although the language is smaller and simpler, it
  911.         retains all the power and flexibility of sh, and improves and
  912.         extends on it in several areas.
  913.  
  914. 7.2     What are the trade-offs between functions and scripts?
  915.  
  916.         Functions are run in the process that invokes them, so they can
  917.         make persistent changes to the environment of the interpreter.
  918.         They are essential for some actions (reading a variable, for
  919.         example).
  920.  
  921.         Functions make excellent wrappers around standard commands, as
  922.         one can use the `builtin' command to invoke the standard command
  923.         from within the function.  Invoking a standard command from
  924.         within a script of the same name involves either hard-wiring the
  925.         name into the script or using a more elaborate method, such as
  926.         the `pathos' script posted to the mailing list in June 1992.
  927.  
  928.         There is less overhead in invoking a function, as the kernel
  929.         need not exec rc once again.
  930.  
  931.         However, functions take up space in the environment.  There is
  932.         often a hard limit on the size of the environment.  Although on
  933.         some systems this limit is very large, on others it is not.  In
  934.         addition, very large environments can degrade the performance of
  935.         fork.
  936.  
  937.         Functions cannot be exec-ed directly by a C program, but must be
  938.         invoked through rc, although it is probably a good idea to do
  939.         this anyway, as responsibility for globbing and variable
  940.         substitution properly resides with the shell.
  941.  
  942. 7.3     What can I do if I have deficient sh, awk, test, ... ?
  943.  
  944.         Shell programming on UNIX rests on two foundations: the shell
  945.         itself and the toolkit of programs provided in /bin and
  946.         /usr/bin.  Unfortunately, many vendors provide out-of-date tools
  947.         and there are many incompatibilities between BSD- and SYSV-
  948.         derived systems, so writing portable scripts is a problem.  This
  949.         problem can be especially acute in heterogeneous clusters.
  950.  
  951.         There are two approaches to this problem: one is to use only
  952.         those tools and options that were present in v7, and the other
  953.         is to use only those tools and options that are present in
  954.         POSIX.2, and to replace the vendor-supplied tools with those
  955.         from GNU if the former do not conform (see 7.4).
  956.  
  957. 7.4     Where do I obtain the GNU tools and other software?
  958.  
  959.         Get prep.ai.mit.edu:/pub/gnu/GNUinfo/FTP by anonymous FTP for a
  960.         list of sites that archive GNU software.
  961.  
  962.         The article `How to find sources' lists sites that archive
  963.         software posted to Usenet source newgroups.  This article is
  964.         regularly cross-posted to comp.sources.wanted, comp.answers, and
  965.         news.answers, and is also available via anonymous FTP as
  966.         rtfm.mit.edu:/pub/usenet/comp.answers/finding-sources.
  967.  
  968. 7.5     How do I perform arithmetic in rc?
  969.  
  970.         You can use /bin/expr, /bin/awk, or unary arithmetic with lists
  971.         (see 7.6 for an example).
  972.  
  973. 7.6     How do I find the first occurrence of an element in a list?
  974.  
  975.         Use an `index' function, as in:
  976.  
  977.             ; x = ( a b a b )
  978.             ; index b $x
  979.             2
  980.  
  981.         This implementation of index uses a list to count up to the
  982.         index of the element:
  983.  
  984.             fn index {
  985.                 switch ( $#* ) {
  986.                 case 0 1
  987.                     echo >[1=2] 'usage: index pattern list ...'
  988.                     return 1
  989.                 case *
  990.                     elem = $1 prefix = () {
  991.                         shift
  992.                         if ( ! ~ $elem $* ) {
  993.                             echo 0
  994.                             return 1
  995.                         }
  996.                         while ( prefix = ( $prefix $1 ) && ! ~ $elem $1 )
  997.                             shift
  998.                         echo $#prefix
  999.                         return 0
  1000.                     }
  1001.                 }
  1002.             }
  1003.  
  1004. 7.7     How do I use ranges in array subscripts in rc?
  1005.  
  1006.         Use a `seq' function, as in:
  1007.  
  1008.             ; x = ( a b c d e f g h )
  1009.             ; echo $x( `{seq 2 5} )
  1010.             b c d e
  1011.  
  1012.         There are many implementations of seq, most of which use awk,
  1013.         like this one:
  1014.  
  1015.             fn seq {
  1016.                 switch ( $#* ) {
  1017.                 case 1
  1018.                     * = ( 1 $1 )
  1019.                 case 2
  1020.                 case *
  1021.                     echo >[1=2] usage: seq [start] stop
  1022.                     return 1
  1023.                 }
  1024.                 echo $1 $2 | awk '{
  1025.                     if ( $1 <= $2 )
  1026.                         for ( i = $1; i <= $2; i++ ) print i;
  1027.                     else
  1028.                         for ( i = $1; i >= $2; i-- ) print i;
  1029.                 }'
  1030.             }
  1031.  
  1032. 7.8     How do I perform the equivalent of read in rc?
  1033.  
  1034.         Use a `read' function, to read a single line from stdin, remove
  1035.         the trailing newline, and assign it to a variable:
  1036.  
  1037.             ; read foo
  1038.             Do the funky gibbon!
  1039.             ; echo $foo
  1040.             Do the funky gibbon!
  1041.             ; echo $#foo
  1042.             1
  1043.  
  1044.         The following implementation of read returns 1 if EOF is seen,
  1045.         like the read in the v7 sh:
  1046.  
  1047.             fn read {
  1048.                 if ( ! ~ $#* 1 ) {
  1049.                     echo >[1=2] 'usage: read variable'
  1050.                     return 1
  1051.                 }
  1052.                 if ( ~ $1 '*' () *'='* ) {
  1053.                     echo >[1=2] 'read: bad variable name'
  1054.                     return 1
  1055.                 }
  1056.                 lines = () nl = () {
  1057.                     ifs = () { nl = `{ echo } }
  1058.                     ifs = $nl { lines = `{ line ; echo $status } }
  1059.                     if ( ~ $#lines 2 ) {
  1060.                         $1 = $lines(1)
  1061.                         return 0
  1062.                     } else {
  1063.                         $1 = ''
  1064.                         return $lines(1)
  1065.                     }
  1066.                 }
  1067.             }
  1068.  
  1069.         If your UNIX does not have a line command, the following is an
  1070.         straightforward substitute:
  1071.  
  1072.             #include <unistd.h>
  1073.             int main ()
  1074.             {
  1075.                 unsigned char c;
  1076.  
  1077.                 while (read (0, &c, 1) == 1 && c != '\n')
  1078.                     write (1, &c, 1);
  1079.                 write (1, "\n", 1);
  1080.                 return c != '\n';
  1081.             }
  1082.  
  1083.         Alternatively, a patch to add a builtin read was posted to the
  1084.         mailing list in September 1993 (see 1.4).
  1085.  
  1086. Acknowledgements
  1087.  
  1088. Many people have contributed ideas, code, and information to this
  1089. document, either directly or indirectly, through the rc mailing list and
  1090. its influence on my understanding of rc.
  1091.  
  1092. People who have directly contributed to this document include Vincent
  1093. Broman, Raymond Chen, Tom Culliton, Stefan Dalibor, Jim Davis, Matt Day,
  1094. Matthew Farwell, David Fiander, Tim Goodwin, Paul Haahr, Charles Hannum,
  1095. David Hogan, Noel Hunt, John LoVerso, Hamish Macdonald, John Mackin,
  1096. Byron Rakitzis, Boyd Roberts, Rich Salz, Chris Siebenmann, Emin Gun
  1097. Sirer, Icarus Sperry, Gerry Tomlinson, Malte Uhl, and Christopher Vance.
  1098. My apologies to anyone I have missed.
  1099.  
  1100. As a user of rc, I am indebted to Tom Duff for its design, to Steve
  1101. Bourne for its foundation, and to Byron Rakitzis for its implementation.
  1102.  
  1103. Alan Watson (alan@oldp.astro.wisc.edu)
  1104.