Reference Tables

This chapter contains tables of syntax elements for all three shells and examples of shell scripts.

Environment Files

Description Bourne Korn C
Read at login. .profile .profile .login
Read at invocation of shell.  Any file specified in .profile with ENV=file. By convention, file is usually .kshrc .cshrc

First Line of Script

Shell Syntax
Bourne #!/bin/sh
Korn #!/bin/ksh
C #!/bin/csh -f

Korn Shell Path Operators

Operator Description
${variable# pattern} Delete the shortest part at the beginning of the variable that matches the pattern and return the rest.
${variable## pattern} Delete the longest part at the beginning of the variable that matches the pattern and return the rest.
${variable% pattern} Delete the shortest part at the end of the variable that matches the pattern and return the rest.
${variable%% pattern} Delete the longest part at the end of the variable that matches the pattern and return the rest.

C Shell Path Modifiers

Modifier Meaning Description
:e Extension Remove prefix ending with a dot.
:h Head Remove trailing path-name component.
:r Root Remove suffixes beginning with a dot (.).
:t Tail Remove all leading path-name components.
:q Quote Force variable to be quoted. Used to quote $argv.
:x Quote Like q, but break into words at each space, Tab, or newline.

Bourne and Korn Shell Built-in Variables Initialized by Shell

Variable Explanation
$* List the value of all command-line parameters. This variable is useful only in scripts because the login shell has no arguments associated with it.
$# Return the number of command-line arguments (in decimal). Useful only in scripts.
$? Return the exit status (in decimal) of the last command executed. Most commands return a zero exit status if they complete successfully; otherwise a non-zero exit status is returned. This variable is set after each command is executed.
$$ Return the process ID (PID) number of the current shell (in decimal).
$! Return the process number (in decimal) of the last process run in the background.

C Shell Built-in Variables Initialized by Shell

Variable Explanation
$* List the value of all command-line parameters. This variable is useful only in scripts because the login shell has no arguments associated with it. Some people prefer to use $argv instead of $*.
$# Check whether a variable of that name has been set.

Shell Built-in Commands

The (K) in the Bourne or Korn Shell column indicates commands that are available only with the Korn shell.

Purpose Bourne or Korn Shell C Shell
Null command. : :
Create a command name alias. alias (K) alias
Run current command in background. bg (K) bg
Exit enclosing for or while loop. break break
Break out of a switch. N/A breaksw
Change directory. cd cd
Continue next iteration of for or while loop. continue continue
Default case in switch. N/A default
Print directory stack. N/A dirs
Write arguments on STDOUT. echo, print (K) echo
Evaluate and execute arguments. eval eval
Execute the arguments. exec exec
Return or set shell variables. set @
Exit shell program. exit exit
Create an environment variable. export setenv
Bring a command into foreground. fg (K) fg
Execute foreach loop. for foreach
Perform file name expansion. N/A glob
Go to label within shell program. N/A goto
Display history list. fc -l(K) history
if-then-else decision. if if
List active jobs. jobs (K) jobs
Send a signal. kill kill
Set limits for a job's resource use. ulimit limit
Terminate login shell and invoke login. N/A login
Terminate a login shell. exit logout
Change to a new user group. newgrp N/A
Change priority of a command. N/A nice
Ignore hang up. N/A nohup
Notify user when job status changes. N/A notify
Control shell processing on receipt of a signal. trap onintr
Pop the directory stack. N/A popd
Push a directory onto the stack. N/A pushd
Read a line from standard input. read $<
Change a variable to read-only. readonly N/A
Repeat a command n times. N/A repeat
Set shell environment variables. = setenv
Set a local C shell variable.  set
Shift positional parameters $* or $argv. shift shift
Read and execute a file. . (dot) source
Stop a background process. N/A stop
Stop the shell. suspend (K) suspend
case statement. case switch
Evaluate conditional expressions. test [ ] [[ ]] (K) N/A
Display execution times. times time
Set default security for creation of files and directories. umask umask
Discard aliases. unalias (K) unalias
Remove limitations on resources. ulimit unlimit
Unset a variable. unset unset
Unset an environment variable. unset unsetenv
until loop. until N/A
Wait for background process to complete. wait N/A
while loop foreground. while while

Bourne and Korn Shell Redirection

Command Description
<file, or 0< file Take standard input from file.
> file, or 1> file Redirect STDOUT to file.
2> file Redirect STDERR to file.
>> file Append STDOUT to file.
2>&1 Redirect STDERR to the place where STDOUT is directed.
cmd1 | cmd2 Pipe standard output of cmd1 as standard input to cmd2.
<> file Use file as both STDIN and STDOUT.
<&- Close STDIN.
>&- Close STDOUT.
2>&- Close STDERR.

C Shell Redirection Metacharacters

Command Description
> file Redirect STDOUT to file.
< file Take input from file.
>> file Append STDOUT to end of file.
>& file Redirect STDOUT and STDERR to file.
>>& file Append STDOUT and STDERR to file.
2>&- Close STDERR.

C Shell $argv Notation

Notation Description
$#argv Count the number of command-line arguments.
$* Display all arguments.
$argv Display all arguments.
$argv[1-3] Display arguments 1 through 3.
$0 Display the command used to run the shell script.
$argv[n] Display the nth argument.
$argv[$#argv} Display the last argument.

Quoting

Character Term Description
\cellBackslash Backslash Nullify the special meaning of the following shell metacharacter, including another backslash.
`` Backquotes Substitute the output of the command enclosed in backquotes as if it were typed in place of the command. Refer to the shell manual pages for more information.
'' Single quote Nullify the special meaning of all characters except bang (!), the backslash (), and the single quote itself ('). Single quotes are more restrictive than double quotes and do not permit variable or backquote expansion.
"" Double quotes Nullify the special meaning of all special characters except bang (!), backquote (``), and dollar sign ($). Permits variable and backquote expansion.

Metacharacter Shell Syntax

Feature Bourne and Korn C
Single-character wildcard. ? ?
Any number of characters. * *
Any one of the characters in the set of characters. [abc] [abc]
Any one character in the range of characters. [a-c] [a-c]
Any one character not in the range of characters specified. [!a-c] N/A

Variable Shell Syntax

Feature Bourne Korn C
Assigning regular variables. x=1 x=1 set x = 1
Accessing regular variables. echo $x echo $x echo $x
Assigning arrays. N/A y[0]=1; y[1]=2 set y=(1 2)
Accessing array elements. N/A echo $y echo ${y[1]} echo $y[1] $y[2]
Accessing entire array. N/A echo ${y[*]} echo $y
Exporting variables (make global). export var export var setenv command
Command-line arguments. N/A N/A $argv, $#argv, $argv[1}
Positional parameters. $*, $#, $1 $*, $#, $1 $*, $1
Setting positional parameters. set a b c set a b c N/A

I/O Redirection and Piping

Feature Bourne Korn C
STDOUT to file. > filename or 1> filename > filename or 1> filename > filename
STDIN from file. < filename or 0< filename < filename or 0< filename < filename
STDERR to file. 2> filename 2> filename N/A
Output and errors to file. 2>&1 2>&1 >& filename
Output to next command. | cmd | cmd | cmd
Output and errors to next command. 2>&1 | 2>&1 | |&

Printing to the Screen

Feature Bourne Korn C
Display text and variables. echo print or echo echo

Reading from the Keyboard

Feature Bourne Korn C
Read keyboard input. read name1 name2 . . . read name1 name2 . . . set var = $<

Math and Calculations

Feature Bourne Korn C
Perform a calculation. var=`expr a + b` let var= a + b @ var = (a + b)
Test a relational condition. var=`expr a < b` let var=a < b @ var = (a < b)

Command Substitution

Feature Bourne Korn C
Command substitution. `command` $(command) or `command` `command`

Tilde Expansion

Feature Korn C
Tilde represents user's home directory. ~loginid ~ ~loginid
Tilde represents current and previous directories. ~+ ~- - N/A

Alias Syntax

Feature Korn C
Create new alias. alias name=value alias name value
Display current list of values. alias alias
Remove alias from list. unalias name unalias name

History Syntax

Feature Korn C
Turn on history. automatic set history = num
Display history list. history or fc -l history
Display partial listing. history n m history -n history n
Reexecute a command. r string r number r !string !number !!

Function Syntax

Feature Bourne and Korn C
Create a function. func() {commands} function func {commands} func() {commands}
Use a function. Use func as a command Use func as a command

Programming Statement Syntax

Feature Bourne and Korn C
if conditional. if commandif (cond) then
 
then
  commands
elif command
											commands
else
  commands
fi

											commands
else if (cond) then
  commands
else
  commands
endif

switch and case pattern.
case variable in
  pattern)
  commands;;
*)
  commands;;
esac

switch (variable)
  case pattern:
  commands
  default:
   commands
endsw

while loops.
while command
do
  commands
done

while (cond)
 commands
end

for/foreach loops.
for variable in list
do
  commands
done

foreach variable (list)
 commands
end


Test and C Shell Built-in Test

What Is Tested Test Command C Shell Built-In
file is block device. -b file N/A
file is character device. -c file N/A
file is directory. -d file -d file
file or directory exists. -e file (Korn shell only) -e file
file is a file. -f file -f file
file has set-group-id bit set. -g file N/A
file has sticky bit set. -k file N/A
file is owned by executing user. N/A -o file
file is a named pipe. -p file N/A
Current user can read file. -r file -r file
file exists and has size >0. -s file N/A
n is a terminal file descriptor. -t n N/A
file has set-user-id bit set. -u file N/A
Current user can write to file. -w file -w file
Current user can execute file. -x file N/A
file has zero size. N/A -z file
string is NULL. -z string string == ""
string is NOT NULL. string != "" -n string, string
strings are equal. string = string string == string
strings are not equal. string != string string != string
string matches file name wildcard pattern. N/A string =~ pattern
string does not match file name wildcard pattern. N/A string !~ pattern
num1 is equal to num2. num1 -eq num2 num1 == num2
num1 is not equal to num2. num1 -ne num2 num1 != num2
num1 is less than num2. num1 -lt num2 num1 < num2
num1 is less than or equal to num2. num1 -le num2 num1 <= num2
num1 is greater than num2. num1 -gt num2 num1 > num2
num1 is greater than or equal to num2. num1 -ge num2 num1 >= num2
Logical AND. -a &&
Logical OR. -o ||
Logical NEGATION. ! !!

Bourne Shell Mathematical Operators

Operator Description
+ Addition.
- Subtraction.
* Multiplication.
/ Division.
% Remainder (modulus).

C Shell Mathematical Operators

Syntax Description
@ variable = (expression) Set value of variable equal to the expression.
@ variable += (expression) Addition.
@ variable -= (expression) Subtraction.
@ variable *= (expression) Multiplication.
@ variable /= (expression) Division.
@ variable ++ Add 1.
@ variable -- Subtract 1.

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

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