bash Shell Script 检查文件是否存在,并具有读取权限

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

Shell Script check if file exists, and has read permissions

bashshell

提问by Asaf Nevo

In my shell script i'm trying to check if a specific file is exists and if it has reading permissions.

在我的 shell 脚本中,我试图检查特定文件是否存在以及它是否具有读取权限。

My file's path has spaces in it.

我的文件路径中有空格。

I quoted the file path:

我引用了文件路径:

file='/my/path/with\ some\ \spaces/file.txt'

This is the function to check if the file exists:

这是检查文件是否存在的函数:

#Check if file exists and is readable
checkIfFileExists() {
    #Check if file exists
    if ! [ -e  ]; then
        error " does not exists";
    fi

    #Check if file permissions allow reading
    if ! [ -r  ]; then
        error " does not allow reading, please set the file permissions";
    fi
}

Here I double quote to make sure it gets the file as a one argument:

在这里我用双引号来确保它将文件作为一个参数:

checkIfFileExists "'$file'";

And I receive an error from the bash saying:

我从 bash 收到一条错误消息:

[: too many arguments

Which make me thinks it doesn't get it as a one argument.

这让我认为它不能作为一个论点。

But in my custom error, I do get the whole path, and it says it doesn't exists.

但是在我的自定义错误中,我确实得到了整个路径,并且它说它不存在。

Error: '/my/path/with\ some\ \spaces/file.txt' does not exists

Although it does exists, and when I tried to read it with "cat $file" I get a permission error..

虽然它确实存在,但当我尝试用“cat $file”读取它时,我收到了一个权限错误..

what am I'm doing wrong?

我做错了什么?

采纳答案by tripleee

The proper way to quote when you require variable interpolation is with double quotes:

当您需要变量插值时,正确的引用方法是使用双引号:

if [ -e "" ]; then

You need similar quoting throughout the script, and the caller needs to quote or escape the string -- but not both. When you assign it, use one of these:

您需要在整个脚本中使用类似的引用,并且调用者需要引用或转义字符串——但不能同时引用或转义字符串。当您分配它时,请使用以下之一:

file='/my/path/with some spaces/file.txt'
# or
file=/my/path/with\ some\ spaces/file.txt
# or
file="/my/path/with some spaces/file.txt"

then use double quotes around the value to pass it in as a single argument:

然后在值周围使用双引号将其作为单个参数传入:

checkIfFileExists "$file"

Again, where you need the variable's value to be interpolated, use double quotes.

同样,在需要插入变量值的地方,请使用双引号。

For a quick illustration of what these quotes do, try this:

要快速说明这些报价的作用,请尝试以下操作:

vnix$ printf '<<%s>>\n' "foo bar" "'baz quux'" '"ick poo"' \"ick poo\" ick\ poo
<<foo bar>>
<<'baz quux'>>
<<"ick poo">>
<<"ick>>
<<poo">>
<<ick poo>>

Furthermore, see also When to wrap quotes around a shell variable?

此外,另请参阅何时将引号括在 shell 变量周围?

回答by Vishnu

if [[ -e  ]];then
 echo it exists
else
 echo it doesnt
fi

if [[ -r  ]];then
  echo readable
else
  echo not readable
fi