0

I'm writing a script that supposed to be ran with sudo permissions. But then I need to execute a command from an another user sourcing it's .bash_rc where there are some env variables are overloaded and then use it in a command.

For example. I need to print the PATH variable for the user:

sudo -s -u ${USER_NAME} source ~/.bash_rc; echo $PATH

But instead it prints PATH for a user running the script. I've also tried a lot of other options (with su or passing a rcfile) but none of them seems to work.

How could I deal with it?

Mehdzor
  • 101

2 Answers2

1

You've got a sudo command that sources a script. You've also got a command that prints $PATH. The two are otherwise unrelated.

Perhaps you meant this:

sudo -u "${USER_NAME}" bash -c 'source ~/.bash_rc; echo $PATH'

The -s can't be used with a compound command, but it could be used with a script containing the two (or more) commands:

cat >/opt/bin/show_me_the_path <<'X'
#!/bin/bash
source ~/.bash_rc
echo $PATH
X
chmod a+x /opt/bin/show_me_the_path

sudo -u "${USER_NAME}" -s /opt/bin/show_me_the_path

But even that doesn't really make much sense because the script mandates its own shell (/bin/bash). All in all it's probably better to rethink the -s flag.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • Actually the command sudo -u "${USER_NAME}" bash -c 'source ~/.bash_rc; echo $PATH' does not work. I have a user "zippy" that has a ~/.bash_rc file. The user executing the sudo command, "bschuck" does not have that file. bschuck@valhalla:~$ sudo -u zippy bash -c 'source ~/.bash_rc; echo $PATH' bash: /home/bschuck/.bash_rc: No such file or directory /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin If I add the '-i' option to the sudo command, I don't get the error, but PATH doesn't reflect what was sourced in ~zippy/.bash_rc. – Deathgrip Jun 05 '17 at 00:37
0

You could execute it as:

sudo su - ${USER_NAME} -c 'source ~/.bash_rc; echo $PATH'

An example of it running. The user "zippy" has a ~/.bash_rc file. Note this is in addition to the standard ~/.bashrc file. The user executing the command, "bschuck" does not. The ~zippy/.bash_rc file contains:

#!/bin/bash
# .bash_rc
PATH=$PATH:/this/is/a/dummy/path

Executing this command works:

bschuck@valhalla:~$ sudo su - zippy -c 'source ~/.bash_rc; echo $PATH'
/home/zippy/bin:/home/zippy/.local/bin:\
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin:\
/this/is/a/dummy/path

Yet this command does not keep the new PATH sourced from the ~zippy/.bash_rc file:

bschuck@valhalla:~$ sudo -i -u zippy bash -c 'source ~/.bash_rc; echo $PATH'
/home/zippy/bin:/home/zippy/.local/bin:/usr/local/sbin:\
/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:\
/snap/bin

If you need to run multiple commands, it's best to put them in a script as I suggested yesterday in a comment to my answer.

Deathgrip
  • 2,566
  • Arrgghh it's that pointless sudo su again – Chris Davies Jun 03 '17 at 21:45
  • Pointless, but it works. Now perhaps if aside from bashing (see what I did there?) my answer you also contributed a solution to the issue at hand... – Deathgrip Jun 03 '17 at 22:28
  • If you put the commands source ~/.bash_rc; echo $PATH in a script named /tmp/foo.sh, you can do this:

    sudo -i -u ${USER_NAME} /tmp/foo.sh

    – Deathgrip Jun 03 '17 at 22:37