bash 如何检查给定路径中是否存在目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12052289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a directory exists within a given path
提问by Mantas
I'm creating a simple script for backing up and I want to check if a directory exists in a given path like so:
我正在创建一个简单的备份脚本,我想检查给定路径中是否存在目录,如下所示:
fullPath=/usr/local/webapps/mydir
if mydir exists in my $fullPath
then back it up
else give error
fi
My question is how do I formulate the if statement to check if the last directory exists in the $fullPathvariable? Just to clarify, for this case it would be mydirif I used /usr/local it would be localetc.
我的问题是如何制定 if 语句来检查$fullPath变量中是否存在最后一个目录?只是为了澄清一下,对于这种情况,如果我使用 /usr/local ,它将是mydir,它将是本地等。
Thanks in advance!
提前致谢!
回答by ronalchn
Duplicate question? Check if a directory exists in a shell script
重复的问题?检查shell脚本中是否存在目录
if [ -d "$DIRECTORY" ]; then
# Control will enter here if $DIRECTORY exists.
fi
The $DIRECTORY is the pathname
$DIRECTORY 是路径名
For your case, you can just do:
对于您的情况,您可以这样做:
if [ -d "$fullPath" ]; then
then back it up
else give error
fi