dre*_*ard 2 linux shell bash command-line-interface
以前从未见过在 BASH 中使用过这个:
pidfile=${PIDFILE-/var/run/service.pid}
我以前从未见过/使用过的${PIDFILE-部分是部分。
Mik*_*kel 10
这意味着使用$PIDFILEif$PIDFILE已定义或/var/run/service.pidif$PIDFILE未定义。
从一个新的 shell 开始:
$ echo ${PIDFILE-/var/run/service.pid}
/var/run/service.pid
Run Code Online (Sandbox Code Playgroud)
现在定义PIDFILE:
$ PIDFILE=/var/run/myprogram.pid
$ echo ${PIDFILE-/var/run/service.pid}
/var/run/myprogram.pid
Run Code Online (Sandbox Code Playgroud)
它来自 Bourne Shell sh 手册页的旧时代。
${parameter-word}
If parameter is set then substitute its value;
otherwise substitute word.
Run Code Online (Sandbox Code Playgroud)
您可能已经看到的另一种形式是${parameter:-word}. 它是类似的,但如果parameter设置为空字符串,则行为不同。
${parameter:-word}
Use Default Values. If parameter is unset or null,
the expansion of word is substituted. Otherwise,
the value of parameter is substituted.
Run Code Online (Sandbox Code Playgroud)
展示:
$ set | grep NOSUCHVAR # produces no output because NOSUCHVAR is not defined
$ echo ${NOSUCHVAR-default}
default
$ echo ${NOSUCHVAR:-default}
default
$ NULLVAR=
$ set | grep NULLVAR # produces output because NULLVAR is defined
NULLVAR=
$ echo ${NULLVAR-default}
$ echo ${NULLVAR:-default}
default
Run Code Online (Sandbox Code Playgroud)
请注意如何${NULLVAR-default}扩展为空字符串,因为NULLVAR 已定义。
对于一个完整的解释,执行“人庆典”,并搜索参数扩展通过键入“/参数扩展”。
${parameter-word} 位被隐藏在这个解释中:
When not performing substring expansion, using the forms documented below,
bash tests for a parameter that is unset or null. Omitting the colon results
in a test only for a parameter that is unset.
Run Code Online (Sandbox Code Playgroud)
感谢 Dennis 关于 set 与 null 的更正。
| 归档时间: |
|
| 查看次数: |
318 次 |
| 最近记录: |