开发者

how to be sure that two directories are not subdirectories to each other (BASH)

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