Bash one liner - 测试文件是否存在,如果其他文件出现错误,请测试源文件

时间:2014-07-11 13:38:00

标签: php bash testing

换句话说,我想把它变成一个单行:

    test -e ${MY_HOME}/setup-env.sh || { echo "ERROR: MY_HOME not defined or does not contain srtup-env.sh" 1>&2 ; exit 1; }

    . ${MY_HOME}/setup-env.sh

2 个答案:

答案 0 :(得分:12)

你可以使用这个衬垫:

[[ -e "${MY_HOME}/setup-env.sh" ]] && source "${MY_HOME}/setup-env.sh" || { echo "ERROR: MY_HOME not defined or does not contain srtup-env.sh" 1>&2 ; exit 1; }

答案 1 :(得分:3)

[[ -e ${MY_HOME}/setup-env.sh ]] && { source "${MY_HOME}/setup-env.sh"; exit; }; echo "ERROR: MY_HOME not defined or does not contain setup-env.sh" >&2; exit 1

或者如果非bash:

test -e "${MY_HOME}/setup-env.sh" && { . "${MY_HOME}/setup-env.sh"; exit; }; echo "ERROR: MY_HOME not defined or does not contain setup-env.sh" >&2; exit 1

对我来说,它实际上不是一个“单行”,而只是一种浓缩形式。