I'm trying to write a script that:
- Prompts you for a name
- Does a
select count
in the sqlplus database to see if it exists - End user looks at query results and prompt asks user if they would like to run statement2
- If you say yes then a statement2 runs. If no script is killed
The issue I am having is after the first query runs to check if it exists, I'm not sure how to add a prompt asking if you want to insert into the database
Here is my code so far:
echo "Please enter last name: 'input;"
read input
statement1="select count(*) as count from users
where fname = '"$input"'"
statement2="INSERT INTO users VALUES ('"$input"');
$ORACLE_HOME********************************
$statement1
/
quit;
Eossql
The part I can't figure out is after statement 1 is executed I want the system to ask if statement 2 should be executed. Any suggestions?
Thanks!
EDIT: Here is my edited code thanks to CDahn suggestion. This solution works but I was wondering if there is a cleaner way to do this instead of connecting to sqlplus twice.
echo "Please enter last name: 'input;"
read input
Statement2="INSERT INTO users VALUES ('"$input"');"
output1=$($sqlplus -s User/Pass@connection <<EOF
set head off
select count (*) from users
where fname = '$input';
EXIT;
EOF
)
read -p "User $input appears $output1 times. Create user? [Y/n]" answer
if [ -z "$answer" -o "$answer" == "y" -o "$answer" == "Y" ]
then
$sqlplus -s User/Pass@connection << Eossql
set autocommit on
set head off
$Statement2
quit;
Eossql
else
echo "Not creating user"
fi