Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99
Part IV
Appendixes
- A Command Quick Reference
- B Glossary
- C Quiz Answers
Appendix A
Command Quick Reference
by Frank Watson
This appendix summarizes and reviews the script elements you have covered:
- Reserved words and built-in shell commands
- Conditional expressions
- Arithmetic expressions (available Korn/Bash only)
- Parameters and variables
- Parameter substitution
- Pattern matching
- I/O
- Miscellaneous command summaries
- Regular expression wildcards
You can also find details not discussed earlier that are included here for completeness.
Reserved Words and Built-in Shell Commands
- . (period)
- executes the following command in the current shell instead of as a child process.
- : (colon)
- no-op command. Its arguments are processed for variable substitution.
- !!
- (Bash) re-executes the previous command.
- alias
- (only Korn/Bash) creates a short name for the command.
- bg
- (Korn/Bash) starts a suspended job running in background.
- break
- exits from current for, while, or until loop.
- case
- executes commands given for first pattern that match expr. Patterns can contain filename expansion wildcards.
case expr in
pattern1) commands ;;
pattern2) commands ;;
esac
- cd
- changes the directory.
- continue
- skips the rest of the commands in a loop and starts the next iteration of a loop.
- do
- indicates the start of a block of code, for example, in a for, while, or until loop.
- done
- indicates the end of a block of code, for example, in a for, while, or until loop.
- echo
- displays its arguments to standard output. Sometimes this is a built-in shell command replacing the external echo command.
- esac
- denotes the end of a case statement.
- eval
- causes the shell to reinterpret the command that follows.
- exec
- executes the following command which replaces the current process instead of running it as a child process.
- exit n
- ends the shell script with status code n.
- export
- marks the following variables, flagging them to be passed to any child processes and called programs. Korn/Bash enable assignment within the export command:
export VAR1=value VAR2=value
- false
- (Korn/Bash builtin) command that always returns an unsuccessful or logical false result.
- fc
- (Korn/Bash) displays or edits a command in history list.
- fg
- (Korn/Bash) brings a background or suspended job to the foreground.
- fi
- denotes the end of an if statement.
- for
- executes a block of code multiple times.
for variable [in list]
do
commands
done
- function
- (Korn/Bash) keyword to define a function enabling local variables.
- getopts
- a function called repeatedly in a loop to process the command line arguments.
- history
- (Korn/Bash) shows the most recent commands run by this user.
- if
- allows conditional execution.
if test-command
then commands
[elif commands]
[else commands]
fi
- integer
- (Korn/Bash) specifies an integer variable.
- jobs
- (Korn/Bash) lists the background and suspended jobs.
- kill
- sends a signal to a process; often used to terminate a process or to reinitialize a daemon background process.
- let
- (Korn/Bash) performs integer arithmetic.
- newgrp
- (Korn) changes your primary group, affecting the group of all new files and directories that you create.
- print
- (Korn) an alternative to echo.
- pwd
- prints the present working or current directory.
- r
- (Korn) re-executes the previous command.
- read
- waits for one line of standard input and saves each word in the following variables. If there are more words than variables, it saves the remaining words in the last variable.
- readonly
- marks the following variables to give error if an attempt to assign a new value is made.
- return
- returns from a function.
- select
- (Korn/Bash) presents a menu and enables user selection.
- set
- displays or changes shell options.
- shift
- discards $1 and shifts all the positional parameters up one to take its place.
- test
- (Korn/Bash builtin) provides many options to check files, strings, and numeric values. Often denoted by [ (left bracket).
- trap
- designates code to execute if a specific signal is received, such as:
0 exit from script
1 hangup/disconnect
2 intr key pressed (Ctrl-C or DEL)
3 quit key pressed
15 request to terminate process
- type
- displays the pathname of the following command or indicates whether it is built-in or an alias.
- typeset
- (Korn/Bash) sets the type of variable and optionally its initial value.
- ulimit
- displays or sets the largest file or resource limit.
- umask
- displays or sets a mask to affect permissions of any new file or directory you create.
- unalias
- (Korn/Bash) removes an alias.
- unset
- undefines the variables that follow.
- until
- (Korn/Bash) loops until the test command is true (successful).
until test-command
do
commands
done
- wait
- pauses until all background jobs are complete.
- whence
- (Korn) similar to the type command.
- while
- loops while a test command is true (successful).
while test-command
do
commands
done
Conditional Expressions
These can be used with
if [ test-expression ]
while [ test-expression ]
until [ test-expression ]
File Tests
-a file
| true if the file exists (Korn/Bash)
|
-b file
| true if the file is a block special device
|
-c file
| true if the file is a character special device
|
-d file
| true if the file is a directory
|
-f file
| true if the file is a regular file
|
-g file
| true if the file has the SGID permission bit set
|
-G file
| true if the files group matches the users group
|
-k file
| true if the file has the sticky bit set
|
-L file
| true if the file is a symbolic link
|
-O file
| true if the user running this command owns this file (Korn/Bash)
|
-p file
| true if the file is a named pipe or fifo
|
-r file
| true if the file is readable
|
-s file
| true if the file has a size greater than zero
|
-S file
| true if the file is a socket
|
-t filedes
| true if file descriptor is associated with a terminal device
|
-u file
| true if the file has its SUID permission bit set
|
-w file
| true if the file is writable
|
-x file
| true if the file is executable
|
String Tests
-z string
| true if the string is empty
|
-n string
| true if the string has nonzero size
|
s1 = s2
| true if string s1 equals s2
|
s1 != s2
| true if the strings are not equal
|
s1
| true if string s1 is not empty
|
s1 < s2
| true if s1 comes before s2 in ASCII order (Korn [[ ]])
|
s1 > s2
| true if s1 comes after s2 in ASCII order (Korn [[ ]])
|
Integer Comparisons
Comparisons stop on first non-digit.
n1 -eq n2
| true if n1 is equal in value to n2.
|
n1 -ne n2
| true if n1 is not equal to n2
|
n1 -gt n2
| true if n1 is greater than n2
|
n1 -ge n2
| true if n1 is greater than or equal ton2
|
n1 -lt n2
| true if n1 is less than n2
|
n1 -le n2
| true if n1 is less than or equal to n2
|
! expr
| true if expr is false (logical NOT)
|
-a
| logical AND (Bourne)
|
&&
| logical AND (Bash/Korn [[ ]])
|
-o
| logical OR (Bourne)
|
||
| logical OR (Bash/Korn [[ ]])
|
Arithmetic Expressions (Korn/Bash Only)
Follow the general format for variable assignment:
let "VARIABLE=integer_expresson"
To embed integer calculations within a command
$((integer_expression))
Operators Allowed in Korn/Bash Integer Expressions
- Logical operators return 1 for true and 0 for false
- This list is from highest to lowest operator precedence
-
| unary minus (negates the following value)
|
! ∼
| logical NOT, binary ones complement
|
* / %
| multiply, divide, modulus (remainder operation)
|
+ -
| add, subtract
|
>> <<
| right, left shift, for example:
|
| $((32 >> 2))
|
| gives 8 (right shift 32 by 2 bits is the same as division by 4)
|
<= >=
| less than or equal to, greater than or equal to
|
> <
| greater than, less than
|
== !=
| equal to, not equal to
|
&
| bitwise AND operation, for example:
|
| $((5 & 3))
|
| converts 5 to binary 101 and 3 to binary 011 and ANDs the bits to give 1 as the result
|
^
| bitwise exclusive OR operation
|
|
| bitwise regular OR operation
|
&&
| logical AND
|
||
| logical OR
|
*= /= %=
| C programming type assignment, for example,
|
| $((a *= 2))
|
| means multiply variable a * 2, save result in a, and substitute result
|
= += -=
| more C programming type assignments
|
>>= <<=
| more C programming type assignments using shift right, shift left
|
&= ^= |=
| more C programming type assignments using AND, exclusive OR, regular OR
|
Parameters and Variables
User-Defined Variables
USERVAR=value
| sets the contents of USERVAR to value
|
$USERVAR
| substitutes the contents of USERVAR
|
${USERVAR}
| also substitutes the contents of USERVAR. The braces are optional if there is no ambiguity.
|
User-defined variable names
- Must start with letter or _
- Can contain only letters, digits, or _
- Are often in capital letters to differentiate from UNIX commands
Korn/Bash 2.x Support Arrays
USERVAR[index]=value
| sets a value for array element denoted by index
|
${USERVAR[index]}
| substitutes a value into the command line
|
${USERVAR[*]}
| substitutes all array elements
|
${USERVAR[@]}
| substitutes all array elements as if individually double quoted
|
Note index must be an integer.
Korn array initialization
set -A USERVAR value1 value2 value3 ...
Bash array initialization
USERVAR=(value1 value2 value3 ...)
Built-in Shell Variables
$0
| name of the command or script being executed
|
$n
| positional parameters, that is, arguments given on the command line numbered 1 through 9
|
$#
| number of positional parameters given on command line
|
$*
| a list of all the command line arguments
|
$@
| a list of all command line arguments individually double quoted
|
$?
| The numeric exit status (that is, return code) of last command executed
|
$$
| PID (process ID) number of current shell
|
$!
| PID (process ID) number of last background command
|
Built-in Commands that Directly Affect Variables
getopts, export, read, readonly, unset
Two Types of Variables
- Environment variables are passed to any child processes.
- Local variables are not passed to any child processes.
Shell Variables
- CDPATH
- contains colon-separated list of directories to facilitate cd command
- HOME
- Your home directory
- IFS
- Internal field separator characters
- OPTARG
- The last cmd line arg processed by getopts (Korn/Bash)
- OPTIND
- The index of the last cmd line arg processed by getopts (Korn/Bash)
- PATH
- Contains a colon-separated list of directories to search for commands that are given without any slash
- PS1
- The primary shell prompt string
- PS2
- The secondary shell prompt string for continuation lines
- PWD
- The current directory
- RANDOM
- Returns a different random number (from 0 to 32,767) each time it is invoked
- REPLY
- The last input line from read via select command (Korn/Bash)
- SECONDS
- The numbers of seconds since shell invocation
- SHLVL
- The number of shells currently nested
- UID
- The numeric user ID number
Parameter Substitution
Parameter Substitution in Bourne/Korn/Bash
- ${parameter}
- substitutes the contents of the parameter, which can be a variable name or digit indicating a positional parameter.
- ${parameter:-word}
- substitutes the contents of the parameter but if it is empty or undefined, it substitutes the word, which might contain unquoted spaces.
- ${parameter:=word}
- substitutes the contents of the parameter but if it is empty or undefined, it sets parameter equal to the word and substitutes word.
- ${parameter:?message}
- substitutes the contents of the parameter, but if it is empty or undefined, aborts the script and gives the message as a final error. Message might contain unquoted spaces.
- ${parameter:+word}
- if parameter is not empty, it substitutes the word; otherwise it substitutes nothing.
Parameter Substitution Only in Korn/Bash
- ${#parameter}
- substitutes the number of characters in the contents of parameter.
- ${#array[*]}
- substitutes the number of elements in array.
- ${parameter#pattern}
- if the regular expression pattern given is found at start of the contents of parameter, it deletes the matching characters and substitutes the remainder. The smallest possible match is deleted.
- ${parameter##pattern}
- same as above but deletes the largest possible match at the start of parameter.
- ${parameter%pattern}
- same as above but deletes the smallest match at the end of parameter.
- ${parameter%%pattern}
- same as above but deletes the largest match at the end of parameter.
Pattern Matching
Rules for filename expansion:
- Any word on the command line containing a wildcard is expanded to a list of files which match the pattern word.
- If no filename matches are found, the pattern word is not substituted.
- Wildcards cannot match a leading period or a slash.
Pattern Wildcards Available in Bourne/Korn/Bash
*
| matches 0 or more of any character
|
?
| matches exactly 1 of any character
|
[list]
| matches exactly 1 of any character in list
|
[!list]
| matches exactly 1 of any character not in list
|
Pattern Wildcards Available Only in Korn
- ?(pattern1|pattern2...)
- matches any of the patterns
- *(pattern1|pattern2...)
- matches zero or more occurrences of the patterns
- +(pattern1|pattern2...)
- matches one or more occurrences of the patterns
- @(pattern1|pattern2...)
- matches only one of the patterns
- !(pattern1|pattern2...)
- matches anything except one of the patterns
I/O
Table A.1 Summary of Standard UNIX I/O
|
Abbreviation
| I/O description
| File Descriptor
|
|
STDIN
| Standard input
| 0
|
STDOUT
| Standard output
| 1
|
STDERR
| Standard error
| 2
|
|
cmd > file
| save STDOUT from UNIX command in file
|
cmd 1> file
| same as above
|
cmd >> file
| append STDOUT from UNIX command to file
|
cmd 1>> file
| same as above
|
cmd 2> file
| save STDERR from UNIX command in file
|
cmd 2>> file
| append STDERR from UNIX command in file
|
cmd < file
| provide STDIN to UNIX command from file instead of keyboard
|
cmd 0< file
| same as above
|
here Document
Provides STDIN to UNIX command from lines that follow until delimiter is found at start of line:
cmd << delimiter
one or more text lines
delimiter
cmd1 | cmd2
| pipe STDOUT of cmd1 as STDIN to cmd2
|
cmd | tee file
| save STDOUT of UNIX command in file but also pass same text as STDOUT
|
exec n> file
| redirect output of file descriptor n to (overwrite) file. This applies to subsequent UNIX commands.
|
exec n>> file
| same as above but append to file instead of overwriting
|
cmd 2>&1
| redirect STDERR from UNIX command to wherever STDOUT is currently going. This is useful when you want to save both output and errors in a file or pipe them together to another command, for example: cmd > file 2>&1 This saves both STDERR and STDOUT in file. 2>&1 must come after > file.
|
cmd >&2
| redirect STDOUT as STDERR. This should be done when echo displays an error message.
|
cmd 1>&2
| same as above
|
cmd n>&m
| redirect file descriptor n to wherever file descriptor m is currently going. This is a generalization of the above examples. Values of n and m above 2 can be used to save an I/O destination and retrieve it later.
|
exec n>&-
| close file descriptor n
|
Miscellaneous Command Summaries
Here is some helpful information about several commands often used in shell programming.
- echo
- display arguments to standard output
\b
| Backspace
|
\c
| Suppress trailing newline
|
\f
| Formfeed
|
\n
| Newline
|
\r
| Carriage return
|
\t
| Tab
|
\\
| Backslash
|
\0nn
| Character whose ASCII value is octal nn
|
- grep
- display lines that contain the given pattern
-i
| ignore upper versus lower case
|
-l
| list only filenames that contain a match, not the matching lines
|
-n
| include the file line number with each matching line displayed
|
-v
| reverse the test, which means ignore lines that contain the pattern
|
- printf
- display formatted text output, for example:
printf "text %[-]m.nx" arguments
-
| Left justify (optional)
|
m
| Minimum field length
|
n
| Maximum field length for string; number of characters to the right of decimal for floating point.
|
x
| Type of argument
|
s
| string
|
c
| character value
|
d
| decimal integer value
|
x
| hexadecimal value
|
o
| octal value
|
e
| exponential floating point value
|
f
| fixed floating point value
|
g
| general floating point value
|
- sort
- display lines in sorted order
-b
| ignore leading blanks
|
-d
| ignore leading punctuation
|
-f
| fold upper- and lowercase together
|
-n
| sort leading numbers by magnitude
|
-r
| sort in reverse order
|
+n
| ignore the first n fields when sorting
|
Regular Expression Wildcards
grep, fgrep, egrep, sed, vi, perl, and awk allow regular expression wildcards in search patterns.
Limited Regular Expression Wildcards
All regular expression patterns can include these wildcards:
^pattern
| only matches if pattern is at start of line
|
pattern$
| only matches if pattern is at end of line
|
.
| matches exactly 1 of any character
|
[list]
| matches exactly 1 of any character in list
|
[^list]
| matches exactly 1 of any character not in list
|
*
| matches 0 or more repetitions of previous element (char or expression)
|
.*
| matches 0 or more of any characters
|
Extended Regular Expression Wildcards
These are additional regular expression wildcards that are only supported in some commands:
\{n\}
| matches n repetitions of previous element
|
\{n,\}
| matches n or more repetitions of previous element
|
\{n,m\}
| matches at least n but not more than m reps of previous element
|
?
| matches 0 or 1 occurrences of previous element
|
+
| matches 1 or more occurrences of previous element
|
Summary
This appendix provides a quick reference for shell commands and features:
- Reserved words and built-in shell commands
- Conditional expressions
- Arithmetic expressions (Korn/Bash only)
- Parameters and variables
- Parameter substitution
- Pattern matching
- I/O
- Miscellaneous command summaries
- Regular expression wildcards
As you write scripts and become familiar with the concepts, you might find this summary helps you to locate a symbol, a command name, or the correct syntax.
Questions
- 1. What section of this summary describes how to append output to a file?
- 2. What section of this summary describes how to end a case statement?
- 3. What section of this summary enables you to determine whether the jobs command is supported in the Bourne shell?
- 4. What section of this summary enables you to determine whether the + sign is a generally supported regular expression wildcard?
- 5. What section of this summary enables you to determine which shell variable gives the numeric result code of the last command executed?