3

I have a script:

#! /bin/bash
sudo chown -R $USER:$USER /opt/Folder

The chown doesn't seem to execute.

If I just write the above line from terminal ,it runs fine though!

I have read this but doesn't help.

The name of the script is mysetup.sh and I am running as sudo sh mysetup.sh.

It doesn't give any output.

If I run it like sudo bash -x mysetup.sh , I am receiving:

sudo chown -R root:root /opt/Folder

This problem does not related to this,since the USER is set.

  • What's the exit code after running? Could you /bin/bash -x it to trace what is going on? – thrig Jul 8 '16 at 21:28
  • @thrig:Just write /bin/bash -x from a terminal?It shows many lines and I can't see something related.I tried also #! /bin/bash -x inside the script but where do I see the messages? – George Jul 8 '16 at 21:35
  • What he means is to run the script using bash -x scriptname. Or put set -x in the script itself. – Barmar Jul 9 '16 at 9:36
  • @Barmar:Hello , I updated. – George Jul 9 '16 at 12:37
3

You're trying to change the contents of /opt/Folder to your own userid, aren't you.

Here's your command once again:

sudo chown -R $USER:$USER /opt/Folder

This works from the command line because the $USER variable is evaluated before the command is executed, and it is therefore executed as this (assuming your username is george):

sudo chown -R george:george /opt/Folder

On the other hand, when you run a command with sudo, amongst other things the $USER variable takes the value of the target user, which in this case is root. So by running sudo bash mysetup.sh you are running mysetup.sh as root, so the command runs with $USER set to root and is evaluated like this

sudo chown -R root:root /opt/Folder

The solution in your case is to avoid running the entire script itself under sudo, and let the script use sudo where necessary to run specific commands with elevated privileges. In other words, use bash mysetup.sh instead of sudo bash mysetup.sh.

  • Your recommendation is what he was doing in the original version of the question. – Barmar Jul 9 '16 at 13:32
  • @George Isn't this what you were doing originally? You've edited the question, but it originally had the sudo command inside the script. – Barmar Jul 9 '16 at 13:33
  • @Barmar:I have (and had ) the sudo command insided the script but I was running the script also with sudo. – George Jul 9 '16 at 13:38
-3

You haven't mentioned your distribution and version of Linux (or unix) you are running, so I am going on a tangent here, assuming this following line is in your /etc/sudoers

Defaults requiretty

hence you can not run sudo without a tty. And when you try to run it from the script, your sudo doesn't run, effectively crippling your chown command.

Try adding -t or -tt switches to your sudo command. These will create a pseudo terminal to run your sudo chain of commands and hopefully will succeed but this option has its side effects as well, not to be discussed here. You can search for them if you want to.

If you have access to sudo configuration, you take out the line mentioned above for the best of solutions but with this action, you are eliminating one level of barricades, despite how weak it is.

  • 1
    Firstly any script run by the user is going to inherit the tty it was launched from. Second sudo's -t flag has nothing to do with ttys, you're thinking of ssh. – Patrick Jul 9 '16 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.