bash 给出路径的挂载点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2167558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:53:49  来源:igfitidea点击:

Give the mount point of a path

linuxbashshellpathmount

提问by Charles Stewart

The following, very non-robust shell code will give the mount point of $path:

以下非常不健壮的 shell 代码将给出挂载点$path

 (for i in $(df|cut -c 63-99); do case $path in $i*) echo $i;; esac; done) | tail -n 1

Is there a better way to do this in shell?

有没有更好的方法在 shell 中做到这一点?

Postscript

后记

This script is really awful, but has the redeeming quality that it Works On My Systems. Note that several mount points may be prefixes of $path.

这个脚本真的很糟糕,但它具有在我的系统上工作的可取之处。请注意,多个挂载点可能是$path.

ExamplesOn a Linux system:

示例在 Linux 系统上:

cas@txtproof:~$ path=/sys/block/hda1
cas@txtproof:~$ for i in $(df -a|cut -c 57-99); do case $path in $i*) echo $i;; esac; done| tail -1
/sys

On a Mac OSX system

在 Mac OSX 系统上

cas local$ path=/dev/fd/0
cas local$ for i in $(df -a|cut -c 63-99); do case $path in $i*) echo $i;; esac; done| tail -1
/dev

Note the need to vary cut's parameters, because of the way df's output differs; using awk solves this, but even awk is non-portable, given the range of result formatting various implementations of df return.

注意需要改变 cut 的参数,因为 df 的输出方式不同;使用 awk 解决了这个问题,但即使 awk 也是不可移植的,因为结果格式的范围是 df return 的各种实现。

AnswerIt looks like munging tabular output is the only way within the shell, but

回答看起来 munging tabular output 是 shell 中的唯一方法,但是

df -P "$path"  | tail -1 | awk '{ print $NF}'

based on ghostdog74's answer, is a big improvement on what I had. Note two new issues: firstly, df $pathinsists that $pathnames an existing file, the script I had above doesn't care; secondly, there are no worries about dereferencing symlinks. This doesn't work if you have mount points with spaces in them, which occurs if one has removable media with spaces in their volume names.

基于 ghostdog74 的回答,对我所拥有的来说是一个很大的改进。注意两个新问题:首先,df $path坚持$path为现有文件命名,我上面的脚本并不关心;其次,不必担心取消引用符号链接。如果您的挂载点中有空格,这将不起作用,如果一个可移动媒体的卷名中有空格,就会发生这种情况。

It's not difficult to write Python code to do the job properly.

编写 Python 代码来正确完成这项工作并不困难。

回答by JesperE

dftakes the path as parameter, so something like this should be fairly robust;

df将路径作为参数,所以像这样的东西应该相当健壮;

df "$path" | tail -1 | awk '{ print  }'

回答by Douglas Leeder

In theory statwill tell you the device the file is on, and there should be some way of mapping the device to a mount point.

理论上stat会告诉您文件所在的设备,并且应该有某种方式将设备映射到安装点。

For example, on linux, this should work:

例如,在 linux 上,这应该有效:

stat -c '%m' $path

回答by Graeme

Always been a fan of using formatting options of a program, as it can be more robust than manipulating output (eg if the mount point has spaces). GNU dfallows the following:

一直喜欢使用程序的格式化选项,因为它比操作输出更健壮(例如,如果挂载点有空格)。GNUdf允许以下内容:

df --output=target "$path" | tail -1

Unfortunately there is no option I can see to prevent the printing of a header, so the tail is still required.

不幸的是,我无法看到阻止打印标题的选项,因此仍然需要尾部。

回答by ghostdog74

i don't know what your desired output is, therefore this is a guess

我不知道你想要的输出是什么,因此这是一个猜测

#!/bin/bash

path=/home
df | awk -v path="$path" 'NR>1 && $NF~path{
 print $NF
}'

Using cut with -c is not really reliable, since the output of df will be different , say a 5% can change to 10% and you will miss some characters. Since the mount point is always at the back, you can use fields and field delimiters. In the above, $NF is the last column which is the mount point.

