Passing a parameter from a shell script to sqlplus | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Passing a parameter from a shell script to sqlplus

Shell Programming and Scripting


Tags
oracle, sqlplus shell select_sql

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-15-2013
Mahomed Mahomed is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 16 August 2013, 8:18 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Oracle 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.


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

Last edited by Scrutinizer; 08-15-2013 at 03:27 AM.. Reason: code tags
Sponsored Links
    #2  
Old 08-15-2013
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 24 October 2014, 3:29 AM EDT
Posts: 1,841
Thanks: 7
Thanked 266 Times in 241 Posts

Code:
$
$
$ 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
$
$

Sponsored Links
    #3  
Old 08-16-2013
Mahomed Mahomed is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 16 August 2013, 8:18 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Durden_Tyler,

Thank you kindly for your reply.

Please excuse my dumb question...

where must this go? in the sql script?

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
    #4  
Old 08-16-2013
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 24 October 2014, 3:29 AM EDT
Posts: 1,841
Thanks: 7
Thanked 266 Times in 241 Posts
Quote:
Originally Posted by Mahomed View Post
...
where must this go? in the sql script?
...
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.
Sponsored Links
    #5  
Old 08-16-2013
Mahomed Mahomed is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 16 August 2013, 8:18 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell Script passing parameters to sqlplus code compprog11 Shell Programming and Scripting 4 08-31-2012 01:55 PM
Passing a Unix parameter to SQLPlus login command Jcpratap UNIX for Dummies Questions & Answers 2 03-08-2012 05:12 AM
How to pass parameter from sqlplus(procedure completed) to your shell script sanora600 Shell Programming and Scripting 1 08-08-2008 05:55 AM
error in passing a variable to sqlplus from a shell script kripssmart Shell Programming and Scripting 3 07-08-2008 07:01 PM
passing parameters from a shell script to sqlplus phani Shell Programming and Scripting 2 03-13-2005 07:41 PM



All times are GMT -4. The time now is 12:17 AM.