计算 chmod

发布于 2024-09-13 23:17:03 字数 351 浏览 4 评论 0原文

假设我有一个权限为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杆小烟枪 2024-09-20 23:17:03
U   G   O
rwx r-x r-x ==
111 101 101 ==
  7   5   5

这里唯一的数学运算是从 2 进制到 10 进制(实际上是 8 进制)的转换。

添加组 r/w 后,您将得到:

U   G   O
rwx rwx r-x ==
111 111 101 ==
  7   7   5

(U=用户 G=组 O=其他)

U   G   O
rwx r-x r-x ==
111 101 101 ==
  7   5   5

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   G   O
rwx rwx r-x ==
111 111 101 ==
  7   7   5

(U=user G=group O=other)

缱绻入梦 2024-09-20 23:17:03

对于每个 U、G 和 O,只需将每个 r、w 或 x 替换为二进制 1,并将每个 - 替换为二进制 0。
结果是一个二进制数。
该数字不能大于二进制 111,十进制和八进制都等于 7。

因此,U、G 和 O 各有一个八进制数。现在只需将它们按正确的顺序组合在一起即可。从技术上讲,您将 U 乘以 8^2,G 乘以 8^1,O 乘以 8^0,因为八进制位是 8 的幂。

这一切似乎都在您提供的链接中进行了解释。

编辑:回复

谢谢,我明白这一切,但如果我在表格或程序中写这个,我会怎么做?

这取决于您使用的编程语言。您希望程序转换数字还是实际使用 chmod?

编辑:回答:

输入是一个权限值,例如 660。旁边是一个字符串,例如。 g+rw。程序计算新的权限值。

好的,首先将当前权限分为 U、G 和 O,然后使用字符串来告诉要修改哪些权限。你可以从数字中唯一地找到权限,反之亦然*,并且你知道r=4、w=2和x=1,所以每当你遇到没有设置适当条件的数字时,你会添加4 、 2 或 1 。之后,您应该检查结果是否有效。如果不是,只需将其设置为 0。

Example: current=660 command=g+rw
u = 6
g = 6
o = 0
from string: we know we are modifying g only
g = 6 means: rw-
since rw is already set, we do not change anything
result = u+g+o = 660
return result

假设该命令是 a+rw。 u 和 g 的情况与上面完全相同,但是当达到 o 时,我们可以看到它是 0,因此我们为 r 添加 4,为 w 添加 2,得到 6。这是一个有效的数字,因此我们将其组合它们全部并返回 666。

*有效组合:

rwx 7

rw-6

接收5

r-- 4

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

Thanks, I understand all that, but if I where writing this in the form or a program how would I go about it?

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:

Input would be a permissions value eg 660. Along side a string eg. g+rw. The program the calculates the new permission value.

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.

Example: current=660 command=g+rw
u = 6
g = 6
o = 0
from string: we know we are modifying g only
g = 6 means: rw-
since rw is already set, we do not change anything
result = u+g+o = 660
return result

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:

rwx 7

rw- 6

r-x 5

r-- 4

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文