Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Pass associative array contents from ksh to calling Ksh

Posted on 2010-09-01
10
Medium Priority
?
1,521 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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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

597 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