0

I'm trying to write a script which would accept the customer ID using BASH ( using a basic read command ) and then I want to use that BASH variable in my SQLPLUS query. How can I do that ? I'm trying to use below format, but it is not working.

echo "Enter Customer ID :- ";
read Customer
sqlplus username\password@host
select first_name from customer where customer_id = $Customer;
quit
exit

1 Answer 1

1

Typically, you would do:

echo "select first_name from customer where customer_id = $Customer;" | sqlplus username\password@host

If you want to run multiple queries, it is common to use a heredoc:

cat << EOF | sqlplus username\password@host
select first_name from customer where customer_id = $Customer;
select first_name from customer where customer_id = $Customer;
EOF

edited in response to query in comment:

to store the result of any command in a variable you can use process substitution. var=$( cmd ). In the heredoc case, the syntax is:

var=$( cat << EOF | sql...
query
query
EOF
)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer.. it was very badly needed. Could you also suggest me a way to store the output of query in a variable ?

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.