如何从Linux和Solaris上的挂载中提取NFS信息?

3

我需要在Linux(RHEL 4/5)和Solaris(Solaris 10)系统上使用mount提取NFS挂载信息。由于这是SSH命令的一部分,所以需要在一行中进行提取。不幸的是,Linux和Solaris将挂载点显示在不同的行的不同位置:

Linux:

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

太阳OS:

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

我希望获得以下以空格分隔的输出。
10.0.0.1 /remote/export /local/mountpoint

我已经成功使用Solaris 10的sed单独完成了这个任务,但我需要一个命令来同时返回相同的输出。

Linux sed:

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

Solaris的sed命令:
sed 's/\([^ ]*\) *on *\([^:]*\):\([^ ]*\) .*/\2 \3 \1/'
解决方案: 我根据被接受的答案进行了修改,使其不仅适用于IP地址,还适用于DNS名称:
awk -F'[: ]' '{if(/^\//)print $3,$4,$1;else print $1,$2,$4}'

为什么要使用 sed 而不是(比如)awkcut - Philip Kendall
2个回答

3

awk可以帮助你:

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

看这个例子:

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

这在Solaris的awk上不起作用。我得到了“awk:语法错误,接近第1行”。我试图弄清楚在Solaris上awk应该是什么样子的。在Linux上它对我有用。 - Florian Feldhaus
1
@FlorianFeldhaus 您的 Solaris 上有 nawk 吗? - Kent
@FlorianFeldhaus 我建议你在Solaris上创建一个别名,让awk -> nawk。 - Kent
我采用了这个答案,但稍微改编了一下,使其也适用于DNS名称而不仅仅是IP地址:awk -F'[: ]' '{if(/^\//)print $3,$4,$1;else print $1,$2,$4}' - Florian Feldhaus
@FlorianFeldhaus 如果你喜欢,awk可以做很多事情,你甚至可以获取uname输出来决定你正在运行哪个内核。 - Kent

0

肯特解决方案的简短版本

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接