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

标签 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

关于shell - 如何在shell脚本中检查文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40082346/

相关文章:

regex - 如何用单个空格替换换行符/换行符,但前提是它们在开始/结束正则表达式中?

linux - 为什么我的命令在 shell 脚本中不起作用,但在 bash 中起作用?

bash shell 脚本 for 循环中的两个变量

linux - 甲骨文错误 : [: into: unknown operator

linux - 将两个或多个空格更改为单个分号

linux - 如何运行到并行终端窗口? Linux

linux - 当我 sudo bash -c 时会发生什么?

shell - 在 shell 脚本中优化 grep(或使用 AWK)

linux - Bash 脚本 - 自动填写答案

linux - 为什么在存在目标的情况下,bash 中的否定文件存在检查会返回奇怪的结果?