|

楼主 |
发表于 2008-7-29 23:28:48
|
显示全部楼层
自问自答了,刚在ABS-Guide中搜到的:
${parameter-default}, ${parameter:-default}
If parameter not set, use default.
1 echo ${username-`whoami`}
2 # Echoes the result of `whoami`, if variable $username is still unset.
Note
${parameter-default} and ${parameter:-default} are almost equivalent. The extra : makes a difference only when parameter has been declared, but is null.
1 #!/bin/bash
2 # param-sub.sh
3
4 # Whether a variable has been declared
5 #+ affects triggering of the default option
6 #+ even if the variable is null.
7
8 username0=
9 echo "username0 has been declared, but is set to null."
10 echo "username0 = ${username0-`whoami`}"
11 # Will not echo.
12
13 echo
14
15 echo username1 has not been declared.
16 echo "username1 = ${username1-`whoami`}"
17 # Will echo.
18
19 username2=
20 echo "username2 has been declared, but is set to null."
21 echo "username2 = ${username2:-`whoami`}"
22 # ^
23 # Will echo because of :- rather than just - in condition test.
24 # Compare to first instance, above.
25
26
27 #
28
29 # Once again:
30
31 variable=
32 # variable has been declared, but is set to null.
33
34 echo "${variable-0}" # (no output)
35 echo "${variable:-1}" # 1
36 # ^
37
38 unset variable
39
40 echo "${variable-2}" # 2
41 echo "${variable:-3}" # 3
42
43 exit 0 |
|