How to extract NFS information from mount on Linux and Solaris?

不想你离开。 提交于 2019-12-10 19:27:44

问题


I need to extract NFS mount information using mount on Linux (RHEL 4/5) and Solaris (Solaris 10) systems. As this is part of an SSH command, the extraction needs to happen in one line. Unfortunately, Linux and Solaris display the mountpoint at different parts of the line:

Linux:

10.0.0.1:/remote/export on /local/mountpoint otherstuff

Solaris:

/local/mountpoint on 10.0.0.1:/remote/export otherstuff

I would like to get the following space separated output

10.0.0.1 /remote/export /local/mountpoint

I managed to do it separately with sed (Solaris 10 sed), but I need one command returing the same output for both.

Linux sed:

sed 's/\([^:]*\):\([^ ]*\)[^\/]*\([^ ]*\) .*/\1 \2 \3/'

Solaris sed:

sed 's/\([^ ]*\) *on *\([^:]*\):\([^ ]*\) .*/\2 \3 \1/'

Solution:

I adapted the accepted answer to also work with DNS names and not only IPs:

awk -F'[: ]' '{if(/^\//)print $3,$4,$1;else print $1,$2,$4}'

回答1:


awk could help you:

 awk -F'[: ]' '{if(/^[0-9]/)print $1,$2,$4;else print $3,$4,$1}' 

see this test:

kent$  cat f
10.0.0.1:/remote/export on /local/mountpoint otherstuff
/local/mountpoint on 10.0.0.1:/remote/export otherstuff

kent$  awk -F'[: ]' '{if(/^[0-9]/)print $1,$2,$4;else print $3,$4,$1}' f
10.0.0.1 /remote/export /local/mountpoint
10.0.0.1 /remote/export /local/mountpoint



回答2:


A some shoter version of Kents solution

awk -F'[: ]' '{print /^[0-9]/?$1" "$2" "$4:$3" "$4" "$1}' file
10.0.0.1 /remote/export /local/mountpoint
10.0.0.1 /remote/export /local/mountpoint


来源:https://stackoverflow.com/questions/20974140/how-to-extract-nfs-information-from-mount-on-linux-and-solaris

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!