10.9. Special Variables and Modifiers

Built into the TC shell are several variables consisting of one character. The $ preceding the character allows variable interpretation. See Table 10.21.

Table 10.21. Variables and Their Meanings
Variable Example Meaning
$?var echo $?name Returns 1 if variable has been set, 0 if not.
$#var echo $#fruit Prints the number of elements in an array.
$%var echo $%name Prints number of characters in a variable or array.
$$ echo $$ Prints the PID of the current shell.
$< set name = $< Accepts a line of input from user up to newline.
$? echo $? Same as $status. Contains the exit status of the last command.
$! kill $! Contains the process id number of the last job put in the background.

Example 10.74.
1  >set num
   > echo $?num
					1

2  > echo $path
					/home/jody/ellie  /usr /bin  /usr/local/bin
   > echo $#path
					3

3  > echo $$
					245
   > tcsh
					Start a subshell
   > echo $$
					248

4  > set name = $<
					Christy Campbell
   > echo $name
					Christy

5  > set name = "$<"
					Christy Campbell
   > echo $name
					Christy Campbell
				

Explanation

  1. The variable num is set to null. The $? preceding the variable evaluates to one if the variable has been set (either to null or some value), and to zero if the variable has not been set.

  2. The path variable is printed. It is an array of three elements. The $ # preceding the variable extracts and prints the number of elements in the array.

  3. The $$ is the PID of the current process, in this case, the C shell.

  4. The $< variable accepts a word of input from the user up to the first space or newline, whichever comes first, and stores the word in the name variable. The value of the name variable is displayed.

  5. The $< variable, when quoted (double quotes) accepts a line of input from the user up to, but not including, the newline, and stores the line in the name variable. The value of the name variable is displayed.

10.9.1. Pathname Variable Modifiers

If a pathname is assigned to a variable, it is possible to manipulate the pathname variable by appending special TC shell extensions to it. The pathname is divided into four parts: head, tail, root, and extension. See Table 10.22 for examples of pathname modifiers and what they do.

Table 10.22. Pathname Modifiers set pn = /home/ellie/prog/check.c
Modifier Meaning Example Result
:r root echo $pn:r /home/ellie/prog/check
:h head echo $pn:h /home/ellie/prog
:t tail echo $pn:t check.c
:e extension echo $pn:e c
:g global echo $p:gt (See Example 10.75)

Example 10.75.
1  > set pathvar = /home/danny/program.c

2  > echo $pathvar:r
						/home/danny/program

3  > echo $pathvar:h
						/home/danny
 
4  > echo $pathvar:t
						program.c

5  > echo $pathvar:e
						c

6  > set pathvar = ( /home/* )
						echo $pathvar
						/home/jody /home/local /home/lost+found /home/perl /home/tmp

7  > echo $pathvar:gt
						jody  local  lost+found  perl tmp
					

Explanation

  1. The variable pathvar is set to /home/danny/program.c.

  2. When:r is appended to the variable, the extension is removed when displayed.

  3. When :h is appended to the variable, the head of the path is displayed; that is, the last element of the path is removed.

  4. When :t is appended to the variable, the tail end of the path (the last element) is displayed.

  5. When :e is appended to the variable, the extension is displayed.

  6. The variable is set to /home/*. The asterisk expands to all the pathnames in the current directory starting in /home/.

  7. When :gt is appended to the variable, the tail end of each (global) of the path elements is displayed.

10.9.2. Upper- and Lowercase Modifiers

A special history modifier can be used to change the case of letters in a variable.

Table 10.23. Case Modifiers (tcsh, not csh)
:u uppercase the first lowercase letter in a word
:l lowercase the first uppercase letter in a word
:g apply a modifier once to each word
:a apply a modifier as many times as possible to a single word

Example 10.76.
1  > set name = nicky
   > echo $name:u
						Nicky

2  >set name = ( nicky jake )
   > echo $name:gu
						Nicky Jake

3  > echo $name:agu
						NICKY JAKE

4  > set name = ( TOMMY DANNY )
						> echo $name:agl
						tommy danny

5  > set name = "$name:agu"
						> echo $name
						TOMMY DANNY
					

Explanation

  1. When :u is appended to the variable, the first letter in its value is uppercased.

  2. When :gu is appended to the variable, the first letter in each word in the list of values is uppercased.

  3. When :agu is appended to the variable, all letters in its value are uppercased.

  4. When :agl is appended to the variable, all letters in its value are lowercased.

  5. The variable is reset with all letters in its list uppercased.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.246.245