Passing a parameter from a shell script to sqlplus

Hi All,

I'm new to Linux and scripting, apologies in advance for 'stupid' questions.

Please help... Im writing a script that calls a sqlplus script but the sqlplus requires inputs and i cant seem to get this to work.

here is my code.

#!/bin/sh
TERM=vt100
export TERM
WORKDIR=/mnt/sr_scripts/PER
export WORKDIR
cd $WORKDIR
 
DEV="dev" #this is dev environment.
TEST="test" #this is TEST environment.
 
echo "Enter Environment"
read mENV
echo "Enter a valid PERSON_ID for the employee:"
read PID
echo "Display Payroll Element Entries (Y|N):"
read PEE

if [ "$mENV" == "$DEV" ]; then
 sqlplus -silent apps/apps @PER12.sql
 expect "Enter a valid PERSON_ID for the employee:"
 send "$PID"
 expect "Display Payroll Element Entries (Y|N):"
 send "$PEE"
elif [ "$mENV" == "$TEST" ]; then
 sqlplus -silent apps/apps123 @PER12.sql
 expect "Enter a valid PERSON_ID for the employee:"
 send "$PID"
 expect "Display Payroll Element Entries (Y|N):"
 send "$PEE"
fi

Please note that the first step of the script executes, im able to kick off the sql script but the sql script dont accept the parameters ie: PID and PEE... It just prompts for it again.

Thanking you in advance.

Regards
Mahomed

$
$
$ cat -n per12.sql
     1  begin
     2    dbms_output.put_line ('You entered PID = ' || '&1');
     3    dbms_output.put_line ('You entered PEE = ' || '&2');
     4  end;
     5  /
     6
$
$
$ cat -n myscript.sh
     1  #!/bin/bash
     2  TERM=vt100
     3  export TERM
     4  WORKDIR=/mnt/sr_scripts/PER
     5  export WORKDIR
     6  #cd $WORKDIR
     7
     8  DEV="dev" #this is dev environment.
     9  TEST="test" #this is TEST environment.
    10
    11  echo "Enter Environment"
    12  read mENV
    13  echo "Enter a valid PERSON_ID for the employee:"
    14  read PID
    15  echo "Display Payroll Element Entries (Y|N):"
    16  read PEE
    17
    18  if [ "$mENV" == "$DEV" ]; then
    19    LOGIN="apps/apps"
    20  elif [ "$mENV" == "$TEST" ]; then
    21    LOGIN="apps/apps123"
    22  fi
    23
    24  sqlplus -silent $LOGIN @per12.sql $PID $PEE <<EOF
    25    -- other stuff that you may want to do in sqlplus here...
    26    exit
    27  EOF
    28
$
$
$ ./myscript.sh
Enter Environment
dev
Enter a valid PERSON_ID for the employee:
100
Display Payroll Element Entries (Y|N):
Y
old   2:   dbms_output.put_line ('You entered PID = ' || '&1');
new   2:   dbms_output.put_line ('You entered PID = ' || '100');
old   3:   dbms_output.put_line ('You entered PEE = ' || '&2');
new   3:   dbms_output.put_line ('You entered PEE = ' || 'Y');
You entered PID = 100
You entered PEE = Y
 
PL/SQL procedure successfully completed.
 
Elapsed: 00:00:00.21
$
$
$ ./myscript.sh
Enter Environment
test
Enter a valid PERSON_ID for the employee:
200
Display Payroll Element Entries (Y|N):
N
old   2:   dbms_output.put_line ('You entered PID = ' || '&1');
new   2:   dbms_output.put_line ('You entered PID = ' || '200');
old   3:   dbms_output.put_line ('You entered PEE = ' || '&2');
new   3:   dbms_output.put_line ('You entered PEE = ' || 'N');
You entered PID = 200
You entered PEE = N
 
PL/SQL procedure successfully completed.
 
Elapsed: 00:00:00.21
$
$

Hi Durden_Tyler,

Thank you kindly for your reply.

Please excuse my dumb question...

where must this go? in the sql script?
:confused::confused:
Regards
Mahomed

cat -n per12.sql
1 begin
2 dbms_output.put_line ('You entered PID = ' || '&1');
3 dbms_output.put_line ('You entered PEE = ' || '&2');
4 end;
5 /
6

Yes, that is my sample SQL script.
I don't know the contents of your SQL script - "PER12.sql", so I created my own sample script to demonstrate how you could pass parameters to it.

Since you put the "expect" command after calling "PER12.sql", my guess is that your SQL script prompts for the inputs when it is run directly in sqlplus. So, I'd think you have an "accept" statement in there somewhere in your SQL script that accepts substitution variables.

What I meant to say in my post was - you could remove the named substitution variables in your original "PER12.sql" script and use "&1", "&2" instead. That way you can pass them in the invocation line itself.

Hi Durden_Tyler,

Thank you very much for your help.

You are correct, the PER12.sql script does have the ACCEPT statement, I will now modify and try...

before:
ACCEPT v_personId PROMPT 'Enter a valid PERSON_ID for the employee: '
PROMPT
ACCEPT v_payElements PROMPT 'Display Payroll Element Entries (Y|N): '

After:
ACCEPT v_personId = &1
REM PROMPT 'Enter a valid PERSON_ID for the employee: '
REM PROMPT
ACCEPT v_payElements = &2
REM PROMPT 'Display Payroll Element Entries (Y|N): '

Let me give this a bash.

Cheers!!!
Mahomed