Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1712
  • Last Modified:

Passing values between Bash Shell (Oracle Linux 6.5) and Oracle 11g procedures and functions

This is a long post but bear with me.  It has 3 parts.

I am writing a bash shell script that will load and process a flat file.  

This script needs to be able to :
1. pass single variables from the script to an oracle procedure or function   <----- This I am able to do without issue
2. pass a shell array to an oracle procedure or function                    <----- this I need help with
3. assign a single value to a shell variable FROM a procedure or a function   <----- this I need help with
4. assign a multiple values to a shell array variable FROM a procedure        <----- this I need help with

Challenge 2 -------

if we assume in the file named testfile_20140318_09583200_0004.csv contains 10 lines of which:
1st Line = H,20140318,09583200,0004
10th Line = T,1234567

In my script I have the following snipet of code

#!/bin/bash
myfile='testfile_20140318_09583200_0004.csv'

hdr=`sed '1q;d' $myfile`
trl=`sed '$q;d' $myfile`
arr_hdr=($( echo "$hdr" | awk 'BEGIN{FS=","} { for (i=1; i<=NF; i++) print $i }'))
arr_trl=($( echo "$trl" | awk 'BEGIN{FS=","} { for (i=1; i<=NF; i++) print $i }'))
cnt_hdr_fields=${#arr_hdr[@]}
cnt_trl_fields=${#arr_trl[@]}

Open in new window


I then want to be able to pass the array variables arr_hdr and arr_trl whether to a procedure or function to do further validation, something like this:

sqlplus -s /nolog << EOF
      SET TIMING OFF SET FEEDBACK OFF SET SERVEROUTPUT ON
      BEGIN
         pkg_file_process.check_hdr_trl(p_hdr => $arr_hdr , p_trl => $arr_trl );
      END;
/
exit;
EOF
}

Open in new window

I can not get this work.

Challenge 3 -------

Assume my oracle package has another function pkg_file_process.check_filename that returns 0 for success and 1 for failure.  Intent is to validate the parts of the filename against parts of the header record plus other business rules.

I want to capture that return value in a shell variable.

In my script I have the following snipet of code

#!/bin/bash
myfile='testfile_20140318_09583200_0004.csv'

hdr=`sed '1q;d' $myfile`
trl=`sed '$q;d' $myfile`
arr_hdr=($( echo "$hdr" | awk 'BEGIN{FS=","} { for (i=1; i<=NF; i++) print $i }'))
arr_trl=($( echo "$trl" | awk 'BEGIN{FS=","} { for (i=1; i<=NF; i++) print $i }'))
cnt_hdr_fields=${#arr_hdr[@]}
cnt_trl_fields=${#arr_trl[@]}

ret_val=`sqlplus -s /nolog << EOF| grep ^l_status | sed "s/^l_status:= //"
      SET TIMING OFF SET FEEDBACK OFF SET SERVEROUTPUT ON
      DECLARE
         l_status NUMBER;
      BEGIN
         l_status:= pkg_file_process.check_filename(p_filename => '$myfile', p_hdr => '$hdr' );
      END;
/
exit;
EOF`
}
if [ $ret_val -eq 0 ]; then
   echo "File name validates successfully"
else
   echo "File name invalid"
fi 

Open in new window

I can not get this work.

Challenge 4 -------

I saw this snipet from

http://asktom.oracle.com/pls/asktom/f/f?p=100:11:0%3a%3a%3a%3aP11_QUESTION_ID:430819636473

which apparently returns multiple values to a shell array variable.  
#!/bin/ksh
echo "------------------------------"
echo "using an array..."
echo "------------------------------"
## Max 4095 in Sun OS 5.6!
set -A my_arr `sqlplus -s scott/tiger@dev <<EOF
set pagesize 0 feedback off verify off heading off echo off
SELECT table_name from user_tables where rownum < 4;
exit;
EOF`

echo "there are ${#my_arr[*]} elements in the array"
element=0
while [ $element -lt  ${#my_arr[*]} ]
  do
  echo "==>"${my_arr[$element]}
  let element=$element+1;
done

Open in new window

I have attempted to get this concept to work in my bash environment for my specific needs but unsuccessfully.  Is it because this is for a ksh shell and certain commands like 'set -A' are not valid in bash shell?

-----

Any assistance on solutions for 2, 3, 4 would be greatly appreciated.
0
klyles95
Asked:
klyles95
  • 6
  • 5
2 Solutions
 
MikeOM_DBACommented:
1,2) Create external table for the csv file and just "select" what you need for your procedure.

3)
sqlplus -s /nolog << EOF
SET TIMING OFF SET FEEDBACK OFF SET SERVEROUTPUT ON
VAR  l_status NUMBER;
DECLARE
BEGIN
    :l_status:= pkg_file_process.check_filename(p_filename => '$myfile', p_hdr => '$hdr' );
END;
/
exit :L_status;
EOF
ret_val=#?
if [ $ret_val -eq 0 ]; then
   echo "File name validates successfully"
else
   echo "File name invalid"
fi 

Open in new window


4) Yes, arrays may be different for bash, try this:

my_arr=( `sqlplus -s scott/tiger@dev <<EOF
set pagesize 0 feedback off verify off heading off echo off
SELECT table_name from user_tables where rownum < 4;
exit;
EOF`
)

Open in new window

0
 
klyles95Author Commented:
Hello Mike.  Thank you for responding

For 1, 2)

