简体   繁体   English

Bash将挂载的文件夹检测为目录

[英]Bash Detect Mounted Folder as Directory

I am creating this bashs script that has a folder path, passed as a paramter. 我正在创建具有文件夹路径的bashs脚本,将其作为参数传递。

And if it detects that paramter is a directory, then it uses it as a path for script work. 而且,如果它检测到参数是一个目录,则将其用作脚本工作的路径。

But for some reason, it denies treating this folder path as a directory. 但是由于某种原因,它拒绝将此文件夹路径视为目录。

Which has befuddled me to no end. 这使我困惑不已。

This is a mounted external 3tb hard drive, works fine, no issues, what so ever. 这是一个已安装的外部3TB硬盘驱动器,工作正常,没有问题,什么都可以。

But is mounted as /media/wdmybook/ 但安装为/ media / wdmybook /

I have this testing script to help me debug and identify the problem. 我有此测试脚本可帮助我调试和确定问题。

#!/bin/bash
DIR="/media/wdmybook/folder"

if grep -qs '$DIR' /proc/mounts; then
    echo "It's mounted."
else
echo "It's not mounted."
fi

if [ ! -d "$DIR" ]; then
    echo "Directory does not exist!"
elif [ "$DIR" != -d ]; then
    echo "Not a Directory"
else
    echo "Path is okay"
fi

But everytime I run it, it detects the path as an invalid directory, and it is not mounted. 但是每次我运行它时,它都会将该路径检测为无效目录,并且不会挂载它。

So what am I missing or not seeing? 那我想念或看不到什么呢?

Is this a permissions issue? 这是权限问题吗?

I am running this on Debian Wheezy XFCE. 我正在Debian Wheezy XFCE上运行它。

You should rewrite your if condition to something like that: 您应该将if条件重写为如下形式:

if [ ! -e "$DIR" ]; then
    echo "Directory does not exist!"
elif [ ! -d "$DIR" ]; then
    echo "Not a Directory"
else
    echo "Path is okay"
fi

For details see man test 有关详细信息,请参阅man test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM