How do you execute SQL from within a bash script? How do you execute SQL from within a bash script? bash bash

How do you execute SQL from within a bash script?


I'm slightly confused. You should be able to call sqlplus from within the bash script. This may be what you were doing with your first statement

Try Executing the following within your bash script:

#!/bin/bash          echo Start Executing SQL commandssqlplus <user>/<password> @file-with-sql-1.sqlsqlplus <user>/<password> @file-with-sql-2.sql

If you want to be able to pass data into your scripts you can do it via SQLPlus by passing arguments into the script:

Contents of file-with-sql-1.sql

 select * from users where username='&1';

Then change the bash script to call sqlplus passing in the value

#!/bin/bashMY_USER=bobsqlplus <user>/<password> @file-with-sql-1.sql $MY_USER


You can also use a "here document" to do the same thing:

VARIABLE=SOMEVALUEsqlplus connectioninfo << HEREstart file1.sqlstart file2.sql $VARIABLEquitHERE


Maybe you can pipe SQL query to sqlplus. It works for mysql:

echo "SELECT * FROM table" | mysql --user=username database