1

In a bash script, I am trying to run a function which pass as parameter a command line. And so I am trying to pass a find command as another user.

Instead of running straight the find command, I have a function run-command which basically run and output the command and the result. Nothing complex.

I am trying to run this command:
su - ${USER} -c '$(run-command find ${DIR} -name TEST* -exec rm -rf "{}" +)'
But it is failing, message error:

I am able to run find command as another user as following:
- su - ${USER} -c '$(find ${DIR} -name TEST* -exec rm -rf "{}" +)'
- sudo -u ${USER} find ${DIR} -name "TEST*" -exec rm -rf "{}" +

It looks like I can not call a function of the script when I switch with su command to another user.

  • I want to continue my script as the current user after running this command line.
  • I do not want to give special privilege to current user to run that command himself (nothing to modify in /etc/sudoers).

Thanks in advance for your help.

jardindeden
  • 125
  • 1
  • 12
  • Why do you have `$()` around the command you want to run? That will execute `run-command`, then try to execute its output as another command. – Barmar Jan 07 '19 at 16:08
  • `su` and `sudo` have to execute the command in a new shell. Functions and variables from the current shell are not available there. – Barmar Jan 07 '19 at 16:10
  • Can you post the whole script? I can show you how solve this with `HERE` docs, but it does require for the function to be declared in the `HERE` doc. – itChi Jan 07 '19 at 16:10
  • 3
    Make `run-command` a shell script instead of a function. Then you should be able to use it from the new shell process. – Barmar Jan 07 '19 at 16:11
  • Great solution @Barmar but I am struggling still, If I try to run my script via sourcing, it still tells me that the command is not found. If I do source and then run the script again, there my `run-command` is available as a shell script. But when running my script, I have plenty of arguments defined in advance by the user and so by doing extra step `source script.sh`I loose all my arguments. Would it be possible to source it and use the new shell script straight ? – jardindeden Jan 07 '19 at 17:45
  • The script is trying to source itself in the superuser shell? – Barmar Jan 07 '19 at 17:46
  • No as the same user, but if I source the script as current user `userA` and then when I switch user to `userB`, it is creating a subshell and so I can not run `run-command`as `userB`, right ? What kind of solution would be possible so ? – jardindeden Jan 07 '19 at 17:57

1 Answers1

0
su - ${USER} -c '$(run-command find ${DIR} -name TEST* -exec rm -rf "{}" +)'

The first issue you're having is that this command is running run-command... as the already logged in user and then passing the result to su.... To mend this, simply change it to the following:

su - ${USER} -c 'run-command find ${DIR} -name TEST* -exec rm -rf "{}" +'

The next issue here is that su is creating a new shell session, so we cannot pass our current variables and functions to the new session. I propose that you create a shell script that contains run-command and running that from su -c..., bringing our final command to:

su - ${USER} -c '/path/to/run-command.sh find ${DIR} -name TEST* -exec rm -rf "{}" +'

or

su - ${USER} -c 'bash /path/to/run-command.sh find ${DIR} -name TEST* -exec rm -rf "{}" +'
Aidan Lovelace
  • 400
  • 2
  • 10