1

I'm trying to write a script that:

  1. Prompts you for a name
  2. Does a select count in the sqlplus database to see if it exists
  3. End user looks at query results and prompt asks user if they would like to run statement2
  4. 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 
1
  • You will need to ask the question and then have an if statement processing the response. Commented Jul 19, 2017 at 19:44

1 Answer 1

2

You need to add calls to sqlplus to get the output of the queries you've written. See this SO question for assistance with that.

Once you have the output, you can use a conditional, as Raman Sailopal suggested. Something like:

output1="`sqlplus $statement1`" read -p "User $input appears $output1 times. Create user? [Y/n]" answer if [ -z "$answer" -o "$answer" == "y" -o "$answer" == "Y" ] then sqlplus $statement2 else echo "Not creating user." fi 
Sign up to request clarification or add additional context in comments.

11 Comments

That's not really what he's asking. He can't figure out how to make a Shell Script ask for confirmation before continuing to execute statements.
@UlisesAndréFierro, he's got way more problems than that. He's not even querying the database to make a decision in the first place. I figured I'd at least try to get him in the area rather than flag it as either duplicate or unsalvageable. But I agree, he's asking for help with conditionals and prompting the user, but he needs help with everything else too.
I assumed he was showing the portion of the code he was having issues with and that he somehow was executing the statements, but you're right, if that's the whole script then he's definitely asking the wrong question.
@CDahn I'm trying to make the script ask for confirmation before executing statement2. Sorry for the confusing post.
@connollyc4, yep, but you can't ask for confirmation until you have the result of the first statement. Your script is currently not talking to sqlplus. If you forgot to include that in your sample code, it would be helpful if you did that. If that's literally your whole script, you need to add calls to sqlplus to get the count from the database first.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.