Create user through a shell script

To create a new user, you must log in to SQL as a system user. To create a new user account, we are going to write the CREATEUSER command.

We grant all of the privileges to a user in the same script. Let's create the script oracle_02.sh to create a user and grant them privileges:

#!/bin/bash 
 
sqlplus system/Training2 <<MY_QUERY 
set serveroutput on; 
 
create user demo_user identified by userdemo; 
grant connect, resource to demo_user; 
 
MY_QUERY 

Save the script and run it as follows:

          $ chmod +x oracle_02.sh
          $ ./oracle_02.sh
  

The output will be:

    SQL*Plus: Release 11.2.0.1.0 Production on Mon Apr 30 17:44:50 2018
    
    Copyright (c) 1982, 2009, Oracle.  All rights reserved.
    
    Connected to:
    Oracle Database 11g Enterprise ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.