TOKEN=$(if [[ $TOKEN ]] then echo $TOKEN else cat ./cloud/token fi)
Run Code Online (Sandbox Code Playgroud)
所以我试图设置变量TOKEN。这可能是之前设置的,在这种情况下,我希望使用该值,如果没有,我希望通过捕获文件来分配它。
上面的方法不起作用,因为我缺乏 bash 技能!
您可以通过使用 bash 对默认参数值的支持来简化这一过程。来自 bash 文档:
Run Code Online (Sandbox Code Playgroud)${parameter:-word} Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
对于您的示例,您可以这样做:
TOKEN=${TOKEN:-$(cat ./cloud/token)}
Run Code Online (Sandbox Code Playgroud)