如何通过bash脚本检查文件?

时间:2015-07-21 08:28:01

标签: bash shell

我试图克隆一个repo并在通过bash脚本完成后测试它。我根据Bash Shell: Check File Exists or Not编写了测试代码。

#!/bin/bash

echo "*** TRY TO INIT INFER ***"

# Clone Infer
INFER_GIT_PATH="https://github.com/facebook/infer.git"
echo "> Try to Clone Infer from ${INFER_GIT_PATH}"
git clone ${INFER_GIT_PATH}

INFER_PATH="/infer/infer/bin/infer"
[ -e ${INFER_PATH} ] && echo "Infer downloaded successfully" || echo "Something went wrong :("

虽然可以成功下载repo并且/infer/infer/bin/infer.sh存在,但我总是收到Something went wrong :(条消息。

任何建议都将不胜感激。感谢。

2 个答案:

答案 0 :(得分:2)

将其更改为此(使用相对路径):

INFER_PATH="./infer/infer/bin/infer"
[ -e ${INFER_PATH} ] && echo "Infer downloaded successfully" || echo "Something went wrong :("

它应该有用。

答案 1 :(得分:1)

如果您想知道文件是否存在,可以使用-f标志:

[ -f /infer/infer/bin/infer ] && echo "Infer downloaded successfully" || echo "Something went wrong :("