use External Table : That would be good however we are using sql loader.  And so before I go through the exercise of inserting in some cases millions of rows into a staging table I want to verify some things first.

3)

I get the error message:

mytest.sh: line 11: [: #?: integer expression expected

Open in new window


Any ideas on this?

4)  Works perfectly!  Thank you
0
 
MikeOM_DBACommented:
3) Its {dollar}?:

ret_val=$?

External Table is perfect substitute for sql loader.
If you only need to "view" the csv file, check out my article:

Quick and Dirty Unix CSV File Browser
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
klyles95Author Commented:
Hello Mike,

I got 3 to work.  Thank you.  But now I want to enhance a little.  I attempted the following:

#!/bin/bash
# global variable
func_result=''

function do_check () {
   sqlplus -s /nolog << EOF
   SET TIMING OFF SET FEEDBACK OFF SET SERVEROUTPUT ON
   VAR  l_status NUMBER;
   DECLARE
   BEGIN
       :l_status:= pkg_file_process.check_filename(p_filename => '$myfile', p_hdr => '$hdr' );
   END;
   /
   exit :l_status;
   EOF
   ret_val=$?
   func_result=$ret_val
}

## call function
do_check
if [ $func_result -eq 0 ]; then
   echo "File name validates successfully"
else
   echo "File name invalid"
fi 

Open in new window


func_result remains empty even though I have confirmed that $ret_val was assigned a value and subsequently assigned to func_result.  Any ideas?
0
 
MikeOM_DBACommented:
Try this:
. . .
 END;
 /
 exit :l_status;
EOF
return $?
}

## call function
do_check
func_result=$?
if [ $func_result -eq 0 ]; then
   echo "File name validates successfully"
else
   echo "File name invalid"
fi 

Open in new window

0
 
klyles95Author Commented:
Thank you Mike,

I have 3 & 4 working as I desire.  I will discuss external tables with my manager but gut feeling is that he wants to stick with SQL Loader.

I will also look at that script you posted on the link.  

Thank you for your quick and useful (i.e. it works) responses!!!
0
 
klyles95Author Commented:
Hello Mike - I have one last question.

I had done the following:


. . .
 END;
 /
 exit :l_status;
EOF
return $?
}

## call function
do_check | tee -a ${LOGFILE}         ## add redirection to file
func_result=$?
if [ $func_result -eq 0 ]; then
   echo "File name validates successfully"
else
   echo "File name invalid"
fi 

Open in new window


Adding the redirection to the function has now masked the result from the function.  Any idea on how to resolve?  If I should open this as a new question please let me know and I will do so.
0
 
MikeOM_DBACommented:
Why would you want to re-direct a result code ( 0 - 255 ) to a file?
You already display a message, redirect that messaeg to the logfile.
0
 
klyles95Author Commented:
The procedure I am writing is doing dbms_output.put_line statements and those are what I am attempting to capture in the log file.

The side affect however is that the result code is also being redirected.  If I remove the redirection, I have the result code but then the dbms_output.put_line from the proc are not being captured in the file.
0
 
MikeOM_DBACommented:
then try this:
 END;
 /
 exit :l_status;
EOF
return $?
}

## call function
do_check >> ${LOGFILE}         ## add redirection to file
func_result=$?
if [ $func_result -eq 0 ]; then
   echo "File name validates successfully"
else
   echo "File name invalid"
fi 

Open in new window

0
 
klyles95Author Commented:
Thank you Mike...that did it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now