使用带有 -c 的 cut 并不是很可靠,因为 df 的输出会有所不同,比如 5% 可以更改为 10% 并且您会错过一些字符。由于挂载点总是在后面,您可以使用字段和字段分隔符。在上面, $NF 是最后一列,它是挂载点。

回答by ndim

I would take the source code to df and find out what it does besides calling statas Douglas Leeder suggests.

我会将源代码带到 df 并找出它除了stat像 Douglas Leeder 建议的那样调用之外还做了什么。

Line-by-line parsing of the dfoutput will cause problems as those lines often look like

逐行解析df输出会导致问题,因为这些行通常看起来像

/dev/mapper/VOLGROUP00-logical--volume
                      1234567  1000000  200000  90% /path/to/mountpoint

With the added complexity of parsing those kinds of lines as well, probably calling statand finding the mountpoint is less complex.

由于解析这些类型的行也增加了复杂性,因此调用stat和查找挂载点可能不那么复杂。

回答by EOhm

Just had the same problem. If some mount point (or the mounted device) is sufficent as in my case You can do:

刚刚有同样的问题。如果某些挂载点(或已挂载的设备)在我的情况下就足够了,您可以执行以下操作:

DEVNO=$(stat -c '%d' /srv/sftp/testconsumer)
MP=$(findmnt -n -f -o TARGET /dev/block/$((DEVNO/2**8)):$((DEVNO&2**8-1)))

(or split the hex DEVNO %Dwith /dev/block/$((0x${DEVNO:0:${#DEVNO}-2})):$((0x${DEVNO:2:2})))

(或将十六进制 DEVNO%D与分开/dev/block/$((0x${DEVNO:0:${#DEVNO}-2})):$((0x${DEVNO:2:2}))

Alternatively the following loop come in to my mind, out of ideas why I cannot find proper basic command..

或者,以下循环出现在我的脑海中,出于为什么我找不到正确的基本命令的想法..

TARGETPATH="/srv/sftp/testconsumer"
TARGETPATHTMP=$(readlink -m "$TARGETPATH")
[[ ! -d "$TARGETPATHTMP" ]] && TARGETPATHTMP=$(dirname "$TARGETPATH")
TARGETMOUNT=$(findmnt -d backward -f -n -o TARGET --target "$TARGETPATHTMP")
while [[ -z "$TARGETMOUNT" ]]
do
  TARGETPATHTMP=$(dirname "$TARGETPATHTMP")
  echo "$TARGETPATHTMP"
  TARGETMOUNT=$(findmnt -d backward -f -n -o TARGET --target "$TARGETPATHTMP")
done

This should work always but is much more then I expect for such simple task?

这应该总是有效,但比我对这种简单任务的期望要多得多?

(Edited to use readlink -fto allow for non existing files, -m or -e for readlink could be used instead if more components might not exists or all components must exists.)

(编辑以用于readlink -f允许不存在的文件,如果可能不存在更多组件或所有组件必须存在,则可以使用 -m 或 -e 来代替 readlink。)

回答by Paused until further notice.

mount | grep "^$path" | awk '{print }'

回答by Charles Stewart

I missed this when I looked over prior questions: Python: Get Mount Point on Windows or Linux, which says that os.path.ismount(path)tells if path is a mount point.

当我查看之前的问题时,我错过了这一点:Python: Get Mount Point on Windows or Linux,它os.path.ismount(path)说明路径是否是一个安装点。

My preference is for a shell solution, but this looks pretty simple.

我更喜欢 shell 解决方案,但这看起来很简单。

回答by Erik Aronesty

I use this:

我用这个:

df -h $path | cut -f 1 -d " " | tail -1

回答by BrainwreckedTech

Linux has this, which will avoid problem with spaces:

Linux 有这个,这将避免空格问题:

lsblk -no MOUNTPOINT ${device}

Not sure about BSD land.

不确定BSD土地。