5.3. Syntax

 < Day Day Up > 

This section describes the syntax used by tcsh . The topics are arranged as follows:

  • Special files

  • Filename metacharacters

  • Quoting

  • Command forms

  • Redirection forms

5.3.1. Special Files

Filename

Description

/etc/csh.cshrc

Read by any shell before reading per-user initialization files.

~/.tcshrc or ~/.cshrc

Executed at each instance of shell startup. If no ~/.tcshrc is found, tcsh tries ~/.cshrc.

/etc/csh.login

Read by login shell before reading per-user initialization files.

~/.login

Executed by login shell after .tcshrc.

~/.cshdirs

Used to reload the directory stack after executing ~/.login. (See the savedirs variable.)

~/.history

History list saved from previous login.

/etc/csh.logout

Executed by login shell at logout, before ~/.logout.

~/.logout

Executed by login shell at logout.

/etc/passwd

Source of home directories for ~name abbreviations. (May come from NIS or NIS+ instead.)


Example startup files are available from http://tcshrc.sourceforge.net.

5.3.2. Filename Metacharacters

Metacharacters

Meaning

*

Match any string of zero or more characters.

?

Match any single character.

[abc...]

Match any one of the enclosed characters; a hyphen can be used to specify a range (e.g., a-z, A-Z, 0 9).

[^abc...]

Match any character not enclosed as above.

{abc,xxx,...}

Expand each comma-separated string inside braces. The strings need not match actual filenames.

~

Home directory for the current user.

~name

Home directory of user name.

=n

The nth entry in the directory stack, counting from zero.

=-

The last entry in the directory stack.

^pattern

Matches anything that pattern does not match. To work correctly, pattern must contain ?, *, or [...], and should not contain {...} or ~.


5.3.2.1. Examples
     % ls new*            Match new and new.1     % cat ch?            Match ch9 but not ch10     % vi [D-R ]*          Match files that begin with uppercase D through R     % ls {ch,app}?       Expand, then match ch1, ch2, app1, app2     % mv info{,.old}     Expands to mv info info.old     % cd ~tom            Change to tom's home directory     % touch aa bb cc     Create some files     % ls ^a*             List nonmatching filenames     bb  cc

On modern systems, ranges such as [D-R] are not portable; the system's locale may include more than just the uppercase letters from D to R in the range.


5.3.3. Quoting

Quoting disables a character's special meaning and allows it to be used literally, as itself. The characters in the following table have special meaning to tcsh .

Characters

Description

;

Command separator

&

Background execution

( )

Command grouping

|

Pipe

* ? [ ] ~ ^

Filename metacharacters

{ }

String expansion characters (usually don't require quoting )

< > & !

Redirection symbols

! ^

History substitution, quick substitution

" ' \

Used in quoting other characters

'

Command substitution

$

Variable substitution

space tab newline

Word separators


The characters that follow can be used for quoting:


" "

Everything between " and " is taken literally except for the following characters, which keep their special meaning:


$

Variable substitution will occur.


'

Command substitution will occur.


"

The end of the double quote.


\

Escape next character.


!

The history character.


newline

The newline character.


' '

Everything between ' and ' is taken literally except for ! (history), another ', and newline.


\

The character following a \ is taken literally. Use within " " to escape ", $, ', and newline. Use within ' ' to escape newlines. Often used to escape itself, spaces, or newlines. Always needed to escape a history character (usually !).

5.3.3.1. Examples
     % echo 'Single quotes "protect" double quotes'     Single quotes "protect" double quotes     % echo "Don't double quotes protect single quotes too?"     Don't double quotes protect single quotes too?     % echo "You have 'ls|wc -l' files in 'pwd'"     You have       43 files in /home/bob     % echo The value of \$x is $x     The value of $x is 100

5.3.4. Command Forms

Command

Action

cmd &

Execute cmd in the background.

cmd1 ; cmd2

Command sequence; execute multiple cmds on the same line.

(cmd1 ; cmd2)

Subshell; treat cmd1 and cmd2 as a command group.

cmd1 | cmd2

Pipe; use output from cmd1 as input to cmd2.

cmd1 'cmd2'

Command substitution; run cmd2 first and use its output as arguments to cmd1.

cmd1 && cmd2

AND; execute cmd1 and then (if cmd1 succeeds) cmd2. This is a "short-circuit" operation; cmd2 is never executed if cmd1 fails.

cmd1 || cmd2

OR; execute either cmd1 or (if cmd1 fails) cmd2. This is a "short-circuit" operation; cmd2 is never executed if cmd1 succeeds.


5.3.4.1. Examples
     % nroff file > file.out &               Format in the background     % cd; ls                                Execute sequentially     % (date; who; pwd) > logfile            All output is redirected     % sort file | pr -3 | lp                Sort file, page output, then print     % vi 'grep -l ifdef *.c'                Edit files found by grep     % egrep '(yes|no)' 'cat list'           Specify a list of files to search     % grep XX file && lp file               Print file if it contains the pattern,     % grep XX file || echo XX not found     otherwise, echo an error message

5.3.5. Redirection Forms

File descriptor

Name

Common abbreviation

Typical default

0

Standard input

stdin

Keyboard

1

Standard output

stdout

Screen

2

Standard error

stderr

Screen


The usual input source or output destination can be changed with the redirection commands listed in the following sections.

5.3.5.1. Simple redirection

Command

Action

cmd > file

Send output of cmd to file (overwrite).

cmd >! file

Same as preceding, even if noclobber is set.

cmd >> file

Send output of cmd to file (append).

cmd >>! file

Same as preceding, even if noclobber is set.

cmd < file

Take input for cmd from file.

cmd << text

Read standard input up to a line identical to text (text can be stored in a shell variable). Input usually is typed at the keyboard or in the shell program. Commands that typically use this syntax include cat, echo, ex, and sed. If text is quoted (using any of the shell's quoting mechanisms), the input is passed through verbatim. Otherwise, the shell performs variable and command substitutions on the input. When quoting text, the ending delimiter must be quoted identically.


5.3.5.2. Multiple redirection

Command

Action

cmd >& file

Send both standard output and standard error to file.

cmd >&! file

Same as preceding, even if noclobber is set.

cmd >>& file

Append standard output and standard error to end of file.

cmd >>&! file

Same as preceding, even if noclobber is set.

cmd1 |& cmd2

Pipe standard error together with standard output.

(cmd > f1) >& f2

Send standard output to file f1 and standard error to file f2.

cmd | tee files

Send output of cmd to standard output (usually the screen) and to files. (See the Example in Chapter 2 under tee.)


5.3.5.3. Examples
     % cat part1 > book                          Copy part1 to book     % cat part2 part3 >> book                   Append parts 2 and 3 to same file as part1     % mail tim < report                         Take input to message from report     % cc calc.c >& error_out                    Store all messages, including errors     % cc newcalc.c >&! error_out                Overwrite old file     % grep Unix ch* |& pr                       Pipe all messages, including errors     % (find / -print > filelist) >& no_access   Separate error messages from list of files     % sed 's/^/XX /' << "END_ARCHIVE"           Supply text right after command     ? This is often how a shell archive is "wrapped",      ? bundling text for distribution. You would normally      ? run sed from a shell program, not from the command line.      ? "END_ARCHIVE"      XX This is often how a shell archive is "wrapped",     XX bundling text for distribution.  You would normally     XX run sed from a shell program, not from the command line.

     < Day Day Up > 


    Unix in a Nutshell
    Unix in a Nutshell, Fourth Edition
    ISBN: 0596100299
    EAN: 2147483647
    Year: 2005
    Pages: 201

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net