如何在bash脚本的特定目录中检查文件是否存在? [英] How to check if a files exists in a specific directory in a bash script?

查看:77
本文介绍了如何在bash脚本的特定目录中检查文件是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我一直在尝试的方法,但未成功.如果我想检查〜/.example目录中是否存在文件

This is what I have been trying and it is unsuccessful. If I wanted to check if a file exists in the ~/.example directory

FILE=$1
if [ -e $FILE ~/.example ]; then
      echo "File exists"
else
      echo "File does not exist"
fi

推荐答案

您可以使用$FILE与目录连接以创建完整路径,如下所示.

You can use $FILE to concatenate with the directory to make the full path as below.

FILE="$1"
if [ -e ~/.myexample/"$FILE" ]; then
    echo "File exists"
else
    echo "File does not exist"
fi

这篇关于如何在bash脚本的特定目录中检查文件是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