简体   繁体   English

在 shell 脚本中包含 chmod - 它适用于所有人吗?

[英]Including chmod in shell script - will it work for everyone?

I have a large wrapper.sh file which does a lot of things, but in the end runs catalina.sh tomcat.我有一个很大的wrapper.sh文件,它可以做很多事情,但最后运行catalina.sh tomcat。

Problem is that users who run this wrapper.sh are experiencing error: no permissions to execute catalina.sh .问题是运行此wrapper.sh的用户遇到错误:没有执行catalina.sh的权限。 They have to manually set this permission, which is something I do not want.他们必须手动设置此权限,这是我不想要的。


On my computer, chmod u+x catalina.sh fixes this, therefore I included it in the wrapper.sh , right before executing catalina.sh :在我的电脑上, chmod u+x catalina.sh修复了这个问题,因此我将它包含在wrapper.sh中,就在执行catalina.sh之前:

#!/bin/sh
# <...>
# <...>
chmod +x catalina.sh
exec ./catalina.sh

But will this change work for everyone?但这种改变对每个人都有效吗? I can imagine, for example, that it indeed works fine on my computer, but someone else would get some sudo-related error when implicitly running that chmod .例如,我可以想象它在我的计算机上确实运行良好,但其他人在隐式运行该chmod时会遇到一些与 sudo 相关的错误。 How can I make it work for everyone?我怎样才能让它适用于所有人?

How about running it instead by sh catalina.sh or bash catalina.sh (depending on what language that script is written in)?如何通过sh catalina.shbash catalina.sh (取决于脚本编写的语言)来运行它? In this case, you don't need x-permission.在这种情况下,您不需要 x 权限。 Read permission is enough.读取权限就足够了。

It will work if the user is root, or else the owner of catalina.sh .如果用户是 root,或者是catalina.sh的所有者,它将起作用。

An unprivileged user cannot change the permissions on a file that the user does not own, even if it is writable.非特权用户不能更改用户不拥有的文件的权限,即使它是可写的。

If catalina.sh is something that the same user unpacked, or that your script created, there shouldn't be a problem.如果catalina.sh是同一个用户解压的,或者是您的脚本创建的,那么应该没有问题。

The one issue might be that the user is trying to do this in a filesystem that is mounted noexec .一个问题可能是用户试图在挂载noexec的文件系统中执行此操作。 But then the wrapper.sh script itself wouldn't be executable.但是wrapper.sh脚本本身将不可执行。 Yet, the user could successfully run sh wrapper.sh in spite of it not being executable, and then run into the catalina.sh problem.然而,用户可以成功运行sh wrapper.sh ,尽管它不可执行,然后遇到catalina.sh问题。

Note that you can exec sh./catalina.sh .请注意,您可以exec sh./catalina.sh Just because the script not executable doesn't mean you cannot exec a new process image for the interpreter to process it;仅仅因为脚本不可执行并不意味着您不能为解释器执行新的进程映像来处理它; you just have to do it explicitly rather than relying on the operating system to bind the interpreter to the script.您只需要明确地执行它,而不是依赖操作系统将解释器绑定到脚本。 That is what requires execute permissions.那就是需要执行权限。

  • Create a user group "sudo groupadd new_group".创建一个用户组“sudo groupadd new_group”。
  • Add the users who need access to this group: "sudo usermod -a -G new_group userA" for a single user, or: "for user in userA userB userC; do sudo usermod -a -G mygroup "$user"; done" to add multiple users with a loop.添加需要访问该组的用户:“sudo usermod -a -G new_group userA”对于单个用户,或:“for user in userA userB userC; do sudo usermod -a -G mygroup "$user"; done”使用循环添加多个用户。
  • Then "sudo chown somebody:new_group catalina.sh"然后“sudo chown somebody:new_group catalina.sh”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM