Problem running anonymous plsql blocks from shell script
Im trying to run an anonymous plsql block from a shell script using sqlplus -s but the command doesnt seem to get run. Does anyone know if there are there any known issues with doing this?
Here is te shell script im trying to run against an oracle 8.1.6 database.
#!/usr/bin/sh
#######################################################################
# Script to create privileged database user
# usage: CreateSchema.sh [USERNAME] [PASSWORD]
#
# WARNING: THIS SCRIPT WILL DROP THE EXISTING USER IF IT ALREADY EXISTS
#
#
# Mark Blakey 21/11/02
#######################################################################
USERNAME=$1
USERPASSWORD=$2
if [ -n "$2" ]
then
echo creating user $1
else
echo Insufficient number of arguments- please supply more arguments
echo usage: CreateSchema.sh [USERNAME] [PASSWORD]
echo WARNING: THIS SCRIPT WILL DROP THE EXISTING USER IF IT ALREADY EXISTS
exit
fi
sqlplus -s /nolog << EOF
connect / as sysdba
whenever sqlerror exit sql.sqlcode;
set serveroutput on
set echo on
SELECT sysdate as starttime from dual;
dbms_output.put_line('creating user ' || userName );
END IF;
EXECUTE IMMEDIATE userCreateSql;
dbms_output.put_line('User ' || UserName || ' created ');
EXCEPTION
WHEN others THEN
raise_application_error(-20112,' CREATE USER: ' ||
userName || ' failed ' ||substr(sqlerrm,1,64))
END;
/
show errors
GRANT dba TO $USERNAME;
GRANT select any table TO $USERNAME;
GRANT execute on sys.dbms_sql TO $USERNAME;
GRANT execute on sys.DBMS_SYS_SQL TO $USERNAME;
GRANT create any sequence TO $USERNAME;
SELECT sysdate as endtime from dual;
spool off
exit sql.sqlcode;
EOF
LOG=$?
if [ $LOG != 0 ]
then
echo unexpected error
exit
else
echo user creation success
fi
exit
It doesnt execute the anonymous block. it doesnt even execute the SELECT sysdate as endtime from dual;
statement at the end of the script. However it does execute the
"SELECT sysdate as starttime from dual" statement at the beginning of the script which proves that sqlplus is recognozing commands from within the here document.
Bookmarks