I'm writing a bash script that will run a series of SQL files and output to either LESS or a file. I figured it would be safer to not hard code the database password into my script, and I didn't feel comfortable putting the password info into a file, either.

I had come up with the following solution:

### code block 1
sqlplus -s -L /nolog >"output_file.txt" <<-EOF
    connect username/$(read -s -p "Password: " a && echo -n "$a" && unset a)@database
    # various sqlplus settings
    start $sql_file

I wanted a way to look at the results without saving them to disk, so I added a flag $less_flag) so I can occasionally pipe to less. The following is directly below the EOF from block 1.

if [[ $less_flag -eq 1 ]]; then
    less -S <"output_file.txt"
    rm "output_file.txt"

Based on this answer, I changed the first line above to...

### code block 2
sqlplus -s -L /nolog <<-EOF | _output

The function _output is defined as follows:

_output() {
    # ...
    if [[ $less_flag -eq 1 ]]; then
        sed -E 'stuff' </dev/stdin | less -S
        sed -E 'other stuff' </dev/stdin >output_file.txt
    return 0

When I run the script with $less_flag set to 0, everything works as expected. But when the $less_flag is set to 1, it asks for the password and immediately proceeds to _output without letting me type said password. I end up having to kill the process.

Instead of code block 2, I've also tried this (also based on the above link's suggestions):

    sqlplus -s -L /nolog <<-EOF
        connect #...
        start   #...
} | _output

But the result is the same - it just hangs.

Any thoughts as to what is going on? Should I just go back to saving the output regardless, calling less after sqlplus exits if I want it, then removing the file afterwards?


Your Answer

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

Browse other questions tagged or ask your own question.