如何确保两个目录不是彼此的子目录(BASH)

发布于 2024-11-19 19:28:45 字数 1822 浏览 1 评论 0原文

编辑:这或多或少是我在 @Mechanical 的精彩输入后想到的。有什么见解吗?

#!/bin/bash

path1="$(readlink -e "$1")"
path2="$(readlink -e "$2")"

EBADARGS=65

function checkArgsNumber()
{
    if test "$#" -ne 2; then
        echo "ERRORE: this script takes exactly 2 params."  
        exit $EBADARGS
    fi
}

function checkExistence()
{
    if [ ! -d $path1 ]; then
        echo  "ERROR: "$1" does not exist"
        exit $EBADARGS
    elif [ ! -d "$2" ]; then
        echo  "ERROR: "$2" does not exist"
        exit $EBADARGS
    elif [[ -L $path1 ]]; then
        echo "ERROR: path1 can't be a symbolic link"
        exit $EBADARGS
    elif [[ -L $2 ]]; then
        echo "ERROR: path2 can't be a symbolic  link"
        exit $EBADARGS
    fi
}

function checkIfSame()
{
    if [[ $path1 == $path2 ]]; then
        echo "ERROR: path1 and path2 must be different directories"
        exit $EBADARGS     
    fi   
}

function checkIfSubdirectories()
{
    if [[ $path1 = *$path2* ]]; then
        echo "ERROR:"$1" is a $path2 subdirectory"
        exit $EBADARGS
    elif [[ $path2 = *$path1* ]]; then
        echo "ERROR:"$2" is a  $path1 subdirectory"
        exit $EBADARGS
    elif [[ -e "$(find $path1 -samefile $path2)" ]]; then
        echo "ERROR:"$(find $path1 -samefile $path2 -print0)" and "$2" have the same inode, $path2 is a  $path1 subdirectory"
        exit $EBADARGS
    elif [[ -e "$(find $path2 -samefile $path1)" ]]; then
        echo "ERROR:"$(find $path2 -samefile $path1 -print0)" and "$2" have the same inode,  $path1 is a  $path2 subdirectory"
        exit $EBADARGS
    fi
}

checkArgsNumber "$@"
checkExistence "$@"
checkIfSame "$@"
checkIfSubdirectories "$@"

现在..这应该有效,我希望它在某种程度上有用。 有人可以解释一下 *$path2* 部分是如何工作的吗?这个 * * 运算符的名称是什么?我应该去哪里阅读相关内容?

EDITED: this is more or less what I came up after @Mechanical's nice input. Any insight?

#!/bin/bash

path1="$(readlink -e "$1")"
path2="$(readlink -e "$2")"

EBADARGS=65

function checkArgsNumber()
{
    if test "$#" -ne 2; then
        echo "ERRORE: this script takes exactly 2 params."  
        exit $EBADARGS
    fi
}

function checkExistence()
{
    if [ ! -d $path1 ]; then
        echo  "ERROR: "$1" does not exist"
        exit $EBADARGS
    elif [ ! -d "$2" ]; then
        echo  "ERROR: "$2" does not exist"
        exit $EBADARGS
    elif [[ -L $path1 ]]; then
        echo "ERROR: path1 can't be a symbolic link"
        exit $EBADARGS
    elif [[ -L $2 ]]; then
        echo "ERROR: path2 can't be a symbolic  link"
        exit $EBADARGS
    fi
}

function checkIfSame()
{
    if [[ $path1 == $path2 ]]; then
        echo "ERROR: path1 and path2 must be different directories"
        exit $EBADARGS     
    fi   
}

function checkIfSubdirectories()
{
    if [[ $path1 = *$path2* ]]; then
        echo "ERROR:"$1" is a $path2 subdirectory"
        exit $EBADARGS
    elif [[ $path2 = *$path1* ]]; then
        echo "ERROR:"$2" is a  $path1 subdirectory"
        exit $EBADARGS
    elif [[ -e "$(find $path1 -samefile $path2)" ]]; then
        echo "ERROR:"$(find $path1 -samefile $path2 -print0)" and "$2" have the same inode, $path2 is a  $path1 subdirectory"
        exit $EBADARGS
    elif [[ -e "$(find $path2 -samefile $path1)" ]]; then
        echo "ERROR:"$(find $path2 -samefile $path1 -print0)" and "$2" have the same inode,  $path1 is a  $path2 subdirectory"
        exit $EBADARGS
    fi
}

checkArgsNumber "$@"
checkExistence "$@"
checkIfSame "$@"
checkIfSubdirectories "$@"

now.. this should work and I hope it is useful somehow.
Could someone explain me how the *$path2* part works? What is the name of this * * operator? Where should I go read about it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱影子 2024-11-26 19:28:46

一些问题:

风格

您可能应该引用 echo 的整个参数,因为

    echo "ERROR: $1 is a subdirectory of $(readlink -e "$2")"

如果没有 echo 参数周围的引号,从技术上讲,您将每个单词作为其自己的参数传递: echo "ERROR:somedir " "is" "a" "subdirectory".... 由于 echo 按给定的顺序打印其参数,并用空格分隔,因此输出与您的情况相同。但从语义上来说,这不是你想要的。

(一个不同的示例:

echo foo          bar

将打印 foo bar。)

错误消息无法正常工作

如果参数不存在

$ ./check.sh nonexistent1 nonexistent2
ERROR:nonexistent1 is a subdirectory of 

显然,如果您已经检查过它们是否存在,则这是无关紧要的。

您同样需要检查极端情况,例如参数引用同一目录的情况:

$ mkdir a b
$ ln -s ../a b/c
$ ./check.sh a b/c
ERROR:a is a subdirectory of /dev/shm/a

不检测符号链接

$ mkdir a b
$ ln -s ../a b/c
$ ./check.sh a b

不会给出错误消息。

不检测 mount --bind

$ mkdir a b b/c
$ sudo mount --bind a b/c
$ ./check.sh a b

不会给出任何错误消息。

Some problems:

Stylistic

You should probably quote the entire argument to echo, as

    echo "ERROR: $1 is a subdirectory of $(readlink -e "$2")"

Without the quotes around the argument to echo, you are technically passing each word as its own parameter: echo "ERROR:somedir" "is" "a" "subdirectory".... Since echo prints its parameters in the order given, separated by spaces, the output is the same in your case. But semantically it's not what you want.

(An example where it would be different:

echo foo          bar

would print foo bar.)

Error message doesn't work properly

If the arguments don't exist

$ ./check.sh nonexistent1 nonexistent2
ERROR:nonexistent1 is a subdirectory of 

Obviously, this is irrelevant if you've already checked they exist.

You similarly need to check for corner cases such as where the parameters refer to the same directory:

$ mkdir a b
$ ln -s ../a b/c
$ ./check.sh a b/c
ERROR:a is a subdirectory of /dev/shm/a

Doesn't detect symbolic links

$ mkdir a b
$ ln -s ../a b/c
$ ./check.sh a b

gives no error message.

Doesn't detect mount --bind

$ mkdir a b b/c
$ sudo mount --bind a b/c
$ ./check.sh a b

gives no error message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文