Avaldor
Avaldor

Reputation: 377

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.

Problem is that users who run this wrapper.sh are experiencing error: no permissions to execute 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:

#!/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. How can I make it work for everyone?

Upvotes: 0

Views: 2022

Answers (3)

Kaz
Kaz

Reputation: 58578

It will work if the user is root, or else the owner of 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.

The one issue might be that the user is trying to do this in a filesystem that is mounted noexec. But then the wrapper.sh script itself wouldn't be executable. Yet, the user could successfully run sh wrapper.sh in spite of it not being executable, and then run into the catalina.sh problem.

Note that you can 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.

Upvotes: 2

user1934428
user1934428

Reputation: 22225

How about running it instead by sh catalina.sh or bash catalina.sh (depending on what language that script is written in)? In this case, you don't need x-permission. Read permission is enough.

Upvotes: 4

Vir&#225;g Marcell
Vir&#225;g Marcell

Reputation: 9

  • Create a user 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.
  • Then "sudo chown somebody:new_group catalina.sh"

Upvotes: 0

Related Questions