Sie sind auf Seite 1von 2

MT XIA INC.

PUBLICATIONS - QUICK REFERENCE


KORN SHELL 93 – TESTING & SUBSTITUTIONS [[ ... ]] Double Square Bracket Test
(( ... )) Numeric Comparision tests
Mt Xia publishes information on a variety of topics such as -u file true if file has SETUID bit set
Business Continuity, Disaster Recovery, High Availability, var = expr evaluate expression and assign to var.
-w file true if file is writable by current user
AIX, and Shell Programming. true if expr evaluates to non-zero
-x file true if file is executable by current user
n1 == n2 true if n1 is equal to n2
Mt Xia Inc. -z string true if string has zero length
n1 != n2 true if n1 is not equal to n2
113 East Rich
Norman, OK 73069 n1 < n2 true if n1 is less than n2
[[ ... ]] File and String Comparison
n1 <= n2 true if n1 is less than or equal to n2
Dana French, President file1 -nt file2 true if file1 is newer than file2 or file 2
dfrench@mtxia.com does not exist n1 > n2 true if n1 is greater than n2
615.556.0456 file1 -ot file2 true if file1 is older than file2 or file 2 n1 >= n2 true if n1 is greater than or equal to n2
does not exist
[[ ... ]] Double Square Bracket Test (( ... )) Numeric Evaluation Operators
file1 -ef file2 true if file1 and file2 are the same file
-a file true if string is not null (obsolete) string == pattern true if string matches pattern var = expr evaluate expression and assign result to
-b file true if file is a block device var
string != pattern true if string doesn't match pattern
-c file true if file is a character device + - addition, subtraction
string1 < string2 true if string1 is lexically less than
-C file true if file is a contiguous file string2 * / % multiplication, division, modulo

-d file true if file is a directory string1 > string2 true if string1 is lexically greater than ** exponentiation
string2
-e file true if file exists ++ -- auto-increment, auto-decrement

-f file true if file is a regular file && || boolean 'and' boolean 'or'
[[ ... ]] Numeric Comparison Tests (obsolete)
-g file true if file has SETGID bit set n1 -eq n2 true if n1 is equal to n2
name=value Variable Substitution / Testing
-G file true if file's group is effective GID n1 -ne n2 true if n1 is not equal to n2
${name} substituted for value of name
-h file true if file is a symbolic link n1 -lt n2 true if n1 is less than n2
${#name} number of characters in value
-k file true if file has sticky bit set n1 -le n2 true if n1 is less than or equal to n2
${name:-word} if name is unset or null, use word
-L file true if file is a symbolic link n1 -gt n2 true if n1 is greater than n2
${name:=word} if name is unset or null, assign
-n string true is string has non-zero length n1 -ge n2 true if n1 is greater than or equal to n2 word to name and substitute word
-o option true if option is on ${name:?word} if name is unset or null, print word
-O file true if file's owner is effective UID Numeric Evaluation Commands on STDERR and exit.

-p file true if file is a pipe (FIFO) (( expr )) true if expression evaluates to non-zero ${name:+word} if name is unset or null, use null,
otherwise use word
-r file true if file is readable by current user $(( expr )) true if expression evaluates to non-zero,
and substitutes expression for evaluated ${!name} name of variable index
-s file true if file has non-zero size
value
${!prefix*] all variables beginning with prefix.
-S file true if file is a socket
let 'expr' true if expression evaluates to non-zero,
${!prefix@} all variables beginning with prefix.
-t filedes true if filedes is a terminal can redirect STDOUT and STDERR

Copyright 2006 Mt Xia Inc, All Rights Reserved


MT XIA INC. PUBLICATIONS - QUICK REFERENCE
name=value Variable Substitution / Testing name[index]=value Array Substitutions name[index]=value Array Assignments

${name#pat} delete smallest matching pattern “${!name[@]}” all indexes of array name, each called name using an
from the beginning of value of value double quoted. index of word
name.
${#name[*]} number of array elements name=( [word]=”value” ... ) assign one or more
${name##pat} delete the largest matching pattern values to an associative
${#name[@]} number of array elements
from the beginning of value of array
name.
[[:class:]] Character Class
${name%pat} delete the smallest matching Pattern – filenames and strings
pattern from the end of value of [:alnum:] alphanumeric [:print:] printable
? match one single character
name.
[:alpha:] alphabetic [:punct:] punctuation
* match 0 or more characters
${name%%pat} delete the largest matching pattern
[:blank:] space or tab [:space:] whitespace
from the end of value of name. [...] match any single character from the set of
[:cntrl:] control [:upper:] uppercase characters between the brackets
${name:start} substitute substring of value from
position start beginning at zero. [:digit:] decimal [:lower:] lowercase [!...] match any single character not matching the set
of characters between the brackets
${name:start:length} substitute substring value from [:graph:] non-spaces [:xdigit:] hexadecimal
position start beginning at zero for [:word:] = [[:alnum:]_]
length number of characters. Pattern Operators
+(\d) = [[:digit:]] +(\D) = [![:digit:]]
${name/pat/string} substitute first occurrence of pat|pat|... pattern list can be one or more patterns.
pattern with string +(\s) = [[:space:]] +(\S) = [![:space:]] separated by pipe symbol '|' means 'or'.

${name//pat/string} substitute all occurrences of +(\w) = [[:word:]] +(\W) = [![:word:]] pat&pat&... pattern list can be one or more patterns.
pattern with string separated by ampersand '&' means 'and'

${name/#pat/string} substitute occurrence of pattern at name[index]=value Array Assignments ?(pat-list) match 0 or 1 occurrences of patterns
beginning of value with string name[n]=”value” assign a single array *(pat-list) match 0 or more occurrences of patterns
${name/%pat/string} substitute occurrence of pattern at element n to a value
+(pat-list) match 1 or more occurrences of patterns
end of value with string name=( ... ) assign one or more
@(pat-list) match exactly one occurrence of pattern
values to an array called
name[index]=value Array Substitutions name !(pat-list) match anything but any of the patterns

${name[n]} substitute array element n of array set -A name val1 ... assign one or more \n text matched by nth sub-pattern in (...)
name values to an array called
{n}(pat-list) match exactly n of any of the patterns
name
${name[word]} substitute array element word of {n,m}(pat-list) match n to m of any of the patterns
associative array name read -A name read values into an array
called name ~(-i:pattern) enable case sensitive option
“${name[*]}” all array elements, all values within
a single pair of double quotes typeset -A name declare an associative ~(+i:pattern) enable ignore case option
array, must be defined
“${name[@]}” all array elements, each value ~(-g:pattern) enable shortest matching pattern option
before any values can be
double quoted. assigned. ~(+g:pattern) enable longest matching pattern option
“${!name[*]}” all indexes of array name, all name[word]=”value” assign a single value to
values within single pair of double an associative array
quotes
Copyright 2006 Mt Xia Inc, All Rights Reserved

Das könnte Ihnen auch gefallen