如何检查文件是否存在于shell脚本中

我想编写一个shell脚本来检查某个文件archived_sensor_data.json是否存在,如果是,则删除它。 遵循http://www.cyberciti.biz/tips/find-out-if-file-exists-with-conditional-expressions.html,我试过以下内容:

[-e archived_sensor_data.json] && rm archived_sensor_data.json

但是,这会引发错误

[-e: command not found

当我尝试使用./test_controller命令运行生成的test_controller脚本时。 代码有什么问题?


在括号和-e之间缺少空白。

#!/bin/bash
if [ -e x.txt ]
then
    echo "ok"
else
    echo "nok"
fi

这是一个使用ls的替代方法:

(ls x.txt && echo yes) || echo no

如果要隐藏ls任何输出,以便只看到yes或no,请将stdoutstderr重定向到/dev/null

(ls x.txt >> /dev/null 2>&1 && echo yes) || echo no
链接地址: http://www.djcxy.com/p/24129.html

上一篇: How to check if a file exists in a shell script

下一篇: shell script to remove a file if it already exist