计算 chmod
假设我有一个权限为 755 (rwxr-xr-x)
的文件。我想chmod g+rw
到我的文件。最终得到 775。这里用什么数学方法来计算这个值?我不太明白,谢谢。
或者类似地从 640 到 660。发生的公式是什么?
http://articles.slicehost.com/ 2010/7/17/using-chmod-part-2-octal-mode
谢谢
Lets suppose I have a file with the permission 755 (rwxr-xr-x)
. I want to chmod g+rw
to my file. To end up with 775. Whats the maths that takes place here to calculate that? I cant quite get my head around it thanks.
Or similarly 640, to 660. Whats the formula taking place?
http://articles.slicehost.com/2010/7/17/using-chmod-part-2-octal-mode
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里唯一的数学运算是从 2 进制到 10 进制(实际上是 8 进制)的转换。
添加组 r/w 后,您将得到:
(U=用户 G=组 O=其他)
The only math here is the conversion from base-2 to base 10 (or 8, actually).
After adding group r/w, you'll get:
(U=user G=group O=other)
对于每个 U、G 和 O,只需将每个 r、w 或 x 替换为二进制 1,并将每个 - 替换为二进制 0。
结果是一个二进制数。
该数字不能大于二进制 111,十进制和八进制都等于 7。
因此,U、G 和 O 各有一个八进制数。现在只需将它们按正确的顺序组合在一起即可。从技术上讲,您将 U 乘以 8^2,G 乘以 8^1,O 乘以 8^0,因为八进制位是 8 的幂。
这一切似乎都在您提供的链接中进行了解释。
编辑:回复
这取决于您使用的编程语言。您希望程序转换数字还是实际使用 chmod?
编辑:回答:
好的,首先将当前权限分为 U、G 和 O,然后使用字符串来告诉要修改哪些权限。你可以从数字中唯一地找到权限,反之亦然*,并且你知道r=4、w=2和x=1,所以每当你遇到没有设置适当条件的数字时,你会添加4 、 2 或 1 。之后,您应该检查结果是否有效。如果不是,只需将其设置为 0。
假设该命令是 a+rw。 u 和 g 的情况与上面完全相同,但是当达到 o 时,我们可以看到它是 0,因此我们为 r 添加 4,为 w 添加 2,得到 6。这是一个有效的数字,因此我们将其组合它们全部并返回 666。
*有效组合:
For each of U, G, and O, just replace each r,w, or x with a binary 1 and each - with a binary 0.
The result is a binary number.
That number cannot be larger than binary 111, which is equal to 7 in both decimal and octal.
So you have one octal number for each of U, G, and O. Now simply put them together in the proper sequence. Technically, you would be multiplying U by 8^2, G by 8^1, and O by 8^0, since octal places are powers of 8.
This all seems to be explained in the link you provided.
EDIT: replying to
That would depend on the programming language you are using. Do you want the program to convert the numbers or actually to use chmod?
EDIT: answer for:
Ok, so first separate the current permissions into U, G, and O, then use the string to tell which of those to modify. You can uniquely find the permissions from the number and vice versa*, and you know that r=4, w=2, and x=1, so whenever you encounter a number which does not have the appropriate condition set, you would add 4, 2, or 1 to it. After that, you should check to see if the result is valid. If not, just set it to 0.
Let's say the command had been a+rw instead. The exact same as above would happen for u and g, but when o is reached, we can see that it is 0, so we add 4 for r and 2 for w, giving us 6. This is a valid number, so we combine them all and return 666.
*Valid Combinations: