?
Solved

Pass associative array contents from ksh to calling Ksh

Posted on 2010-09-01
10
Medium Priority
?
1,503 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:aaba646
ID: 33605131
How can I assign the function MyScript array contents to the calling ksh array.
0
 
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 2000 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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

741 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