You are not logged in.

#1 2012-12-22 18:34:43

Thme
Member
From: Raleigh NC
Registered: 2012-01-22
Posts: 105

[Solved] Parsing findmnt output?... Is it reliable/portable in bash.

Solution/better suggestion in second post...

There seems to be little about using anything other than the output of mount or fstab when dealing with filesystems in scripting. Most of what I've read on it uses awk or cut for parsing "mount" but what about findmnt when needing  some of the same info. particularly "findmnt -rn" which prints the same raw information as mount but space separated and without the words like "type" and "on". I'm not entrirely concerned with posix compliance as most of it amount to makings it compatible with much much older systems. Personally I think bash has been around long enough to be portable in and of itself in most circumstances but that's just my opinion. Is "findmnt" as portable as "mount" for scripting? that is really my question.
An example would be say I wanted only the "target" and "source" information stored with process substitution in bash.
with mount I could use

#mount | cut -d ' ' -f 1,3 
to get that info for all mounted filesystems
OR
#mount | grep sdb1 | cut -d ' ' -f 3
to get where a particular partition (/dev/sdb1) is mounted.

with findmnt it would be just as simple but take the words "on" and "type" out of the parsing.

#findmnt -rn | cut -d ' ' -f 1,2
or for the second example using mount... 
#findmnt -rn | grep sdb1 | cut -d ' ' -f 1

if neither of these are the best for getting simple information then I can accept other suggestions as well.
Think of the pipe to grep section having a variable with the needed string instead of "sdb1" for instance...
maybe this is just a minor formatting discrepancy. If so feel free to point it out... I'm kinda on my journey of do's and don't's in shell scripting with only google and the man pages as my resources for learning.

Last edited by Thme (2012-12-22 19:55:58)


"Hidden are the ways for those who pass by, for light is perished and darkness comes into being." Nephthys:
Ancient Egyptian Coffin Texts

Offline

#2 2012-12-22 19:03:33

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [Solved] Parsing findmnt output?... Is it reliable/portable in bash.

Thme wrote:

with findmnt it would be just as simple but take the words "on" and "type" out of the parsing.

#findmnt -rn | cut -d ' ' -f 1,2
or for the second example using mount... 
#findmnt -rn | grep sdb1 | cut -d ' ' -f 1

This is an entire misuse of findmnt. If you're going to talk about "portable" or "POSIX", then you can't even use mount.

If you want specific columns from findmnt, then ask for them. Specifically, the -o option is your friend, i.e.

$ findmnt -runo source,target /dev/sdb1
/dev/sdb1 /home

If you're looking for some absurd level of portability where you might encounter systems that don't have findmnt, then you'll need to build a parser for /etc/mtab, and even that isn't portable.

Last edited by falconindy (2012-12-22 19:04:19)

Offline

#3 2012-12-22 19:54:43

Thme
Member
From: Raleigh NC
Registered: 2012-01-22
Posts: 105

Re: [Solved] Parsing findmnt output?... Is it reliable/portable in bash.

falconindy wrote:
Thme wrote:

with findmnt it would be just as simple but take the words "on" and "type" out of the parsing.

#findmnt -rn | cut -d ' ' -f 1,2
or for the second example using mount... 
#findmnt -rn | grep sdb1 | cut -d ' ' -f 1

This is an entire misuse of findmnt. If you're going to talk about "portable" or "POSIX", then you can't even use mount.

If you want specific columns from findmnt, then ask for them. Specifically, the -o option is your friend, i.e.

$ findmnt -runo source,target /dev/sdb1
/dev/sdb1 /home

If you're looking for some absurd level of portability where you might encounter systems that don't have findmnt, then you'll need to build a parser for /etc/mtab, and even that isn't portable.

That's clear enough to me. And no I wasn't too interested in POSIX compliance. It seems like overkill in most circumstances. As for the findmnt options.. I should have read the man page a little more... totally skimmed over it... I'll mark this as solved...


"Hidden are the ways for those who pass by, for light is perished and darkness comes into being." Nephthys:
Ancient Egyptian Coffin Texts

Offline

Board footer

Powered by FluxBB