0

I am trying to write a bash function which should delete some entries from table1 if the user enters 1, otherwise remove different entries. My function looks like this:

function reset_db
{
        if [ $usr_input == 1 ]  
        then
        sqlplus -s $USR/$pwd@$SID << EOF

        delete from table1 where component = 'ABC';
        delete from table2 where component = 'ABC';
        exit
EOF
        else if [ $usr_input == 2 ]

        delete from table1 where component = 'XYZ';
        delete from table2 where component = 'XYZ';
        exit
EOF
fi
}

I am getting error: syntax error near unexpected token `fi'

I am sure that it is happening because I am using if-else incorrectly somewhere but not able to figure out a way to fix it.

Also please let me know how can I post code under the same thread if I have any more follow up questions.

1
  • Please don't use "Thanks" in your questions. It is useless noise. Commented Feb 25, 2013 at 19:47

2 Answers 2

2

Your 'else if' is wrong, the correct syntax is 'elif'.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the quick response. Actually I do programming in Java and now I am learning bash (which is going to be my scripting language). So I am having hard time with syntax. Anyways thanks again.
1

You need to repeat the command in each clause of the if statement:

function reset_db
{
        if [ $usr_input == 1 ]  
        then
        sqlplus -s $USR/$pwd@$SID << EOF

        delete from table1 where component = 'ABC';
        delete from table2 where component = 'ABC';
        exit
EOF
        elif [ $usr_input == 2 ]; then

        sqlplus -s $USR/$pwd@$SID << EOF
        delete from table1 where component = 'XYZ';
        delete from table2 where component = 'XYZ';
        exit
EOF
fi
}

As a simplification, you should refactor this:

reset_db () {
    if [[ $usr_input = 1 ]]; then
      to_delete='ABC'
    elif [[ $usr_input = 2 ]]; then
      to_delete='XYZ'
    else
      return
    fi
    sqlplus -s "$USR/$pwd@$SID" <<EOF
    delete from table1 where component = '$to_delete'
    delete from table2 where component = '$to_delete'  
    exit
EOF
    fi
}

7 Comments

Thank you. That's very helpful to have a precise code. But I have some schemas which end with 'ABC'. Ex: read_ABC, write_ABC, execute_ABC. If I give something like: delete from table1 where component = '%\_$to_delete' then I am getting "invalid character" error. How can I do this?
It is pointing the error at "%". delete from upgrade_details where component = %_'ABC' * ERROR at line 1: ORA-00911: invalid character
It would be better to use bind variable.
It is fixed. There was a typo. Also I used "like" to do the string match.
1. I want to prompt user for the owner name and read in a variable called owner ` read -p "Enter schema owner: " owner 2. Now I want to pass this value to this block of sql: declare usr2kill varchar2(5) := '&owner'; sqlstmt varchar2(10); ......some more code to delete the users... My code looks like this: function reset_db { read -p "Enter Schema Owner: " owner declare usr2kill varchar2(5) := '&owner'; sqlstmt varchar2(10); begin ......some more code to drop the users... Please let me know how can I do this. Thanks in advance.
|

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.