Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何检查rmdir是否返回EEXIST或enotery?_Linux_Bash_Return Value_Errno_Rmdir - Fatal编程技术网

Linux 如何检查rmdir是否返回EEXIST或enotery?

Linux 如何检查rmdir是否返回EEXIST或enotery?,linux,bash,return-value,errno,rmdir,Linux,Bash,Return Value,Errno,Rmdir,我正在尝试编写一个bash脚本,它请求一个目录,然后在确认后删除该目录。我还需要它告诉用户目录是否为空,并询问他们是否仍要删除它 我想我会使用rmdir并检查返回值,以确保目录被删除,如果没有,原因是什么,但到目前为止,我不知道什么返回值等同于EEXIST或enotery。到目前为止,我得到的唯一错误值是1 如果目录中有文件,返回值应该是多少?分别检查。不是完美的,而是一个开始 if [ ! -e "$DIR" ] then echo "ERROR: $DIR does not exis

我正在尝试编写一个bash脚本,它请求一个目录,然后在确认后删除该目录。我还需要它告诉用户目录是否为空,并询问他们是否仍要删除它

我想我会使用rmdir并检查返回值,以确保目录被删除,如果没有,原因是什么,但到目前为止,我不知道什么返回值等同于EEXIST或enotery。到目前为止,我得到的唯一错误值是1


如果目录中有文件,返回值应该是多少?

分别检查。不是完美的,而是一个开始

if [ ! -e "$DIR" ]
then
    echo "ERROR: $DIR does not exist" >&2
elif [ ! -d "$DIR" ]
then
    echo "ERROR: $DIR is not a directory" >&2
elif [ ! -r "$DIR" ]
then
    echo "ERROR: $DIR cannot be read" >&2
elif [ $(ls -a $DIR | wc -l) -gt 2 ]
then
    echo "ERROR: $DIR is not  empty" >&2
else
    rmdir $DIR
fi

注意:
rmdir
仍有可能失败。我想到的一个问题是,您没有对
$DIR

的父目录的写入权限。您可以尝试使用以下代码:

#!/bin/bash

check_path() {
        if [ "x$1" = "x" ]
        then
                echo "ERROR: You have to specify a valid path."
                exit 1
        fi

        if ! [ -d "$1" ]
        then
                echo "ERROR: The specified path does not exists or it's not a directory"
                exit 1
        fi

        X="`find \"$1\"  -maxdepth 1 | tail -n 2 | wc -l`"
        if [ $X -gt 1 ]
        then
                X="R"
        else
                X=""
        fi

        while [[ "x$X" != "x" && ("x$X" != "xs" && "x$X" != "xn") ]]
        do
                echo "The specified path ($1) is not empty. Are you sure you want to delete it anyway? (S/n)"
                stty -echo
                read X
                stty echo
        done
        if [ "x$X" == "xn" ]
        then
                echo "Operation interrupted by the user."
                exit 0
        fi
}

echo -n "Please insert the path to delete: "
stty -echo
read DIRNAME
stty echo
echo

check_path "$DIRNAME"

echo "Removing path $1"
echo rm -fr "$DIRNAME"

HTH

向我们展示您迄今为止所做的尝试。2提示:您知道
echo$?
将向您显示上一个命令的返回值吗?您知道返回的“0”表示“true”吗?祝你好运。你必须更改“echo rm-fr”行,删除“echo”