Solved

Pass associative array contents from ksh to calling Ksh

Posted on 2010-09-01
10
1,420 Views
Last Modified: 2013-12-26
I have following output from my plsql procedure :

name     values
-------------------------
log          abc_mmddyyyy.log
file          abc_mmddyyyy.txt
filepath   /root/abc/


I need to pass above values to ksh within which this procedure has been called and in return output them to different ksh.

My limitations are I cannot use nawk or perl. I can use awk and sed within ksh.
I am already using associative array inside plsql procedure where name is indexed.
I can also go without using an assiciative array inside plsql procedure by just having
a straight cursor to pull the name and values, but I need to get exactly the corresponding
values for the given names in the outer most(Parent) ksh.

Thanks in advance
aaba
0
Comment
Question by:aaba646
  • 5
  • 4
10 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 33595847
Try something like this:

#!/usr/bin/ksh
export ORACLE_SID=SOACPROD
ORAENV_ASK=NO
. /usr/local/bin/oraenv
MyScript () {
set -A parm $1 $2 $3 $4 $5
for i in 0 1 2 3 4 5
do
  echo "parm $i: ${parm[$i]}"
done
}
##
## Get array from sqlplus
##
set -A array `
sqlplus -s / <<EOSQL
--
-- Avoid headers and other stuff:
--
set pages 0 trims on term off
--
-- Here goes your query:
--
with tab as (
select 'log' name, 'abc_mmddyyyy.log' value from dual union all
select 'file', 'abc_mmddyyyy.txt' from dual union all
select 'filepath', '/root/abc/' from dual)
select * from tab;
exit
EOSQL`
##
## Pass parameters to another script (Simulated by inline functiop)
##
MyScript "${array[*]}"

Open in new window

0
 

Author Comment

by:aaba646
ID: 33600855
Thanks Mike.
Is there a way that I can send all elements of array i.e array[*] dynamically to array parm with out hardcoding the number of arguments.
This is because my sql query might change at any time.

Thanks
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 33603026
Try:


#...etc...
MyScript () {
set -A parm $*
#...etc...

Open in new window

0
 

Author Comment

by:aaba646
ID: 33605131
How can I assign the function MyScript array contents to the calling ksh array.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 33612796
Please clarify...
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 33621003
OK try this (note the TWO scripts):


$ cat ./MainScript.sh
#!/usr/bin/ksh
#
# MainScript.sh
#
echo "Starting script: $0 `date`"
export ORACLE_SID=orcl
ORAENV_ASK=NO
. /usr/local/bin/oraenv
##
## Get array from sqlplus
##
set -A array `
sqlplus -s / <<EOSQL
--
-- Avoid headers and other stuff:
--
set pages 0 trims on term off
--
-- Here goes your query:
--
with tab as (
select 'log' name, 'abc_mmddyyyy.log' value from dual union all
select 'file', 'abc_mmddyyyy.txt' from dual union all
select 'filepath', '/root/abc/' from dual)
select * from tab;
exit
EOSQL`
##
## Pass parameters to another script (MyScript.sh)
##
./MyScript.sh ${array[*]}
exit


$ cat MyScript.sh
#!/usr/bin/ksh
#
# MyScript.sh Script...
#
echo "Starting script: $0 `date`"
echo "------------------------------------------------------------"
set -A parm $*
i=0
while test i -le $#
do
  echo "parm $i: ${parm[$i]}"
  ((i+=1))
done
exit

Open in new window

0
 

Author Comment

by:aaba646
ID: 33623912
Thanks for all the above more detailed example but my requirement is the other way round.
I want the SQL to be part of more generic ksh and when ever called from other ksh, the generic ksh array elements should be available to the calling ksh. More specifically for above example, Myscript.ksh should return the sql query ouput to Mainscript.ksh as array, when Myscript.ksh is called from Mainscript.ksh. I tried with exporting all array elements inside the loop but I was not successful.

I hope I have not missed any details.

Thanks
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 total points
ID: 33627537
ok then just reverse the scripts like this:
(Note that I commented out the sqlplus crap and replaced by "echo")



#!/usr/bin/ksh
#
# Script: MainScript.sh
#
echo "Starting: $0 `date`"
#
# Set Array:
#
#
# Get array from MyScript.sh
#
set -A array `./MyScript.sh `
i=0
while [ i -lt ${#array[*]} ]
do
  echo "array $i: ${array[$i]}"
  ((i+=1))
done
############################


#!/usr/bin/ksh
#
# MyScript.sh - Generic script to provide values for array
#
export ORACLE_SID=ORCL
ORAENV_ASK=NO
#. /usr/local/bin/oraenv
##
## Get array from sqlplus
##
set -A array `
# Replace following "echo" commands with the sqlplus script:
# name, value
echo 'log abc_mmddyyyy.log'
echo 'file abc_mmddyyyy.txt'
echo 'filepath /root/abc/'
#
#sqlplus -s / <<EOSQL
#--
#-- Avoid headers and other stuff:
#--
#set pages 0 trims on term off
#--
#-- Here goes your query:
#--
#with tab as (
#select 'log' name, 'abc_mmddyyyy.log' value from dual union all
#select 'file', 'abc_mmddyyyy.txt' from dual union all
#select 'filepath', '/root/abc/' from dual)
#select * from tab;
#exit
#EOSQL
`
## Return array
echo "${array[*]}"
##

Open in new window

0
 

Author Closing Comment

by:aaba646
ID: 33632734
It woks Perfect.
Thanks Mike.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now