错误检查文件是否存在

时间:2019-07-11 19:36:43

标签: bash

我想通过输入文件名来检查文件是否存在。但是我遇到一个错误,不确定如何解决。另外,当我运行脚本并输入文件名时,我一直对不起,即使文件存在,文件$ filename也不存在。我也在第6行第6行出现错误:[[-e:未找到命令

#!/bin/bash
filename=$1

echo "Please enter the file name:"
read filename
if [[-e " $filename"]]; then
        echo " $filename exists"
else
        echo " Sorry, the file $filename does not exist"
        exit 1
fi

1 个答案:

答案 0 :(得分:1)

尝试在if语句的[[ ]]之间以及右引号之后添加一个空格,例如:

if [[ -e "$filename" ]]; then

有关更多信息,请查看this tutorial有关bash脚本的信息。

相关问题