0

I am running a bash script that includes a here document, which calls SQLPLUS. This includes a .sql script to perform several grants. It works correctly without the substitution.

I want to be able to substitute in a bash variable into the grant statements. Is that possible?

This is a snippet from the bash script

CREDLINE=ownusr/ownpass
GRANT2DO=foo.sql
PASSPROC=bar    <=== this is what I want to pass
sqlplus << EOQ11
$CREDLINE@chaos01
@$GRANT2DO
quit
EOQ11

This is a snippet from the foo.sql

grant execute on $PASSPROC to user86;
grant execute on $PASSPROC to user99;

I have tried several variations on $VAR and &1, but none has worked so far.

2 Answers 2

1

You have to call the sql-script with the value of what you want to pass

from sqlplus execute the script:

@foo.sql bar

in foo.sql use:

grant execute on &1 to usr
Sign up to request clarification or add additional context in comments.

Comments

0

You can make your script create .sql file instead of having it replace existing one. Then pass it to sqlplus like you already do.

To make the script readable, you can use so-called "here documents" syntax, which is described here and which you are seemingly already familiar with.

If you insist on doing actual substitution, you could copy the template .sql to some temporary file and run sed -i on it with proper arguments to replace the variables. That, however, is much more complicated than the approach above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.