从一本英文ksh书籍里面总结出来的一些内容
KSH BASIC.. 2
Table 2.1: Command Execution Format2
Table 2.2: Some I/O Redirection Operators. 2
Table 2.3: File Descriptors. 2
Table 2.4: Redirect Operators and File Descriptors. 3
Table 2.5: Basic Pattern-Matching Characters. 3
Table 2.6: Other Patterns. 3
Table 2.7: Tilde Substitution. 4
Table 3.1: Assigning Values to Variables. 4
Table 3.2: Assigning Values/Attributes to Variables. 4
Table 3.3: Some Variable Attributes. 4
Table 3.4: Some Preset Special Parameters. 5
Table 3.5: Variable Expansion Formats. 5
Table 3.6: More Variable Expansion Formats. 5
Table 3.7: Array Variables. 6
Table 3.7.1: Quote. 6
Table 4.2: Some Vi Command Mode Commands. 6
Table 4.3: Some Emacs/Gmacs In-Line Edit Commands. 7
Table 5.1: Job Control Commands. 8
Table 6.1: (( ))Arithmetic Operators (in order of precedence)8
Table 7.1: Some Korn Shell Environment Variables. 9
Table 7.2: Some Korn Shell Options. 9
Table 8.1: Positional Parameters. 10
Table 8.2: [[...]] String Operators. 10
Table 8.3: [[...]] File Operators. 11
Table 8.4: [[...]] Integer Operators. 11
Table 8.5: [[...]] Other Operators. 11
Table 8.5.1: case syntax. 12
Table 8.5.2: for syntax. 12
Table 8.5.3: if syntax. 12
Table 8.5.4: while syntax. 13
Table 8.5.5: until syntax. 13
Table 8.5.6: select syntax. 13
Table 8.6: print Escape Characters. 13
Table 8.6.1 READ Command. 14
Table 8.7: print Options. 14
Table 8.8: read Options. 14
Table 8.9: Korn Shell Debugging Options. 14
Table 8.10: Co-Processes. 15
Table 9.1: ulimit Options. 15
KSH B
ASIC
Table 2.1: Command Execution Format
command1
;
command2
execute
command1
followed by
command2
command
&
execute
command
asynchronously in the background; do not wait for completion
command1
|
command2
pass the standard output of
command1
to standard input of
command2
command1
&&
command2
execute
command2
if
command1
returns zero (successful) exit status
command1
| |
command2
execute
command2
if
command1
returns non-zero (unsuccessful) exit status
command
|&
execute
command
asynchronously with its standard input and standard output attached to the parent shell; use
read .
p
/
print -p
to manipulate the standard input/output (see
Chapter 8
)
command
/
continue
command
onto the next line
{
command
; }
execute
command
in the current shell
(
command
)
execute
command
in a subshell
Table 2.2: Some I/O Redirection Operators
<
file
redirect standard input from
file
>
file
redirect standard output to
file
. Create
file
if non-existent, else overwrite.
>>
file
append standard output to
file
. Create
file
if non-existent.
>|
file
redirect standard output to
file
. Create
file
if non-existent, else overwrite even if
noclobber
is enabled.
<>
file
open
file
for reading and writing as standard input
<&
-
close standard input
>&
-
close standard output
Table 2.3: File Descriptors
0
standard input
1
standard output
2
standard error
3-9
unassigned file descriptors
Table 2.4: Redirect Operators and File Descriptors
<&
n
redirect standard input from file descriptor
n
>&
n
redirect standard output to file descriptor
n
n
<
file
redirect file descriptor
n
from
file
n
>
file
redirect file descriptor
n
to
file
n
>>
file
redirect file descriptor
n
to
file
. Create
file
if non-existent, else overwrite.
n
>|
file
redirect file descriptor
n
to
file
. Create
file
even if
noclobber
is enabled.
n
<&
m
redirect file descriptor
n
input from file descriptor
m
n
>&
m
redirect file descriptor
n
output to file descriptor
m
n
<>
file
open
file
for reading and writing as file descriptor
n
n
<<
word
redirect to file descriptor
n
until
word
is read
n
<<
-
word
redirect to file descriptor
n
until
word
is read; ignore leading tabs
n
<&
-
close file descriptor
n
for standard input
n
>&
-
close file descriptor
n
for standard output
print -u
n args
redirect arguments to file descriptor
n
. If
n
is greater than
2
, it must first be opened with
exec
. If
n
is not specified, the default file descriptor argument is
1
(standard output).
read -u
n args
read input line from file descriptor
n
. If
n
is greater than
2
, it must first be opened with
exec
. If
n
is not specified, the default file descriptor argument is
0
(standard input).
Table 2.5: Basic Pattern-Matching Characters
?
match any single character
*
match zero or more characters, including null
[
abc
] match any character or characters between the brackets
[
x-z
] match any character or characters in the range
x
to
z
[
a
-
ce-g
] match any character or characters in the range
a
to
c
,
e
to
g
[
!
abc
] match any character or characters not between the brackets
[
!
x
-
z
] match any character or characters not in the range
x
to
z
-
strings
Table 2.6: Other Patterns
?(
pattern-list
)
match zero or one occurrence of any
pattern
*(
pattern-list
)
match zero or more occurrences of any
pattern
+(
pattern-list
)
match one or more occurrence of any
pattern
@(
pattern-list
)
match exactly one occurrence of any
pattern
!(
pattern-list
)
match anything except any
pattern pattern-list
multiple patterns must be separated with a
|
character
?(pattern1 | pattern2 | ... | patternn)
matches zero or one occurrence of any pattern
*(pattern1 | pattern2 | ... | patternn)
matches zero or more occurrences of any pattern
@(pattern1 | pattern2 | ... | patternn)
matches exactly one occurrence of any pattern
+(pattern1 | pattern2 | ... | patternn)
matches one or more occurrence of any pattern
!(pattern1 | pattern2 | ... | patternn)
matches all strings except those that match
Table 2.7: Tilde Substitution
~
replaced with
$HOME
~
user
replaced with the home directory of
user
~-
r
eplaced with
$OLDPWD
( previous directory)
~+
replaced with
$PWD
(current directory)
Table 3.1: Assigning Values to Variables
variable
= declare
variable
and set it to null
typeset
variable
= declare
variable
and set it to null
variable
=
value
assign
value
to
variable
typeset
variable
=
value
assign
value
to
variable
Table 3.2: Assigning Values/Attributes to Variables
typeset -
attribute variable=value
assign
attribute
and
value
to
variable
typeset
-
attribute variable
assign
attribute
to
variable
typeset
+attribute variable
remove
attribute
from
variable
Table 3.3: Some Variable Attributes
typeset -i
var
Set the type of
var
to be integer
typeset -l
var
Set
var
to lower case
typeset -L
var
Left justify
var
; the field width is specified by the first assignment
typeset -L
n var
Left justify
var
; set field width to
n
typeset -LZ
n var
Left justify
var
; set field width to
n
and strip leading zeros
typeset -r
var
Set
var
to be readonly (same as the
readonly
command)
typeset -R
var
Right justify
var
; the field width is specified by the first assignment
typeset -R
n var
Right justify
var
; set field width to
n
typeset -RZ
n var
Right justify
var
; set field width to
n
and fill with leading zeros
typeset -t
var
Set the user-defined attribute for
var
. This has no meaning to the Korn shell.
typeset -u
var
Set
var
to upper case
typeset -x
var
Automatically export
var
to the environment (same as the
export
command)
typeset -Z
var
Same as
typeset -RZ
Table 3.4: Some Preset Special Parameters
?
exit status of the last command
$
process id of the current Korn shell
-
current options in effect
! process id of the last background command or co-process
ERRNO
error number returned by most recently failed system call (system dependent)
PPID
process id of the parent shell
Table 3.5: Variable Expansion Formats
(%% ##)
${#
variable
} length of
variable
${variable:-word} value of variable if set and not null,else print word
${variable:=word} value of variable if set and not null,else variable is set to word, then expanded
${variable:+word} value of word if variable is set and not null, else nothing is substituted
${variable:?} value of variable if set and not null,else print "variable: parameter null or not set"
${variable:?word} value of variable if set and not null,else print value of word and exit
${variable#pattern} value of variable without the smallest beginning portion that matches pattern
${variable##pattern} value of variable without the largest beginning portion that matches pattern
${variable%pattern} value of variable without the smallest ending portion that matches pattern
${variable%%pattern} value of variable without the largest ending portion that matches pattern
${variable//pattern1/pattern2} replace all occurrences of pattern1 with pattern2 in variable
example
$ X=/usr/spool/cron
$ print ${X##*/}
cron
$ X=file.Z
$ print ${X%.*}
file
Table 3.6: More Variable Expansion Formats
${variable//pattern1/pattern2} replace all occurrences of pattern1 with pattern2 in variable
${variable/pattern1/pattern2} replace first occurrence of pattern1 with pattern2 in variable
${variable/#pattern1/pattern2} replace first occurrence of pattern1 with pattern2 in variable if variable begins with pattern1
${variable/%pattern1/pattern2} replace last occurrence of pattern1 with pattern2 in variable if variable ends with pattern1
${variable:start} return variable from character position start to end
${variable:start:length} return length characters from variable from character position start to end
Table 3.7: Array Variables
${
array
}, $
array
array element zero
${
array
[
n
]} array element
n
${
array
[
n
+2
]} array element
n
+2
${
array
[
$i
]} array element
$i
${
array
[*]}, ${
array
[@]} all elements of an array
${#
array
[*]}, ${#
array
[@]} number of array elements
${#
array
[
n
]} length of array element
n
${!
array
[*]}, ${!
array
[@]} all initialized subscript values
${!
array
[*]
:
n
:
x
} x array elements starting with element n
${!
array
[@]
:
n
} all array elements starting with element n
Table 3.7.1: Quote
Double quotes are like single quotes, except that they do not remove the meaning of the special characters
$
,
`
, and
/
.
Single quotes are also used to hide the meaning of special characters like
$
,
*
,
/
,
!
,
"
,
`
and
/
. Any characters between single quotes, except another single quote, are displayed without interpretation as special characters:
Back quotes are used to assign the output of a command to a variable. This format, from the Bourne shell, is accepted by the Korn shell but considered obsolescent. This command sets the variable
SYS
to the
Table 4.2: Some Vi Command Mode Commands
h
, <BACKSPACE> move left one character
l
, <SPACE> move right one character
b
move left one word
B
move left one word; ignore punctuation
w
move right one word
W
move right one word; ignore punctuation
e
move to the end of the next word
E
move to end of next word; ignore punctuation
^
move to beginning of the line
$
move to end of line
f
c
move right to character
c
F
c
move left to character
c
a
add text after the current character
A
append text to end of the current line
i
insert text left of the current character
r
c
replace current character with
c
x
delete the current character
u
undo the last text modification command
k
get previous command from history file
j
get next command from history file
/
string
search backward in the history file for command that matches
string
?
string
search forward in the history file for command that matches
string
.
repeat the last text modification command
~
toggle the case of the current character
Table 4.3: Some Emacs/Gmacs In-Line Edit Commands
Ctl-b
move left one character
Ctl-f
move right one character
Esc-b
move left one word
Esc-f
move right one word
Ctl-a
move to beginning of line
Ctl-e move to end of line
Ctl-
]
c
move right to character
c
Ctl-h
delete preceding character
Ctl-x
, @ kill the entire line
Ctl-k
delete from cursor to end of line
Ctl-d
delete current character
Esc-d
delete current word
Ctl-w
delete from cursor to mark
Ctl-y
undo last delete (w/
Esc-p
)
Ctl-p
get previous command from history file
Ctl-n
get next command from history file
Ctl-r
string
search backward in history file for command that contains
string
Ctl-c
change current character to upper case
Esc-l
change current character to lower case
Esc-p
save to buffer from cursor to mark
Esc-
<SPACE> mark current location
Ctl-l
redisplay current line
Table 5.1: Job Control Commands
bg
put the current stopped job in the background
bg %
n
put the stopped job
n
in the background
fg
move the current background job into the foreground
fg %
n
move background job
n
into the foreground
jobs
display the status of all jobs
jobs -l
display the status of all jobs along with their process ids
jobs -p
display the process ids of all jobs
kill %
n
kill job
n
kill -l
list all valid signal names
kill .
signal
%
n
send the specified signal to job
n
set -m
,
set -o monitor
enable job control; execute background jobs in a separate process group, and report the exit status of background jobs
stty tostop
prevent background jobs from generating output
stty -tostop
allow background jobs to generate output (default)
wait
wait for all background jobs to complete
wait %
n
wait for background
Table 6.1: (( ))Arithmetic Operators (in order of precedence)
. unary minus
! logical negation
~ bitwise negation
*, /, % multiplication, division, remainder (modulo)
+, . addition, subtraction
<<, >> left shift, right shift
<=, < less than or equal to, less than
>=, > greater than or equal to, greater than
== equal to
!= not equal to
& bitwise AND
^ bitwise exclusive OR
| bitwise OR
&& logical AND
| | logical OR
= assignment
*=, /=, %= multiply assign, divide assign, modulo assign
+=, .= increment, decrement
<<=, >>= left shift assign, right shift assign
&=, ^=, |= bitwise AND assign, bitwise exclusive
OR assign, bitwise OR assign
(...) grouping
Table 7.1: Some Korn Shell Environment Variables
CDPATH
search path for
cd
when not given a full pathname (no default)
COLUMNS
window width for in-line edit mode and
select
command lists (default
80
)
EDITOR
pathname of the editor for in-line editing (default
/bin/ed
)
ENV
pathname of the environment file (nodefault)
HISTFILE
pathname of the history file (default
$HOME/.sh_history
)
HISTSIZE
number of commands to save in the command history file (default
128
)
HOME
home directory
IFS
internal field separator (default space,tab, newline)
LANG
locale
MAIL
name of mail file
MAILCHECK
specifies how often to check for mail (default
600
seconds)
MAILPATH
search path for mail files (no default)
PATH
search path for commands (default
/bin:/usr/bin:
)
PS1
primary prompt string (default
$
,
#
)
Example:
$hostname($whoami):${PWD#/.../yamato.ibm.com/fs/home}$
patent2(patadm):/PATENT_DATA/2007/jpf_2007805/B9$
PS2
secondary prompt string (default
>
)
PS3 select
command prompt (default
#?
)
PS4
debug prompt string (default
+
)
SHELL
pathname of the shell
TERM
specifies your terminal type (no default)
TMOUT
Korn shell timeout variable (default 0)
VISUAL
pathname
Table 7.2: Some Korn Shell Options
set -a, set -o allexport automatically export variables when defined
set -o bgnice
execute all background jobs at a lower priority
set -o emacs
,
set -o gmacs
use
emacs
/
gmacs
in-line editor
set -o ignoreeof
do not exit on end of file; use
exit
(default
Ctl-d
)
set -o markdirs
display trailing
/
on directory names resulting from file name substitution
set -m
,
set -o monitor
enable job control (system dependent)
set -n
,
set -o noexec
read commands without executing them
set -o noclobber
prevent I/O redirection from truncating existing files
set -f
,
set -o noglob
disable file name expansion
set -u
,
set -o nounset
return error on substitution of unset variables
set -h
,
set -o trackall
make commands tracked aliases when first encountered
set -o vi
use
vi
-style editor for in-line editing
set -x
,
set -o xtrace
display commands and arguments as they are executed
Table 8.1: Positional Parameters
$0
name of script or function or pathname of Korn shell for
set
$
n n
th argument to script, function, or
set
${
n
}
n
th argument to script, function, or
set
when n is greater than 9
$# number of positional parameters
$*, $@ all positional parameters separated with a blank
"$*" all positional parameters enclosed in double quotes
${*:
X
} all
X
to the last positional parameters
"${*:
X
}" all
X
to the last positional parameters enclosed in double quotes
${*:
X
:
n
}
n
positional parameters beginning with
X
th
"${*:
X
:
n
}"
n
positional parameters beginning with
X
th enclosed in double quotes
Table 8.2: [[...]] String Operators
-n
string
true if length of
string
is not zero
-o
option
true if
option
is set
-z
string
true if length of
string
is zero
string1
=
string2
true if
string1
is equal to
string2
string1
!=
string2
true if
string1
is not equal to
string2
string
=
pattern
true if
string
matches
pattern
string
!=
pattern
true if
string
does not match
pattern
string1
<
string2
true if
string1
less than
string2
string1
>
string2
true if
string1
greater than
string2
note that:
[[$X=$Y]]
[[$X = $Y]]
error
while this is correct:
[[ $X == $Y ]]
there is white space between
$X
,
$Y
, and the
=
operator.
Table 8.3: [[...]] File Operators
-a
file
true if
file
exists
-d
file
true if
file
exists and is a directory
-f
file
true if
file
exists and is a regular file
-G
file
true if
file
exists and its group id matches the effective group id of the current process
-L
file
true if
file
exists and is a symbolic link
-O
file
true if
file
exists and its user id matches the effective user id of the current process
-r
file
true if
file
exists and is readable
-s
file
true if
file
exists and its size is g reater than zero
-S
file
true if
file
exists and is a socket
-u
file
true if
file
exists and its set user-id bit is set
-w
file
true if
file
exists and is writable
-x
file
true if
file
exists and is executable. If
file
is a directory, then true indicates that the directory is searchable.
file1
-ef
file2
true if
file1
exists and is another name for
file2
file1
-nt
file2
true if
file1
exists and is newer than
file2
file1
- ot
file2
true if
file1
exists and is older than
file2
Table 8.4: [[...]] Integer Operators
exp1
-eq
exp2
true if
exp1
is equal to
exp2 =
exp1
-ne
exp2
true if
exp1
is not equal to
exp2 !=
exp1
-le
exp2
true if
exp1
is less than or equal to
exp2 <=
exp1
-lt
exp2
true if
exp1
is less than
exp2 <
exp1
-ge
exp2
true if
exp1
is greater than or equal to
exp2 >=
exp1
-gt
exp2
true if
exp1
is greater than
exp2 >
Table 8.5: [[...]] Other Operators
[[
expression1
&&
expression2
]]
true if both
expression1
and
expression2
are true
[[
expression1
||
expression2
]]
true either
expression1
or
expression2
are true
[[
(
expression
)
]]
true if
expression
evaluates to true. The ()'s are used to override the precedence rules.
[[ !
expression
]]
true if
expression
evaluates to false
Table 8.5.1: case syntax
case
value
in
pattern1
)
command
command
;;
pattern2
)
command
command
;;
patternn
)
command
command
;;
esac
Table 8.5.2: for syntax
for
variable
in
word1 word2 . . . wordn
do
commands
done
Table 8.5.3: if syntax
if
command1
then
commands
fi
if
command1
then
commands
else
commands
fi
if
command1
then
commands
elif
command2
then
commands
. . .
elif
commandn
then
commands
else
commands
fi
Table 8.5.4: while syntax
while
command1
do
commands
done
Table 8.5.5: until syntax
until
command1
do
commands
done
Table 8.5.6: select syntax
select
variable
in
word1 word2
. . .
wordn
do
commands
done
Table 8.6: print Escape Characters
/a
bell character
/b
backspace
/c
line without ending newline (remaining arguments ignored)
/f
formfeed
/n
newline
/r
return
/t
tab
/v
vertical tab
//
backslash
/0
x
8-bit character whose ASCII code is the 1-, 2-, or 3-digit octal number
x
$ print "This is output" | read X Y Z
$ print $X
This
$ print $Y
is
$ print $Z
output
Table 8.7: print Options
-
treat everything following
.
as an argument, even if it begins with
.
-n
do not add a ending newline to the output
-p
redirect the given arguments to a coprocess
-r
ignore the
/
escape conventions
-R
ignore the
/
escape conventions; do not interpret
.
arguments as options (except
.n
)
-s
redirect the given arguments to the history file
-u
n
redirect arguments to file descriptor
n
. If the file descriptor is greater than
2
, it must first be opened with the
exec
command. If
n
is not specified, the default file descriptor is
1
.
Table 8.8: read Options
-p
read input line from a co-process
-r
do not treat
/
as the line continuation character
-s
save a copy of input line in the command history file
-u
n
read input line from file descriptor
n
. If the file descriptor is greater than
2
, it must first be opened with the
exec
command. If
n
is not specified, the default file descriptor is
0
.
Table 8.9: Korn Shell Debugging Options
set-e
,
set-o errexit
execute
ERR
trap (if set) on non-zero exit status from any commands
set-n
,
set-o noexec
read commands without executing them
set-v
,
set-o verbose
display input lines as they are read
set-x
,
set-o xtrace
display commands and arguments as they are executed
typeset-ft
function
display the commands and arguments from
function
as they are executed
command
|&
execute
command
in the background with the standard input and output attached to the shell
n
<&
p
redirect input from co-process to file descriptor
n
. If
n
is not specified, use standard input.
n
>&
p
redirect output of co-process to file descriptor
n
. If
n
is not specified, use standard output.
print -p
write to the standard input of a coprocess
read -p
read from the standard output of a coprocess
Table 9.1: ulimit Options
-a
displays all the current resource limits
-c
n
set the core dump size limit to
n
512 - byte blocks
-d
n
set the data area size limit to
n
kilobytes
-f
n
set the child process file write limit to
n
512-byte blocks (default)
-m
n
set the physical memory size limit to
n
kilobytes
-s
n
set the stack area size limit to
n
kilobytes
-t
n
set the process time limit to
n
seconds