0

I am a root user and in a shell script I would like to change user to oracle than run a sql script, I tried following;

#!/bin/sh

portStatus=`lsof -ni:5060`
if [ ${#portStatus} -ne 0 ]
  then
    sudo -u oracle << EOF
    /oracle/product/102/db/bin/sqlplus -s a513s6p4/a513s6p4 @/oracle/product/102/db/GW_EP_List.sql;
    EOF
  else
    exit
fi

it gives me following error;

./deneme2.sh: syntax error at line 12: `end of file' unexpected

Can you please let me know what might be the problem?

Thanks, Halit

3 Answers 3

1

When using here documents the closing string MUST be at the beginning of the line!

Try

#!/bin/sh

portStatus=`lsof -ni:5060`
if [ ${#portStatus} -ne 0 ]
  then
    sudo -u oracle << EOF
    /oracle/product/102/db/bin/sqlplus -s a513s6p4/a513s6p4 @/oracle/product/102/db/GW_EP_List.sql;
EOF
  else
    exit
fi
Sign up to request clarification or add additional context in comments.

Comments

0

You can use su. Remember get environment with su -:

COMMAND="/oracle/product/102/db/bin/sqlplus -s a51... "
su - oracle -c $COMMAND

A nice sample oracle-base site, Automating Database Startup and Shutdown on Linux Post:

case "$1" in
    'start')
        # Start the Oracle databases:
        # The following command assumes that the oracle login 
        # will not prompt the user for any values
        su - $ORA_OWNER -c "$ORA_HOME/bin/lsnrctl start"
        su - $ORA_OWNER -c $ORA_HOME/bin/dbstart
        touch /var/lock/subsys/dbora
        ;;

Comments

0
sudo -u oracle /oracle/product/102/db/bin/sqlplus -s a513s..........

You don't need EOF here. Execute your sqlplus command like above. In this case your oracle user must be a sudo user.

If oracle is a normal user

su - oracle -c "/oracle/product/102/db/bin/sqlplus -s a513s.........."

A little more about su command (From man page):

The su command is used to become another user during a login session. Invoked without a username, su defaults to becoming the superuser. The optional argument - may be used to provide an environment similar to what the user would expect had the user logged in directly. Additional arguments may be provided after the username, in which case they are supplied to the user's login shell. In particular, an argument of -c will cause the next argument to be treated as a command by most command interpreters. The command will be executed by the shell specified in /etc/passwd for the target user.

2 Comments

What you mean by su iso sudo ?
Dear suku, thanks I tried with su iso sudo and it worked, however I am wondering if I can run it in silence mode because whenever I run it oracle loging text comes up which bothers me :( #!/bin/ksh ..... su - oracle -c "/oracle/product/102/db/bin/sqlplus -s a513s6p4/a513s6p4 @/oracle/product/102/db/GW_EP_List.sql"; ..... ---> WARNING: Do not use computer if you are not authorized. WARNING: You have been warned. Sourcing /etc/.profile-EIS.....

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.