Appendix B. Reference Cards
优质
小牛编辑
129浏览
2023-12-01
The following reference cards provide a usefulsummaryof certain scripting concepts.The foregoing text treats these matters in more depth and givesusage examples.
Table B-1. Special Shell Variables
Variable | Meaning |
---|---|
$0 | Name of script |
$1 | Positional parameter #1 |
$2 - $9 | Positional parameters #2 - #9 |
${10} | Positional parameter #10 |
$# | Number of positional parameters |
"$*" | All the positional parameters (as a single word) * |
"$@" | All the positional parameters (as separate strings) |
${#*} | Number of command line parameters passed toscript |
${#@} | Number of command line parameters passed toscript |
$? | Return value |
$$ | Process ID (PID) of script |
$- | Flags passed to script (usingset) |
$_ | Last argument of previous command |
$! | Process ID (PID) of last job run in background |
*Must be quoted,otherwise it defaults to"$@".
Table B-2. TEST Operators: Binary Comparison
Operator | Meaning | ----- | Operator | Meaning |
---|---|---|---|---|
Arithmetic Comparison | String Comparison | |||
-eq | Equal to | = | Equal to | |
== | Equal to | |||
-ne | Not equal to | != | Not equal to | |
-lt | Less than | \< | Less than (ASCII) * | |
-le | Less than or equal to | |||
-gt | Greater than | \> | Greater than (ASCII) * | |
-ge | Greater than or equal to | |||
-z | String is empty | |||
-n | String is not empty | |||
Arithmetic Comparison | within double parentheses (( ... )) | |||
> | Greater than | |||
>= | Greater than or equal to | |||
< | Less than | |||
<= | Less than or equal to |
*If within adouble-bracket[[ ... ]] test construct,then no escape\ isneeded.
Table B-3. TEST Operators: Files
Operator | Tests Whether | ----- | Operator | Tests Whether |
---|---|---|---|---|
-e | File exists | -s | File is not zero size | |
-f | File is a regularfile | |||
-d | File is a directory | -r | File has readpermission | |
-h | File is a symbolic link | -w | File has writepermission | |
-L | File is a symbolic link | -x | File has executepermission | |
-b | File is a block device | |||
-c | File is a character device | -g | sgidflag set | |
-p | File is a pipe | -u | suidflag set | |
-S | File is a socket | -k | "sticky bit"set | |
-t | File is associated with aterminal | |||
-N | File modified since it was last read | F1 -nt F2 | File F1 is newerthan F2 * | |
-O | You own the file | F1 -ot F2 | File F1 is olderthan F2 * | |
-G | Group idof file same asyours | F1 -ef F2 | Files F1 and F2 are hard linksto the same file * | |
! | "NOT"(reverses sense of abovetests) |
*Binaryoperator(requires two operands).
Table B-4. Parameter Substitution and Expansion
Expression | Meaning |
---|---|
${var} | Value of var, same as$var |
${var-DEFAULT} | If varnot set, evaluate expressionas $DEFAULT* |
${var:-DEFAULT} | If varnot set or is empty,evaluate expression as $DEFAULT* |
${var=DEFAULT} | If varnot set, evaluate expressionas $DEFAULT* |
${var:=DEFAULT} | If varnot set, evaluate expressionas $DEFAULT* |
${var+OTHER} | If varset, evaluate expression as$OTHER, otherwise as null string |
${var:+OTHER} | If varset, evaluate expression as$OTHER, otherwise as null string |
${var?ERR_MSG} | If varnot set, print$ERR_MSG* |
${var:?ERR_MSG} | If varnot set, print$ERR_MSG* |
${!varprefix*} | Matches all previously declared variables beginning withvarprefix |
${!varprefix@} | Matches all previously declared variables beginning withvarprefix |
*Of course if varisset, evaluate the expression as$var.
Table B-5. String Operations
Expression | Meaning |
---|---|
${#string} | Length of $string |
${string:position} | Extract substring from $stringat $position |
${string:position:length} | Extract $lengthcharacters substring from $stringat $position |
${string#substring} | Strip shortest match of$substringfrom front of$string |
${string##substring} | Strip longest match of$substringfrom front of$string |
${string%substring} | Strip shortest match of$substringfrom back of$string |
${string%%substring} | Strip longest match of$substringfrom back of$string |
${string/substring/replacement} | Replace first match of$substringwith$replacement |
${string//substring/replacement} | Replace allmatches of$substringwith$replacement |
${string/#substring/replacement} | If $substringmatches frontend of$string, substitute$replacementfor$substring |
${string/%substring/replacement} | If $substringmatches backend of$string, substitute$replacementfor$substring |
expr match "$string" '$substring' | Length of matching $substring*at beginning of $string |
expr "$string" : '$substring' | Length of matching $substring*at beginning of $string |
expr index "$string" $substring | Numerical position in $stringof first character in $substringthat matches |
expr substr $string $position$length | Extract $lengthcharactersfrom $stringstarting at$position |
expr match "$string"'\($substring\)' | Extract $substring* atbeginning of $string |
expr "$string" :'\($substring\)' | Extract $substring* atbeginning of $string |
expr match "$string"'.*\($substring\)' | Extract $substring* atend of $string |
expr "$string" :'.*\($substring\)' | Extract $substring* atend of $string |
*Where $substringis aregular expression.
Table B-6. Miscellaneous Constructs
Expression | Interpretation |
---|---|
Brackets | |
if [ CONDITION ] | Test construct |
if [[ CONDITION ]] | Extended test construct |
Array[1]=element1 | Array initialization |
[a-z] | Range of characters within a Regular Expression |
Curly Brackets | |
${variable} | Parameter substitution |
${!variable} | Indirect variable reference |
{ command1; command2 } | Block of code |
{string1,string2,string3,...} | Brace expansion |
Parentheses | |
( command1; command2 ) | Command group executed within a subshell |
Array=(element1 element2 element3) | Array initialization |
result=$(COMMAND) | Execute command in subshell and assign result tovariable |
>(COMMAND) | Process substitution |
<(COMMAND) | Process substitution |
Double Parentheses | |
(( var = 78 )) | Integer arithmetic |
var=$(( 20 + 5 )) | Integer arithmetic, with variable assignment |
Quoting | |
"$variable" | "Weak" quoting |
'string' | "Strong" quoting |
Back Quotes | |
result=`COMMAND` | Execute command in subshell and assign result to variable |