1

I need to run a SQL select query in an Oracle database and have to capture the list of retrieved records in a shell script. Also I would like to modify the query for certain conditions and need to fetch it again. How can I do this?

Is there a way to have a persistent connection to an Oracle database using a shell script?

2

You can do something like the following:

#!/bin/bash

DatabaseconnectStr=user/password@ALIAS
Dynamic=DynamicContent

sqlplus -S ${DatabaseconnectStr} << EOF

spool results.out
SELECT * FROM YOUR_TABLE WHERE COLUMN = ${Dynamic} ;
spool off
exit;
EOF
  • Anyone who uses this, please make sure the folder you're placing the script in is only readable by you. Alternatively, put the password in another file in a protected folder and include it in the script. Still, I wouldn't do this in production. – Patrick R. Feb 10 '17 at 16:51
  • A way to get around that issue is having a tool like Jenkins call the script and store the passwords there so that it's not visible to others unless granted. – Eli Mar 14 '17 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.