寻找一种更有效(且可移植)的方法来获取 unix 中的(数字)文件权限
简短背景:我需要使用 Zabbix 监视 unix 文件(目录)的权限,以查看它们是否/何时更改。 Zabbix 没有像 vfs.file.mode[xxxx] 这样的内置函数,所以我必须使用数字类型来滚动我自己的 UserParameter。
到目前为止我所做的是使用 ls -l | cut -c 2-10 得到 rwxr-xr-x
部分,然后使用 sed
将字母转换为其“重量”,然后 awk
与 substr
相加,得到数字 755
或任何值。
目前在 Solaris 上,我没有 GNU coreutils stat
命令,我希望它可移植且高效,并且仅使用始终可用的标准 unix 工具。 (恕我直言,perl 并不总是可用)。
我的第一次尝试(根目录的示例):
ls -ld / | \
cut -c 2-10 | \
sed -e 's%-%0%g' -e 's%r%4%g' -e 's%w%2%g' -e 's%x%1%g' | \
awk '{print (100 * ((substr($0,1,1)) + (substr($0,2,1)) + (substr($0,3,1))) + \
(10 * ((substr($0,4,1) + (substr($0,5,1)) + (substr($0,6,1)) ))) + \
( (substr($0,7,1)) + (substr($0,8,1)) + (substr($0,9,1)) ) );}'
如您所见,我不关心 setuid 位或文件以外的任何内容,但始终欢迎纯粹的响应!
当然必须有一个更优雅的解决方案。也许是我没有想到的标准unix工具。
大约一周前我“偶然”发现了这个地方,我真的非常喜欢它!令人惊讶的是在一个地方看到这么多的知识、技能和友善! 这是我的第一个问题,所以我很高兴看到是否得到任何答复! :-) 多谢!
Short background: I need to monitor the permissions on a unix file (a directory) with ZABBIX to see if/when they change. ZABBIX doesn't have any built in like vfs.file.mode[xxxx] for this, so I had to roll my own UserParameter, with a numeric type.
What I do so far, is use ls -l | cut -c 2-10
to get the rwxr-xr-x
part, and then use sed
to convert letters to their "weight", and awk
with substr
to sum it up, to get the numeric 755
or whatever value.
This is currently on Solaris, I don't have GNU coreutils stat
command, and I want it to be portable and efficient, and only using standard unix tools, that are always available. (IMHO, perl is not always available).
My first attempt (example for the root directory):
ls -ld / | \
cut -c 2-10 | \
sed -e 's%-%0%g' -e 's%r%4%g' -e 's%w%2%g' -e 's%x%1%g' | \
awk '{print (100 * ((substr($0,1,1)) + (substr($0,2,1)) + (substr($0,3,1))) + \
(10 * ((substr($0,4,1) + (substr($0,5,1)) + (substr($0,6,1)) ))) + \
( (substr($0,7,1)) + (substr($0,8,1)) + (substr($0,9,1)) ) );}'
As you can see, I don't care about setuid bits or anything other than files, but purist responses are always welcome!
Surely there must be a more elegant solution. Perhaps a standard unix tool that I didn't think of.
I found this place "accidentally" about a week ago, and I really really love it! Amazing to see that much knowledge, skills, and friendliness in one place!
This is my first question, so I'm really excited to see if I get any response! :-)
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
ls
的实现各不相同,因此您的ls
也不是真正可移植的。我会使用 Lua 来解决这个问题,它可以在任何 ANSI 标准 C 平台上编译,以及 Lua POSIX 库,它应该是开箱即用的任何 POSIX 平台。在那里,您可以直接调用
stat
并编写清晰简单的代码来进行算术运算。关于 Lua 的精彩介绍Lua 编程可免费在线获取(对于Lua 的早期版本)。最后,如果您关心效率,结果将极其高效,部分原因是 Lua 速度很快,但主要是因为您只需分叉一个进程(Lua 解释器)即可完成所有操作。Implementations of
ls
vary, so yourls
is not really portable either.I'd solve this problem using Lua, which compiles on any ANSI Standard C platform, together with the Lua POSIX library, which should build out of the box on any POSIX platform. There you can make your
stat
call directly and write clear simple code to do the arithmetic. An excellent introduction to Lua Programming in Lua is available free online (for the previous version of Lua). Finally, if you care about efficiency, the results will be extremely efficient, partly because Lua is fast, but mostly because you will be able to do everything while forking only one process—the Lua interpreter.这是啰嗦,但我认为它比你的更易于维护。您可以这样调用它:
ls -ld / |切-c 2-10 | ./perm.awk
。This is long-winded, but I think it's more maintainable than yours. You can call it like this:
ls -ld / | cut -c 2-10 | ./perm.awk
.stat
应该有您要查找的内容。尝试以下其中一项:或
stat
should have what you're looking for. Try one of these:or
如果您的系统有
bash
,并且ls -l
显示文件权限 (rwxrwxrwx),您可以通过以下示例完成您正在寻找的内容:优点是其他实用程序不需要 awk 或 sed 等。
If your system has
bash
, andls -l
shows file permissions (rwxrwxrwx), you may accomplish what you were looking for with the following example:The advantage is that other utilities such as awk or sed are not needed.
我不认为这比您自己的版本更优雅/更高效,但我会将其提出,以防任何技术对改进您自己的版本有用。显然,这可以通过脚本更简单/优雅地完成,
基本前提是使用 tr 将 rwx 转换为相关的八进制数字,然后使用 sed 分成 3 个加号组,然后生成 awk 命令字符串whicg传递给 awk 将它们相加。
I don't believe this is any more elegant/efficient than your own version but I will put it up in case any of the techniques are useful for improving your own. This could obviously be done much more simply/elegantly with a script though
The basic premise is to use tr to translate the rwx to the relevant octal numbers, then use sed to split into groups of 3 add pluses and then generate an awk command string whicg gets passed to awk to add them up.
如果您可以使用
find
那么这看起来会更好:find FILENAME -prune -printf '%m\n'
找到它 此处
if you can use
find
then this looks better:find FILENAME -prune -printf '%m\n'
found it here