简体   繁体   English

Bash - 检查我是否对任何目录路径的父级有写权限

[英]Bash - check if I have write permissions to parent of the any directory path

In my bash script I have to create bunch of directories (NOT files) - /opt/some/path1/ and /opt/some/path2/ and so on.在我的 bash 脚本中,我必须创建一堆目录(不是文件) - /opt/some/path1//opt/some/path2/等等。

Please note: In my case, /opt/some/ , the parent already exists, guaranteed.请注意:在我的情况下, /opt/some/ ,父母已经存在,保证。 No validation required here.这里不需要验证。

How can I check if have write permissions to parent for my every mkdir /opt/some/path1/ and mkdir /opt/some/path2/ ...?如何检查是否对我的每个mkdir /opt/some/path1/mkdir /opt/some/path2/ ...具有写入权限?

I learned that we can use -w for files so I tried this:我了解到我们可以将-w用于文件,所以我尝试了这个:

if [ -w /opt/some/path1/ ]; then echo "You have directory creation permissions"; else echo "You are having a rough day.!"; fi

Which "obviously fails" miserably because the directory I am going to create (the path), does not exist yet.哪个“显然失败”很惨,因为我要创建的目录(路径)还不存在。

Is there a way we can use something similar to -w for files for directories?有没有一种方法可以将similar to -w for files Or what I am missing here?或者我在这里缺少什么?

$ mkdir d1
$ [[ -w d1 ]] && echo Y
Y
$ mkdir d1/d11
$ chmod a-w d1
$ [[ ! -w d1 ]] && echo N
N
$ mkdir d1/d12
mkdir: d1/d12: Permission denied

you can check on a dir, which after all is a file你可以检查一个目录,它毕竟是一个文件

If I understand the question correctly, you want to check whether you have write access to the directory you want to create a subdirectory of, ie before creating /opt/some/path1/ you'd check write access to /opt/some/ .如果我正确理解了这个问题,您想检查您是否对要创建子目录的目录具有写访问权限,即在创建/opt/some/path1/之前,您将检查对/opt/some/的写访问权限。 You can do that by using the dirname command to get the parent directory's path, then using the -w test on that .您可以通过使用dirname命令获取父目录的路径,然后使用-w测试来做到这一点 Note that mkdir still might fail for reasons other than permissions on the parent directory, so you still need to check it for errors.请注意, mkdir仍然可能由于父目录权限以外的原因而失败,因此您仍然需要检查它是否有错误。 Something like this:像这样的东西:

dirToCreate=/opt/some/path1/
if [ ! -w "$(dirname "$dirToCreate")" ]; then
    echo "Insufficient permissions to create $dirToCreate" >&2
else
    mkdir "$dirToCreate" || {
        echo "Error creating $dirToCreate (due to something other than permissions)" >&2
    }
fi

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查父目录上的写权限 - Checking write permissions on parent directory bash:按名称获取父目录的路径 - bash: get path to parent directory by name 将目录更改为bash中父/调用脚本的路径 - Change directory to path of parent/calling script in bash 如何使用BASH设置循环以查找具有写权限的目录? - How to set up a loop with BASH to find a directory with write permissions? 从 bash 中的文件系统路径获取父目录路径 - Take parent directory path from a filesystem path in bash 如何在我的机器上没有重复的自定义 bash 脚本目录并将其添加为使用 PATH 获取的链接? - How do I not have a duplicated custom bash script directory on my machine and add it as a link that is picked up with PATH? Bash脚本:目前,我在一个目录中应该有500个文件,如果缺少任何文件,如何停止bash脚本? - Bash Scripting: I currently am supposed to have 500 files inside a directory, how can I stop a bash script if any files are missing? 如何在bash的文件路径中获得最外层的父目录? - How do you get the outermost parent directory in a file path in bash? 如何编写 bash 脚本来比较具有相同格式的 any.txt 文件中的 output 值? - How do I write a bash script to compare and output values in any .txt files that have the same format? 将目录中所有文件的名称及其绝对路径写入bash中的csv文件 - write the name of all files in a directory, and their absolute path, to a csv file in bash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM