2

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 ...)

clt60
  • 62,119
  • 17
  • 107
  • 194

1 Answers1

3

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
clt60
  • 62,119
  • 17
  • 107
  • 194
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Thank you! Nice solution and portable too. – clt60 Apr 23 '13 at 22:08
  • Hehe, nice edit :) Would upvote it if I could ;). Note that I first tried something like `df ... | tail -n 1` and was astonished that this didn't worked. However the `sed '1d'` should work well although I liked the `awk` solution more as it gives you the sixth column (mounted) without additional parsing effort – hek2mgl Apr 23 '13 at 22:09
  • Yeah - and the read has usual problems when the filename contains spaces, so back to the awk :) – clt60 Apr 23 '13 at 22:41