1

I have requirement where in,I need to remove a oracle user and this should be done in a shell script.I am new to both sql and shell scripting.I am using below script but not able to remove the user.Can any one help me out

#!/bin/sh
su - oracle;
sqlplus as / sysdba;
startup force;
drop user SM cascade;
9
  • 1
    sqlplus as / sysdba; is wrong. It should be sqlplus / as sysdba Commented Feb 6, 2015 at 7:07
  • Hi Kumar thanks,I tried that ,but still not able to delete the user Commented Feb 6, 2015 at 7:24
  • Redirect the responses into a logfile, and show the content here. Commented Feb 6, 2015 at 7:33
  • SQL*Plus: Release 11.2.0.3.0 Production on Fri Feb 6 17:48:08 2015 Copyright (c) 1982, 2011, Oracle. All rights reserved. ERROR: ORA-01031: insufficient privileges Enter user-name: Commented Feb 6, 2015 at 7:41
  • Isn't is clear enough looking at the error? You couldn't even connect to database. Commented Feb 6, 2015 at 7:45

1 Answer 1

5

You're running a series of commands in the shell, but they are not connected. Your su starts a new shell as the Oracle user, but then exits and your script continues as the original user. You then try to run SQL*Plus as that original user, and (with the command corrected) that OS user doesn't have permission to connect as SYSDBA - that is protected. And the startup/drop commands won't run until SQL*Plus has exited, when they are also just (invalid) shell commands.

Conceptually you want something more like this; running SQL*Plus as an su command rather than a shell, and using a 'heredoc' to give SQL commands:

#/bin/sh
su - oracle -c sqlplus -s -l / as sysdba << !EOF
drop user SM cascade;
exit;
!EOF

The startup command seems rather out of place there so I've left it out, but you can run whatever commands you like before the !EOF marking the end of the heredoc.

But mixing su with a heredoc may not work ("standard in must be a tty"?) depending on your environment. You can put everything except the su - oracle -c in a script instead:

#!/bin/sh
sqlplus -s -l / as sysdba <<!EOF
drop user SM cascade;
exit;
!EOF

and call that from the command line with su - oracle -c "<path to your script>", but your script then needs to be visible to and executable by the Oracle account.

Having a script lying around that will drop a user, especially with cascade, seems rather dangerous; all too easy to run it accidentally at some point, maybe years later, maybe wiping out a lot of important data. I imagine this is just an example but it doesn't really seem like something you'd script at all, particularly for a normal user to be able to run whenever they like.

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

1 Comment

Also, why startup force in this script? It's an unsupported command and generally unwise to use; it issues a shutdown abort to the database. Why would you want to do that?

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.