Oracle – Run SQL-Script from Bash

Just create a file and insert SQL commands as you would in sqlplus command line:

[oracle@server ~]#  vim script.sql
                      select sysdate from dual;
                      exit

Now we can run it from bash:

[oracle@server ~]#  su - oracle -c 'sqlplus / as oracle @/path/to/script/script.sql'

  SQL*Plus: Release 12.1.0.2.0 Production on Tue Nov 19 11:46:59 2019
   Copyright (c) 1982, 2014, Oracle.  All rights reserved.
   Connected to:
   Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
   With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

   SYSDATE
   19-NOV-19
   1 row selected.

   Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
   With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

[root@rossa-prod-db21 svenf]#