Question

Want make a script (called getmpoint) what will return the mountpoint from ANY filename.

The 1st idea, like: parsing output form the df or parsing fstab is not as easy as seems, because for example:

getmpoint ../../../some/path/tmp/somefile
getmpoint /tmp/somesymlink   #and want get the mountpoint where the real file is
getmpoint /

I have some idea by using stat (getting the device) - but i'm lost. Need some pointers how to solve this.

Another question is than the stat command is different on Freebsd-stat and Linux-stat. Is here any portable way?

Similarly, what about:

getmpoint /some/real/path/up/to/here/but/nonexistent_file

would be nice to get a mountpoint only from the path - without the file existence - so without stat.

Any advices? (I'm probably able make a script myself - but need some guide how to do ...)

Était-ce utile?

La solution

Try this:

getmpoint.sh, expects the filenames as param

#!/bin/bash

for path
do
    orig=$path

    #find the existing path component
    while [ ! -e "$path" ]
    do
        path=$(dirname "$path")
    done

    #get a real file from a symlink
    [ -L "$path" ] && path=$(readlink "$path")

    # use "portable" (df -P)  - to get all informatons
    # 512-blocks      Used Available Capacity  Mounted on
    read s512 used avail capa mounted <<< $(df -P "$path" | awk '{if(NR==2){ print $2, $3, $4, $5, $6}}')

    echo "Filename: $orig"
    echo "Mounted: $mounted"
    echo "Available blocks: $avail"
done
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top