Solved

Pass associative array contents from ksh to calling Ksh

Posted on 2010-09-01
10
1,432 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
improve Shell script 5 149
Unix How to Bulk Remove Multiple Directories from FileSystem 10 77
incorrect file reading in bash 7 104
how to pick files up from ftp via script 8 52
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 …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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 video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 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

17 Experts available now in Live!

Get 1:1 Help Now