0

I have a shell script that runs as root. I want the script to switch to oracle user, run sqlplus and run some .sql files. I am trying to followung :

su - oracle << -EOF1  2>&1
        sqlplus $user/$password << -EOF2
                @oracle.sql;
                @quartz.sql;
        EOF2
EOF1

first of all i get stty: standard input: Inappropriate ioctl for device what does it mean ?

second, can someone explain to me how the redirect (should) work in this case ?

Thanks

2 Answers 2

2

Use:

if [ "$(id -un)" -eq "root" ]; then
    exec su - oracle -c $0
fi

sqlplus <<EOF
blablabla
EOF

If your script potentially takes arguments, the solution will differ.

What this does it checking whether the user running is currently root. If so, it re-executes the script ($0) as user oracle instead.

But BTW, why does the script run as root in the first place?

Sign up to request clarification or add additional context in comments.

5 Comments

i agree running the script as root is a very bad practice, but i have no choice since the infrastructure that runs the script is running as root. Changing it is not my call
so what does your solution do exactly ?
I see. nice trick, unfortunately, there are some parts of the script that i need to be running as root (this is not the whole script, there are other parts). therefore i have to use the EOF block structure. my scrip could also have arguments as well
Make the Oracle specific parts run in another script which you invoke like the above?
Also, please note that heredoc markers MUST be at the beginning of line
0

su - oracle -c " echo 'select 1 from dual; select 2 from dual;'| sqlpus / as sysdba "

if contain ' using following su - oracle -c " echo \"select 1 from dual; select 2 from dual;\" | sqlpus / as sysdba "

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.