2

Starting with root, I want execute a sql script with another $USER. But I have to set the right $ORACLE_HOME and $PATH for $USER.

With this command I receive error export: command not found

su - $USER -c "export ORACLE_HOME=$ORA_HOME;export PATH=$ORA_HOME/bin:$PATH;sqlplus / as sysdba"

My question is: starting executing a script in root, how can I export a environment variable for another user and execute a command using that environment variable?

  • 4
    Perhaps the default shell of $USER is one that does not have an export (csh or tcsh for example) – steeldriver Aug 20 '19 at 12:16
  • ksh and I don't have problem using export outside su - user – NxA Aug 20 '19 at 13:53
  • 2
    @NxA yes, but that's your shell, right? What does su - $USER -c 'echo $SHELL' print? – terdon Aug 20 '19 at 13:59
  • yes you are right, is set to CSH – NxA Aug 20 '19 at 14:08
1

As steeldriver commented, it's likely that your target user is using csh or tcsh. If you want to be sure of the shell that you're using, invoke it explicitly:

su - "$USER" -c \
  "/bin/ksh -c 'export ORACLE_HOME=$ORA_HOME;export PATH=$ORA_HOME/bin:$PATH;sqlplus / as sysdba'"

While the (sub)shell will contain any (exported) environment variables from the main shell, it will not contain any (unexported) shell variables from the user's main shell.

| improve this answer | |
0

You don't need to export anyway. Just run:

su - $USER -c "ORACLE_HOME=$ORA_HOME;PATH=$ORA_HOME/bin:$PATH;sqlplus / as sysdba"

The export is only needed if you need the variables to be inherited by any child shells launched by your shell. Here, you're launching the command from the same shell, so there's no need to export.

But, as pointed out in the comments, that won't help if sqlplus needs to see those variables, only if they need to be set in order to find sqlplus correctly. If that fails, try:

su - $USER -c "ORACLE_HOME='$ORA_HOME' PATH='$ORA_HOME/bin:$PATH' sqlplus / as sysdba"
| improve this answer | |
  • It's likely that sqlplus needs to see the variables (at least ORACLE_HOME). – Kusalananda Aug 20 '19 at 13:59
  • @Kusalananda that was my original thought as well, but I couldn't get that to work. However, that was just because I was using su -c 'foo=bar echo "a:$foo"' to test, stupidly forgetting that the string would be expanded before the actual command so of course it didn't work! D'oh! – terdon Aug 20 '19 at 14:02

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.