Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.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 如何在ShellScript中查找目录是否为装载_Linux_Unix - Fatal编程技术网

Linux 如何在ShellScript中查找目录是否为装载

Linux 如何在ShellScript中查找目录是否为装载,linux,unix,Linux,Unix,我需要找到一个位置是否是目录,以及它是否是ShellScript中的装载 我试过这个: new_dir="/home/fruits/apple" if [ -d "$new_dir" ] then if [ mountpoint -q "$new_dir" ] then echo "directory is mounted" else echo "directory is not mounted" fi fi 但它不起作用。 给出

我需要找到一个位置是否是目录,以及它是否是ShellScript中的装载

我试过这个:

new_dir="/home/fruits/apple"
if [ -d "$new_dir" ]
then
    if [ mountpoint -q "$new_dir" ]
    then
        echo "directory is mounted"
    else
        echo "directory is not mounted"
    fi
fi
但它不起作用。 给出类似于

test_dir.sh: line 13: [: -q: binary operator expected
directory is not mounted
有人能帮忙吗。。。我需要这个工作…

关键在于说:

if [ mountpoint -q "$new_dir" ]   # WRONG
您不必使用[因为mountpoint-q$new_dir已经返回一个布尔值

if mountpoint -q "$new_dir"
从人的角度:

装入点检查目录是否为装入点

退出状态

如果目录是装入点,则为零;如果不是,则为非零

事实上,你甚至可以说:

mountpoint -q "$new_dir" && echo "mounted" || echo "not mounted"
如果确实要使用[],则需要在内部执行命令并考虑退出状态:

if [ ! $(mountpoint -q "/home") ]; then
    echo "directory is mounted"
else
    echo "directory is not mounted"
fi
注:我用[!expression]否定结果

为什么会出现预期的二进制运算符错误? 因为您在测试中编写的表达式不受支持

man测试解释了它接受的语法:

[ EXPRESSION ]
[ ]
[ OPTION
它还提到它的状态由表达式决定

当您在[]中说mountpoint时,bash假设它正在处理一个表达式。给定可能的条件列表,第一个元素不是运算符这一事实使bash希望在它后面看到一个二进制运算符,例如=,=~…因为它看到-q,所以失败并提到-q是问题。

关键在于:

if [ mountpoint -q "$new_dir" ]   # WRONG
您不必使用[因为mountpoint-q$new_dir已经返回一个布尔值

if mountpoint -q "$new_dir"
从人的角度:

装入点检查目录是否为装入点

退出状态

如果目录是装入点,则为零;如果不是,则为非零

事实上,你甚至可以说:

mountpoint -q "$new_dir" && echo "mounted" || echo "not mounted"
如果确实要使用[],则需要在内部执行命令并考虑退出状态:

if [ ! $(mountpoint -q "/home") ]; then
    echo "directory is mounted"
else
    echo "directory is not mounted"
fi
注:我用[!expression]否定结果

为什么会出现预期的二进制运算符错误? 因为您在测试中编写的表达式不受支持

man测试解释了它接受的语法:

[ EXPRESSION ]
[ ]
[ OPTION
它还提到它的状态由表达式决定

当您在[]中说mountpoint时,bash假设它正在处理一个表达式。给定可能的条件列表,第一个元素不是运算符这一事实使bash希望在它后面看到一个二进制运算符,例如=,=~…因为它看到了-q,所以失败并提到-q是问题。

@fedorqui是正确的

条件构造:

if [ mountpoint -q "$new_dir" ]
被解释为[…]内的装入点

因此-q被视为运算符,没有这样的运算符接受两个参数。

@fedorqui是正确的

条件构造:

if [ mountpoint -q "$new_dir" ]
被解释为[…]内的装入点


因此-q被视为运算符,没有这样的运算符接受两个参数。

What is-q?My[1没有这个选项有一个叫做findmnt的工具是什么-q?My[1没有这个选项有一个叫做findmnt的工具谢谢fedorqui。我真的需要检查目录部分吗?如果[-d dirname]是无用的,对吗?@user1731553否,要检查它是否是目录,您需要使用test命令。这是因为-d是test本身的一个选项-d不是命令本身。@user1731553查看我更新的答案并提供更多详细信息。谢谢fedorqui。我真的需要检查目录部分吗?如果[-d dirname]是无用的,对吗?@user1731553否,要检查它是否是一个目录,您需要使用test命令。这是因为-d是test本身的一个选项-d不是命令本身。@user1731553查看我更新的答案,了解更多详细信息。