Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
.TH RC 1
2
.SH NAME
3
rc, cd, eval, exec, exit, flag, rfork, shift, wait, whatis, ., ~ \- command language
4
.SH SYNOPSIS
5
.B rc
6
[
7
.B -srdiIlxepvV
8
]
9
[
10
.B -c
11
.I command
12
]
13
[
14
.B -m
15
.I initial
16
]
17
[
18
.I file
19
[
20
.I arg ...
21
]]
22
.SH DESCRIPTION
23
.I Rc
24
is the Plan 9 shell.
25
It executes command lines read from a terminal or a file or, with the
26
.B -c
27
flag, from
28
.I rc's
29
argument list.
30
.SS Command Lines
31
A command line is a sequence of commands, separated by ampersands or semicolons
32
.RB ( &
33
or
34
.BR ; ),
35
terminated by a newline.
36
The commands are executed in sequence
37
from left to right.
38
.I Rc
39
does not wait for a command followed by
40
.B &
41
to finish executing before starting
42
the following command.
43
Whenever a command followed by
44
.B &
45
is executed, its process id is assigned to the
46
.I rc
47
variable
48
.BR $apid .
49
Whenever a command
50
.I not
51
followed by
52
.B &
53
exits or is terminated, the
54
.I rc
55
variable
56
.B $status
57
gets the process's wait message (see
58
.IR wait (2));
59
it will be the null string if the command was successful.
60
.PP
61
A long command line may be continued on subsequent lines by typing
62
a backslash
63
.RB ( \e )
64
followed by a newline.
65
This sequence is treated as though it were a blank.
66
Backslash is not otherwise a special character.
67
.PP
68
A number-sign
69
.RB ( # )
70
and any following characters up to (but not including) the next newline
71
are ignored, except in quotation marks.
72
.SS Simple Commands
73
A simple command is a sequence of arguments interspersed with I/O redirections.
74
If the first argument is the name of an
75
.I rc
76
function or of one of
77
.I rc's
78
built-in commands, it is executed by
79
.IR rc .
80
Otherwise if the name starts with a slash
81
.RB ( / ),
82
it must be the path name of the program to be executed.
83
Names containing no initial slash are searched for in
84
a list of directory names stored in
85
.BR $path .
86
The first executable file of the given name found
87
in a directory in
88
.B $path
89
is the program to be executed.
90
To be executable, the user must have execute permission (see
91
.IR stat (2))
92
and the file must be either an executable binary
93
for the current machine's CPU type, or a shell script.
94
Shell scripts begin with a line containing the full path name of a shell
95
(usually
96
.BR /bin/rc ),
97
prefixed by
98
.LR #! .
99
.PP
100
The first word of a simple command cannot be a keyword unless it is
101
quoted or otherwise disguised.
102
The keywords are
103
.EX
104
	for in while if not switch fn ~ ! @
105
.EE
106
.SS Arguments and Variables
107
A number of constructions may be used where
108
.I rc's
109
syntax requires an argument to appear.
110
In many cases a construction's
111
value will be a list of arguments rather than a single string.
112
.PP
113
The simplest kind of argument is the unquoted word:
114
a sequence of one or more characters none of which is a blank, tab,
115
newline, or any of the following:
116
.EX
117
	# ; & | ^ $ = ` ' { } ( ) < >
118
.EE
119
An unquoted word that contains any of the characters
120
.B *
121
.B ?
122
.B [
123
is a pattern for matching against file names.
124
The character
125
.B *
126
matches any sequence of characters,
127
.B ?
128
matches any single character, and
129
.BI [ class ]
130
matches any character in the
131
.IR class .
132
If the first character of
133
.I class
134
is
135
.BR ~ ,
136
the class is complemented.
137
The
138
.I class
139
may also contain pairs of characters separated by
140
.BR - ,
141
standing for all characters lexically between the two.
142
The character
143
.B /
144
must appear explicitly in a pattern, as must the
145
first character of the path name components
146
.B .
147
and
148
.BR .. .
149
A pattern is replaced by a list of arguments, one for each path name matched,
150
except that a pattern matching no names is not replaced by the empty list,
151
but rather stands for itself.
152
Pattern matching is done after all other
153
operations.
154
Thus,
155
.EX
156
	x=/tmp echo $x^/*.c
157
.EE
158
matches
159
.BR /tmp/*.c ,
160
rather than matching
161
.B "/*.c
162
and then prefixing
163
.BR /tmp .
164
.PP
165
A quoted word is a sequence of characters surrounded by single quotes
166
.RB ( ' ).
167
A single quote is represented in a quoted word by a pair of quotes
168
.RB ( '' ).
169
.PP
170
Each of the following is an argument.
171
.PD 0
172
.HP
173
.BI ( arguments )
174
.br
175
The value of a sequence of arguments enclosed in parentheses is
176
a list comprising the members of each element of the sequence.
177
Argument lists have no recursive structure, although their syntax may
178
suggest it.
179
The following are entirely equivalent:
180
.EX
181
	echo hi there everybody
182
	((echo) (hi there) everybody)
183
.EE
184
.HP
185
.BI $ argument
186
.HP
187
.BI $ argument ( subscript )
188
.br
189
The
190
.I argument
191
after the
192
.B $
193
is the name of a variable whose value is substituted.
194
Multiple levels
195
of indirection are possible, but of questionable utility.
196
Variable values
197
are lists of strings.
198
If
199
.I argument
200
is a number
201
.IR n ,
202
the value is the
203
.IR n th
204
element of
205
.BR $* ,
206
unless
207
.B $*
208
doesn't have
209
.I n
210
elements, in which case the value is empty.
211
If
212
.I argument
213
is followed by a parenthesized list of subscripts, the
214
value substituted is a list composed of the requested elements (origin 1).
215
The parenthesis must follow the variable name with no spaces.
216
Subscripts can also take the form
217
.IB m - n
218
or
219
.IB m -
220
to indicate a sequence of elements.
221
Assignments to variables are described below.
222
.HP
223
.BI $# argument
224
.br
225
The value is the number of elements in the named variable.
226
A variable
227
never assigned a value has zero elements.
228
.HP
229
$"\c
230
.I argument
231
.br
232
The value is a single string containing the components of the named variable
233
separated by spaces.  A variable with zero elements yields the empty string.
234
.HP
235
.BI `{ command }
236
.br
237
.I rc
238
executes the
239
.I command
240
and reads its standard output, splitting it into a list of arguments,
241
using characters in
242
.B $ifs
243
as separators.
244
If
245
.B $ifs
246
is not otherwise set, its value is
247
.BR "'\ \et\en'" .
248
.HP
249
.BI <{ command }
250
.HP
251
.BI >{ command }
252
.br
253
The
254
.I command
255
is executed asynchronously with its standard output or standard input
256
connected to a pipe.
257
The value of the argument is the name of a file
258
referring to the other end of the pipe.
259
This allows the construction of
260
non-linear pipelines.
261
For example, the following runs two commands
262
.B old
263
and
264
.B new
265
and uses
266
.B cmp
267
to compare their outputs
268
.EX
269
	cmp <{old} <{new}
270
.EE
271
.HP
272
.IB argument ^ argument
273
.br
274
The
275
.B ^
276
operator concatenates its two operands.
277
If the two operands
278
have the same number of components, they are concatenated pairwise.
279
If not,
280
then one operand must have one component, and the other must be non-empty,
281
and concatenation is distributive.
282
.PD
283
.SS Free Carets
284
In most circumstances,
285
.I rc
286
will insert the
287
.B ^
288
operator automatically between words that are not separated by white space.
289
Whenever one of
290
.B $
291
.B '
292
.B `
293
follows a quoted or unquoted word or an unquoted word follows a quoted word
294
with no intervening blanks or tabs,
295
a
296
.B ^
297
is inserted between the two.
298
If an unquoted word immediately follows a
299
.BR $ 
300
and contains a character other than an alphanumeric, underscore,
301
or
302
.BR * ,
303
a
304
.B ^
305
is inserted before the first such character.
306
Thus
307
.IP
308
.B cc -$flags $stem.c
309
.LP
310
is equivalent to
311
.IP
312
.B cc -^$flags $stem^.c
313
.SS I/O Redirections
314
The sequence
315
.BI > file
316
redirects the standard output file (file descriptor 1, normally the
317
terminal) to the named
318
.IR file ;
319
.BI >> file
320
appends standard output to the file.
321
The standard input file (file descriptor 0, also normally the terminal)
322
may be redirected from a file by the sequence
323
.BI < file \f1,
324
or from an inline `here document'
325
by the sequence
326
.BI << eof-marker\f1.
327
The contents of a here document are lines of text taken from the command
328
input stream up to a line containing nothing but the
329
.IR eof-marker ,
330
which may be either a quoted or unquoted word.
331
If
332
.I eof-marker
333
is unquoted, variable names of the form
334
.BI $ word
335
have their values substituted from
336
.I rc's
337
environment.
338
If
339
.BI $ word
340
is followed by a caret
341
.RB ( ^ ),
342
the caret is deleted.
343
If
344
.I eof-marker
345
is quoted, no substitution occurs.
346
The standard input file
347
may also be redirected from a file by the sequence
348
.BI <> file \f1,
349
which opens
350
.I file
351
exactly once, for reading and writing.
352
.PP
353
Redirections may be applied to a file-descriptor other than standard input
354
or output by qualifying the redirection operator
355
with a number in square brackets.
356
For example, the diagnostic output (file descriptor 2)
357
may be redirected by writing
358
.BR "cc junk.c >[2]junk" .
359
.PP
360
A file descriptor may be redirected to an already open descriptor by writing
361
.BI >[ fd0 = fd1 ],
362
.BI <>[ fd0 = fd1 ],
363
or
364
.BI <[ fd0 = fd1 ]\f1.
365
.I Fd1
366
is a previously opened file descriptor and
367
.I fd0
368
becomes a new copy (in the sense of 
369
.IR dup (2))
370
of it.
371
A file descriptor may be closed by writing
372
.BI >[ fd0 =]
373
or
374
.BI <[ fd0 =]\f1.
375
.PP
376
Redirections are executed from left to right.
377
Therefore,
378
.B cc junk.c >/dev/null >[2=1]
379
and
380
.B cc junk.c >[2=1] >/dev/null
381
have different effects: the first puts standard output in
382
.BR /dev/null
383
and then puts diagnostic output in the same place, where the second
384
directs diagnostic output to the terminal and sends standard output to
385
.BR /dev/null .
386
.PP
387
.B newconn <>/net/tcp/clone >[1=0]
388
opens
389
.B /net/tcp/clone
390
exactly once for reading and writing and puts it on standard input and output.
391
.B lpd <>[3]/net/tcp/42/data
392
opens
393
.B /net/tcp/42/data
394
exactly once for reading and writing and puts it on file descriptor 3.
395
.SS Compound Commands
396
A pair of commands separated by a pipe operator
397
.RB ( | )
398
is a command.
399
The standard output of the left command is sent through a pipe
400
to the standard input of the right command.
401
The pipe operator may be decorated
402
to use different file descriptors.
403
.BI |[ fd ]
404
connects the output end of the pipe to file descriptor
405
.I fd
406
rather than 1.
407
.BI |[ fd0 = fd1 ]
408
connects output to
409
.I fd1
410
of the left command and input to
411
.I fd0
412
of the right command.
413
.PP
414
A pair of commands separated by
415
.B &&
416
or
417
.B ||
418
is a command.
419
In either case, the left command is executed and its exit status examined.
420
If the operator is
421
.B &&
422
the right command is executed if the left command's status is null.
423
.B ||
424
causes the right command to be executed if the left command's status is non-null.
425
.PP
426
The exit status of a command may be inverted (non-null is changed to null, null
427
is changed to non-null) by preceding it with a
428
.BR ! .
429
.PP
430
The
431
.B |
432
operator has highest precedence, and is left-associative (i.e. binds tighter
433
to the left than the right).
434
.B !
435
has intermediate precedence, and
436
.B &&
437
and
438
.B ||
439
have the lowest precedence.
440
.PP
441
The unary
442
.B @
443
operator, with precedence equal to
444
.BR ! ,
445
causes its operand to be executed in a subshell.
446
.PP
447
Each of the following is a command.
448
.PD 0
449
.HP
450
.B if (
451
.I list
452
.B )
453
.I command
454
.br
455
A
456
.I list
457
is a sequence of commands, separated by
458
.BR & ,
459
.BR ; ,
460
or newline.
461
It is executed and
462
if its exit status is null, the
463
.I command
464
is executed.
465
.HP
466
.B if not
467
.I command
468
.br
469
The immediately preceding command must have been
470
.BI if( list )
471
.IR command .
472
If its condition was non-zero, the
473
.I command
474
is executed.
475
.HP
476
.BI for( name
477
.B in
478
.IB arguments )
479
.I command
480
.HP
481
.BI for( name )
482
.I command
483
.br
484
The
485
.I command
486
is executed once for each
487
.IR argument 
488
with that argument assigned to
489
.IR name .
490
If the argument list is omitted,
491
.B $*
492
is used.
493
.HP
494
.BI while( list )
495
.I command
496
.br
497
The
498
.I list
499
is executed repeatedly until its exit status is non-null.
500
Each time it returns null status, the
501
.I command
502
is executed.
503
An empty
504
.I list
505
is taken to give null status.
506
.HP
507
.BI "switch(" argument "){" list }
508
.br
509
The
510
.IR list
511
is searched for simple commands beginning with the word
512
.BR case .
513
(The search is only at the `top level' of the
514
.IR list .
515
That is,
516
.B cases
517
in nested constructs are not found.)
518
.I Argument
519
is matched against each word following
520
.B case
521
using the pattern-matching algorithm described above, except that
522
.B /
523
and the first characters of
524
.B .
525
and
526
.B ..
527
need not be matched explicitly.
528
When a match is found, commands in the list are executed up to the next
529
following
530
.B case
531
command (at the top level) or the closing brace.
532
.HP
533
.BI { list }
534
.br
535
Braces serve to alter the grouping of commands implied by operator
536
priorities.
537
The
538
.I body
539
is a sequence of commands separated by
540
.BR & ,
541
.BR ; ,
542
or newline.
543
.HP
544
.BI "fn " name { list }
545
.HP
546
.BI "fn " name
547
.br
548
The first form defines a function with the given
549
.IR name .
550
Subsequently, whenever a command whose first argument is
551
.I name
552
is encountered, the current value of
553
the remainder of the command's argument list will be assigned to
554
.BR $* ,
555
after saving its current value, and
556
.I rc
557
will execute the
558
.IR list .
559
The second form removes
560
.IR name 's
561
function definition.
562
.HP
563
.BI "fn " note { list }
564
.br
565
.HP
566
.BI "fn " note
567
.br
568
A function with a special name will be called when
569
.I rc
570
receives a corresponding note; see
571
.IR notify (2).
572
The valid note names (and corresponding notes) are
573
.B sighup
574
.RB ( hangup ),
575
.B sigint
576
.RB ( interrupt ),
577
.BR sigalrm
578
.RB ( alarm ),
579
and
580
.B sigfpe
581
(floating point trap).
582
By default
583
.I rc
584
exits on receiving any signal, except when run interactively,
585
in which case interrupts and quits normally cause
586
.I rc
587
to stop whatever it's doing and start reading a new command.
588
The second form causes
589
.I rc
590
to handle a signal in the default manner.
591
.I Rc
592
recognizes an artificial note,
593
.BR sigexit ,
594
which occurs when
595
.I rc
596
is about to finish executing.
597
.HP
598
.IB name = "argument command"
599
.br
600
Any command may be preceded by a sequence of assignments
601
interspersed with redirections.
602
The assignments remain in effect until the end of the command, unless
603
the command is empty (i.e. the assignments stand alone), in which case
604
they are effective until rescinded by later assignments.
605
.PD
606
.SS Built-in Commands
607
These commands are executed internally by
608
.IR rc ,
609
usually because their execution changes or depends on
610
.IR rc 's
611
internal state.
612
.PD 0
613
.HP
614
.BI . " file ..."
615
.br
616
Execute commands from
617
.IR file .
618
.B $*
619
is set for the duration to the remainder of the argument list following
620
.IR file .
621
.I File
622
is searched for using
623
.BR $path .
624
.HP
625
.BI builtin " command ..."
626
.br
627
Execute
628
.I command
629
as usual except that any function named
630
.I command
631
is ignored in favor of the built-in meaning.
632
.HP
633
.BI "cd [" dir "]"
634
.br
635
Change the current directory to
636
.IR dir .
637
The default argument is
638
.BR $home .
639
.I dir
640
is searched for in each of the directories mentioned in
641
.BR $cdpath .
642
.HP
643
.BI "eval [" "arg ..." "]"
644
.br
645
The arguments are concatenated separated by spaces into a single string,
646
read as input to
647
.IR rc ,
648
and executed.
649
.HP
650
.BI "exec [" "command ..." "]"
651
.br
652
This instance of
653
.I rc
654
replaces itself with the given (non-built-in)
655
.IR command .
656
.HP
657
.BI "flag " f " [+-]"
658
.br
659
Either set
660
.RB ( + ),
661
clear
662
.RB ( - ),
663
or test (neither
664
.B +
665
nor
666
.BR - )
667
the flag
668
.IR f ,
669
where
670
.I f
671
is a single character, one of the command line flags (see Invocation, below).
672
.HP
673
.BI "exit [" status "]"
674
.br
675
Exit with the given exit status.
676
If none is given, the current value of
677
.B $status
678
is used.
679
.HP
680
.BR "rfork " [ nNeEsfFm ]
681
.br
682
Become a new process group using
683
.BI rfork( flags )
684
where
685
.I flags
686
is composed of the bitwise OR of the
687
.B rfork
688
flags specified by the option letters
689
(see
690
.IR fork (2)).
691
If no
692
.I flags
693
are given, they default to
694
.BR ens .
695
The
696
.I flags
697
and their meanings are:
698
.B n
699
is
700
.BR RFNAMEG ;
701
.B N
702
is
703
.BR RFCNAMEG ;
704
.B e
705
is
706
.BR RFENVG ;
707
.B E
708
is
709
.BR RFCENVG ;
710
.B s
711
is
712
.BR RFNOTEG ;
713
.B f
714
is
715
.BR RFFDG ;
716
.B F
717
is
718
.BR RFCFDG ;
719
and
720
.B m
721
is
722
.BR RFNOMNT .
723
.HP
724
.BI "shift [" n "]"
725
.br
726
Delete the first
727
.IR n
728
(default 1)
729
elements of
730
.BR $* .
731
.HP
732
.BI "wait [" pid "]"
733
.br
734
Wait for the process with the given
735
.I pid
736
to exit.
737
If no
738
.I pid
739
is given, all outstanding processes are waited for.
740
.HP
741
.BI whatis " name ..."
742
.br
743
Print the value of each
744
.I name
745
in a form suitable for input to
746
.IR rc .
747
The output is
748
an assignment to any variable,
749
the definition of any function,
750
a call to
751
.B builtin
752
for any built-in command, or
753
the completed pathname of any executable file.
754
.HP
755
.BI ~ " subject pattern ..."
756
.br
757
The
758
.I subject
759
is matched against each
760
.I pattern
761
in sequence.
762
If it matches any pattern,
763
.B $status
764
is set to zero.
765
Otherwise,
766
.B $status
767
is set to one.
768
Patterns are the same as for file name matching, except that
769
.B /
770
and the first character of
771
.B .
772
and
773
.B ..
774
need not be matched explicitly.
775
The
776
.I patterns
777
are not subjected to
778
file name matching before the
779
.B ~
780
command is executed, so they need not be enclosed in quotation marks.
781
.PD
782
.SS Environment
783
The
784
.I environment
785
is a list of strings made available to executing binaries by the
786
.B env
787
device
788
(see
789
.IR env (3)).
790
.I Rc
791
creates an environment entry for each variable whose value is non-empty,
792
and for each function.
793
The string for a variable entry has the variable's name followed by
794
.B =
795
and its value.
796
If the value has more than one component, these
797
are separated by ctrl-a
798
.RB ( '\e001' )
799
characters.
800
The string for a function is just the
801
.I rc
802
input that defines the function.
803
The name of a function in the environment is the function name
804
preceded by
805
.LR fn# .
806
.PP
807
When
808
.I rc
809
starts executing it reads variable and function definitions from its
810
environment.
811
.SS Special Variables
812
The following variables are set or used by
813
.IR rc .
814
.PD 0
815
.TP \w'\fL$promptXX'u
816
.B $*
817
Set to
818
.IR rc 's
819
argument list during initialization.
820
Whenever a
821
.B .
822
command or a function is executed, the current value is saved and
823
.B $*
824
receives the new argument list.
825
The saved value is restored on completion of the
826
.B .
827
or function.
828
.TP
829
.B $apid
830
Whenever a process is started asynchronously with
831
.BR & ,
832
.B $apid
833
is set to its process id.
834
.TP
835
.B $home
836
The default directory for
837
.BR cd .
838
.TP
839
.B $ifs
840
The input field separators used in backquote substitutions.
841
If
842
.B $ifs
843
is not set in
844
.IR rc 's
845
environment, it is initialized to blank, tab and newline.
846
.TP
847
.B $path
848
The search path used to find commands and input files
849
for the
850
.B .
851
command.
852
If not set in the environment, it is initialized by
853
.BR "path=(.\ /bin)" .
854
Its use is discouraged; instead use
855
.IR bind (1)
856
to build a
857
.B /bin
858
containing what's needed.
859
.TP
860
.B $pid
861
Set during initialization to
862
.IR rc 's
863
process id.
864
.TP
865
.B $prompt
866
When
867
.I rc
868
is run interactively, the first component of
869
.B $prompt
870
is printed before reading each command.
871
The second component is printed whenever a newline is typed and more lines
872
are required to complete the command.
873
If not set in the environment, it is initialized by
874
.BR "prompt=('%\ '\ '\ ')" .
875
.TP
876
.B $status
877
Set to the wait message of the last-executed program.
878
(unless started with
879
.BR &).
880
.B !
881
and
882
.B ~
883
also change
884
.BR $status .
885
Its value is used to control execution in
886
.BR && ,
887
.BR || ,
888
.B if
889
and
890
.B while
891
commands.
892
When
893
.I rc
894
exits at end-of-file of its input or on executing an
895
.B exit
896
command with no argument,
897
.B $status
898
is its exit status.
899
.PD
900
.SS Invocation
901
If
902
.I rc
903
is started with no arguments it reads commands from standard input.
904
Otherwise its first non-flag argument is the name of a file from which
905
to read commands (but see
906
.B -c
907
below).
908
Subsequent arguments become the initial value of
909
.BR $* .
910
.I Rc
911
accepts the following command-line flags.
912
.PD 0
913
.TP \w'\fL-c\ \fIstring\fLXX'u
914
.BI -c " string"
915
Commands are read from
916
.IR string .
917
.TP
918
.B -s
919
Print out exit status after any command where the status is non-null.
920
.TP
921
.B -e
922
Exit if
923
.B $status
924
is non-null after executing a simple command.
925
.TP
926
.B -i
927
If
928
.B -i
929
is present, or
930
.I rc
931
is given no arguments and its standard input is a terminal,
932
it runs interactively.
933
Commands are prompted for using
934
.BR $prompt .
935
.TP
936
.B -I
937
Makes sure
938
.I rc
939
is not run interactively.
940
.TP
941
.B -l
942
If
943
.B -l
944
is given or the first character of argument zero is
945
.BR - ,
946
.I rc
947
reads commands from
948
.BR $home/lib/profile ,
949
if it exists, before reading its normal input.
950
.TP
951
.B -m
952
Read commands to initialize
953
.I rc
954
from
955
.I initial
956
instead of from
957
.BR /rc/lib/rcmain .
958
.TP
959
.B -p
960
A no-op.
961
.TP
962
.B -d
963
A no-op.
964
.TP
965
.B -v
966
Echo input on file descriptor 2 as it is read.
967
.TP
968
.B -x
969
Print each simple command before executing it.
970
.TP
971
.B -r
972
Print debugging information (internal form of commands
973
as they are executed).
974
.PD
975
.SH SOURCE
976
.B /sys/src/cmd/rc
977
.SH "SEE ALSO"
978
Tom Duff,
979
``Rc \- The Plan 9 Shell''.
980
.SH BUGS
981
There should be a way to match patterns against whole lists rather than
982
just single strings.
983
.PP
984
Using
985
.B ~
986
to check the value of
987
.B $status
988
changes
989
.BR $status .
990
.PP
991
Functions containing here documents don't work.
992
.PP
993
Free carets don't get inserted next to keywords.