如何确保两个目录不是彼此的子目录(BASH)
编辑:这或多或少是我在 @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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些问题:
风格
您可能应该引用 echo 的整个参数,因为
如果没有
echo
参数周围的引号,从技术上讲,您将每个单词作为其自己的参数传递:echo "ERROR:somedir " "is" "a" "subdirectory"
.... 由于echo
按给定的顺序打印其参数,并用空格分隔,因此输出与您的情况相同。但从语义上来说,这不是你想要的。(一个不同的示例:
将打印
foo bar
。)错误消息无法正常工作
如果参数不存在
显然,如果您已经检查过它们是否存在,则这是无关紧要的。
您同样需要检查极端情况,例如参数引用同一目录的情况:
不检测符号链接
不会给出错误消息。
不检测
mount --bind
不会给出任何错误消息。
Some problems:
Stylistic
You should probably quote the entire argument to echo, as
Without the quotes around the argument to
echo
, you are technically passing each word as its own parameter:echo "ERROR:somedir" "is" "a" "subdirectory"
.... Sinceecho
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:
would print
foo bar
.)Error message doesn't work properly
If the arguments don't exist
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:
Doesn't detect symbolic links
gives no error message.
Doesn't detect
mount --bind
gives no error message.