Bash:检查是否存在相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32857392/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:39:49  来源:igfitidea点击:

Bash: check if a relative path exists

bashshell

提问by kcstricks

I am writing a shell script that takes in a directory as its only argument. I need to check if the directory exists before I do anything else. The directory can be absolute or relative. I know I can check for the existence of an absolute directory (say, /Users/keith/Documents/TestFolder) using

我正在编写一个将目录作为唯一参数的 shell 脚本。在我做任何其他事情之前,我需要检查该目录是否存在。目录可以是绝对的,也可以是相对的。我知道我可以使用

if [ -d ""]; then
    # do something if the absolute directory exists
fi

However, if the working directory is /Users/keith/Documents/ and I call my script and pass just "TestFolder", then the test in this if statement will evaluate to false, even though TestFolder exists within the current working directory.

但是,如果工作目录是 /Users/keith/Documents/ 并且我调用我的脚本并只传递“TestFolder”,那么这个 if 语句中的测试将评估为 false,即使 TestFolder 存在于当前工作目录中。

I have tried converting the directory to an absolute directory using

我曾尝试使用将目录转换为绝对目录

abs_path=`cd ""; pwd`

and then testing for the existence of the absolute path using the if statement above. This works fine (recognizes that "TestFolder" exists even if that's all that I pass as the argument) provided that the directory passed to the script does indeed exist, but if it doesn't then this fails.

然后使用上面的 if 语句测试绝对路径的存在。如果传递给脚本的目录确实存在,则可以正常工作(即使我将其作为参数传递,也识别出“TestFolder”存在),但如果不存在,则此操作将失败。

What I need is a way to determine whether the directory passed as an argument exists, regardless of whether it is passed as an absolute directory or one that is relative to the user's current working directory, and it cannot fail if the directory doesn't exist.

我需要的是一种确定作为参数传递的目录是否存在的方法,无论它是作为绝对目录传递还是相对于用户当前工作目录的目录传递,并且如果目录不存在则不会失败.

I am clearly no bash expert - any advice will be greatly appreciated!

我显然不是 bash 专家 - 任何建议将不胜感激!

回答by rici

However, if the working directory is /Users/keith/Documents/ and I call my script and pass just "TestFolder", then the test in this if statement will evaluate to false, even though TestFolder exists within the current working directory.

但是,如果工作目录是 /Users/keith/Documents/ 并且我调用我的脚本并只传递“TestFolder”,那么这个 if 语句中的测试将评估为 false,即使 TestFolder 存在于当前工作目录中。

That's not correct. If TestFolderexists in the current working directory, [ -d TestFolder ]will succeed. So there is no problem here.

那不正确。如果TestFolder存在于当前工作目录中,[ -d TestFolder ]则成功。所以这里没有问题。

By the way, if you want to canonicalize a filepath and you have Gnu coreutils (which you probably do if you have a Linux system), then you can use:

顺便说一句,如果您想规范化文件路径并且您有 Gnu coreutils(如果您有 Linux 系统,您可能会这样做),那么您可以使用:

readlink -f "$path"

or

或者

readlink -e "$path"

The difference is that -fwill succeed even if the last component in the path doesn't exist (so it will succeed if the path is a plausible name for a file to be created), while -ewill only succeed if the path resolves to an existing file or directory. See man readlinkfor more details.

区别在于-f即使路径中的最后一个组件不存在也会成功(因此如果路径是要创建的文件的合理名称,-e它将成功),而只有在路径解析为现有文件时才会成功或目录。有关man readlink更多详细信息,请参阅。

回答by User51

A simple solution would be to...

一个简单的解决方案是...

  1. turn off fail on error
  2. Try to CD and pwd (which may or may not fail)
  3. Turn on fail on error (if desired)
  1. 关闭失败错误
  2. 尝试 CD 和密码(可能会也可能不会失败)
  3. 打开失败错误(如果需要)
set +e
TESTPATH="$( cd ~/workspace/my-super-folder && pwd )"
set -e

Once you have the variable TESTPATH, you can test that. If the path is in the variable, the directory exists. If the path is empty, it doesn't.

一旦有了变量 TESTPATH,就可以对其进行测试。如果路径在变量中,则目录存在。如果路径为空,则不会。

if [[ "${TESTPATH}" == "" ]]; then
  echo -e "FOLDER DOES NOT EXIST."
else
  echo -e "FOLDER EXISTS."
fi