Need to pass Oracle SQL output to Linux and back... Help!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to pass Oracle SQL output to Linux and back... Help!
# 1  
Old 10-24-2014
Need to pass Oracle SQL output to Linux and back... Help!

Hi all,

Hopefully you can help. This is what I'm trying to achieve:
  1. Obtain a list of usernames out of an Oracle Database
  2. Based on this list, link each username with an Oracle Internet Directory (OID) GUID
  3. Using the username and GUID perform a database update for all users
Here are the commands:
  1. This is SQL which is not the scope of this forum. Let's say I'll have a flat text file that has each username listed on a single line as the output
  2. Now I have to run this list against OID to obtain the so so called GUID. For this I'll run:
    Code:
    $ ldapsearch -h $OIDHOST -p $OIDPORT  -w $OIDPWD "(sn=<USERNAME>)" orclguid sn

The output looks like (used jdoe as a username example):

Code:
cn=doe\, john 
orclguid=B2E3F3CADE9EF026E0430AFE192AF026
sn=jdoe

3. Now I need to pass this back to SQL
Ideally I have one file that takes the output above and prints it like

Code:
jdoe B2E3F3CADE9EF026E0430AFE192AF026

Any thoughts how to do this easily? Remember that the output of step 1 will be a list of app 100 users, and the final output of step 3 should basically have the GUID printed next to this (I assume in a new flat file)

Thanks!
# 2  
Old 10-24-2014
Use/include an awk script to parse for "^orclguid=" and "^sn="...i.e.
Code:
/^orclguid=/ {split($0,a,"=");GUID=a[2]}
/^sn=/ {split($0,a,"=");USER=a[2];print USER, GUID}

something like that

awk can be piped to our script specified inline using ' ', or use -f to specify a awk script file.
If your ldap results in multiple lines, I would save that entire output to a file, then run it through awk.

the key here is awk since you have multiple lines that convey a single context.

This may also work

Code:
ldapsearch ... | awk -F\= '{getline;GUID=$NF;getline;print $NF,$GUID}'


Last edited by blackrageous; 10-24-2014 at 06:01 PM..
# 3  
Old 10-27-2014
Thanks for the help, but I think I'm still stuck.

So let's see I have a flat text file ldap.txt which contains
JDOE
HCLINTON
JBUSH

I will need to loop through those names like this
Code:
ldapsearch -h $OIDHOST -p $OIDPORT  -w $OIDPWD "(sn=JDOE)" orclguid sn
ldapsearch -h $OIDHOST -p $OIDPORT  -w $OIDPWD "(sn=HCLINTON)" orclguid sn
ldapsearch -h $OIDHOST -p $OIDPORT  -w $OIDPWD "(sn=JBUSH)" orclguid sn

The output will look like
Code:
 
cn=doe\, john  
orclguid=B2E3F3CADE9EF026E0430AFE192AF026 
sn=jdoe

cn=clinton\, hillary
orclguid=C2E3F3CADE9EF026E0430AFE192AF025
sn=hclinton

cn=bush\, jeb
orclguid=G2E3F3CADE9EF026E0430AFE192AF031 
sn=jbush

And then THIS output has to be put in a flat file like this
Code:
jdoe B2E3F3CADE9EF026E0430AFE192AF026
hclinton C2E3F3CADE9EF026E0430AFE192AF025
jbush G2E3F3CADE9EF026E0430AFE192AF031

I'm not sure based on your advise how to incorporate that?
# 4  
Old 10-27-2014
Code:
while read -r U
do
        ldapsearch -h $OIDHOST -p $OIDPORT  -w $OIDPWD "(sn=$U)" orclguid sn
done < ldap.txt | while IFS="=" read NAME VAL
do
cn=doe\, john  
orclguid=B2E3F3CADE9EF026E0430AFE192AF026 
sn=jdoe
        if [ -z "$NAME" ]
        then
                printf "%s\t%s\n" "${sn}" "${orclguid}"
                cn=""
                orclguid=""
                sn=""
        else
                read $NAME <<EOF
$VAL
EOF

        fi
done > flatfile

# 5  
Old 10-28-2014
Thanks all for the help. The code below is doing what I am trying to accomplish. I figure to share this in case it helps someone, and I am open for suggestions for any further tuning Smilie

Code:
# Section 1: Obtain list of user names from EBS
echo "
set echo off;
set heading off;
set feedback off;
set pagesize 3000;
set linesize 200;
set termout off
set trimout on;
set trimspool on;
spool $CLONING_LOG/ldap.txt
select user_name from fnd_user where user_id<>3304
and (END_DATE > SYSDATE or END_DATE is null)
and user_name <> 'ASADMIN'
order by user_name;
spool off
exit" > $CLONING_LOG/ldap.sql
sqlplus apps/$APPS_PASSWORD @$CLONING_LOG/ldap.sql
grep -v "^$" $CLONING_LOG/ldap.txt > $CLONING_LOG/ldap2.txt
mv $CLONING_LOG/ldap2.txt $CLONING_LOG/ldap.txt

# Section 2 obtain the orclguid from OID for these users
while read -r U
do
        ldapsearch -h $OIDHOST -p $OIDPORT  -w $OIDPWD "(sn=$U)" orclguid sn | awk 'NR==2,NR==3' | sed 's/^.*\=//' | xargs
done < $CLONING_LOG/ldap.txt | while IFS="=" read NAME 
do
# Section 3 updating the GUID in EBS from OID 
 echo "Now Updating" $NAME
 sqlplus -s apps/$APPS_PASSWORD @$CLONING_SCRIPTS/ebsclone_target_guid.sql $NAME
done

This User Gave Thanks to exm For This Post:
# 6  
Old 10-28-2014
Quote:
Originally Posted by exm
Thanks all for the help. The code below is doing what I am trying to accomplish. I figure to share this in case it helps someone, and I am open for suggestions for any further tuning Smilie
Hmm.

It's cleaner to use a here-document for large multi-line strings.

I'm guessing xargs was to strip off quotes? That's cool. That can be moved outside the loop so it only needs to be run once instead of n times.

It's simple to skip blank lines in the loop instead of editing the temp file with a grep and a mv.

If you're editing the string with awk, don't bother with sed | cut | kitchen | sink -- solve the whole thing in one awk. It'd be nice if we could strip the quotes off too, but xargs really is better at that.

The sed was unnecessary with the code I originally gave you. It split the data on "=" into NAME and VAL. When you got rid of one of those variables you ended up having to do the splitting yourself again, with sed. But never mind, since we need awk, it can do that too.

Code:
# Section 1: Obtain list of user names from EBS
cat > $CLONING_LOG/ldap.sql <<EOF

set echo off;
set heading off;
set feedback off;
set pagesize 3000;
set linesize 200;
set termout off
set trimout on;
set trimspool on;
spool $CLONING_LOG/ldap.txt
select user_name from fnd_user where user_id<>3304
and (END_DATE > SYSDATE or END_DATE is null)
and user_name <> 'ASADMIN'
order by user_name;
spool off
exit
EOF

sqlplus apps/$APPS_PASSWORD @$CLONING_LOG/ldap.sql

# Section 2 obtain the orclguid from OID for these users
while read -r U
do
        [ -z "$U" ] && continue # Skip blank lines

        ldapsearch -h $OIDHOST -p $OIDPORT  -w $OIDPWD "(sn=$U)" orclguid sn | awk -F"=" 'NR==2,NR==3 { print $2 }'
done < $CLONING_LOG/ldap.txt | xargs | while read NAME 
do
# Section 3 updating the GUID in EBS from OID 
 echo "Now Updating" $NAME
 sqlplus -s apps/$APPS_PASSWORD @$CLONING_SCRIPTS/ebsclone_target_guid.sql $NAME
done

# 7  
Old 10-28-2014
awk can be moved completely outside the loop, so awk and xargs only need to be run once instead of n times. Since you're only using sn= (I think!) that simplifies it a lot too.

Code:
# Section 1: Obtain list of user names from EBS
cat > $CLONING_LOG/ldap.sql <<EOF

set echo off;
set heading off;
set feedback off;
set pagesize 3000;
set linesize 200;
set termout off
set trimout on;
set trimspool on;
spool $CLONING_LOG/ldap.txt
select user_name from fnd_user where user_id<>3304
and (END_DATE > SYSDATE or END_DATE is null)
and user_name <> 'ASADMIN'
order by user_name;
spool off
exit
EOF

sqlplus apps/$APPS_PASSWORD @$CLONING_LOG/ldap.sql

# Section 2 obtain the orclguid from OID for these users
while read -r U
do
        [ -z "$U" ] && continue # Skip blank lines

        ldapsearch -h $OIDHOST -p $OIDPORT  -w $OIDPWD "(sn=$U)" orclguid sn
done < $CLONING_LOG/ldap.txt | awk -F"=" '/^sn=/ { print $2 }' | xargs |
while read NAME 
do
# Section 3 updating the GUID in EBS from OID 
 echo "Now Updating $NAME"
 sqlplus -s apps/$APPS_PASSWORD @$CLONING_SCRIPTS/ebsclone_target_guid.sql $NAME
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass Oracle sql script as argument to UNIX shell script?

Hi all, $ echo $SHELL /bin/bash Requirement - How to pass oracle sql script as argument to unix shell script? $ ./output.sh users.sql Below are the shell scripts and the oracle sql file in the same folder. Shell Script $ cat output.sh #!/bin/bash .... (7 Replies)
Discussion started by: a1_win
7 Replies

2. Programming

pass value from Oracle sql to Korn shell

Hi All , I am trying to pass a value from sqlplus to korn shell . There is a table tab1 in Oracle that has a column userdate. I need to pass the userdate to the korn shell . This is what I am doing . VALUE=`sqlplus -silent username/password << END set pagesize 0 feedback off verify off... (14 Replies)
Discussion started by: megha2525
14 Replies

3. UNIX for Dummies Questions & Answers

Removing unnecessary eol ($) character from Oracle sql query output

Hi All, I am fetching oracle query result in shell variable. As columns numbers are more the output wraps in unix terminal .i.e one complete record in db gets store in multiple lines. with each line ends with $ character. I want to remove these unnecessary $ character but to keep required $... (8 Replies)
Discussion started by: Harshal22
8 Replies

4. Shell Programming and Scripting

Pass perl array to SQL oracle

Hello, Could someone please suggest if there is a way to pass an perl array(pass @v_array) to SQL as in below eg : #!/usr/bin/perl @v_array = (1,2,4,5,6,8); $db_userid = 'ni71/ni711'; $bufTPO = qx{ sqlplus -s << EOF $db_userid set verify off set feedback off set... (1 Reply)
Discussion started by: arunshankar.c
1 Replies

5. UNIX for Dummies Questions & Answers

Pass value back to unix variable

i had this unix korn shell code that connects to oracle database and execute the oracle procedure. i need to add a variable that indicates the oracle procedure failed. basically the variable is to check if the oracle procedure failed it will assign 1 and when the variable is equal to 1 it will not... (4 Replies)
Discussion started by: wtolentino
4 Replies

6. Shell Programming and Scripting

How to Pass the Output Values from the PL/SQL Procedure to Shell Script?

hi, Could anyone tell me how to pass the output values of the PL/SQL procedure to Shell script and how to store that values in a shell script variable... Thanks in advance... (5 Replies)
Discussion started by: funonnet
5 Replies

7. Linux

In Oracle Enterprise Linux, not able redirect pvscan output

Hi, I'm not able to redirect output of ovscan and vgscan commands to a file in Oracle Enterprise Linux. Please suggest something. Thanks Mayank (1 Reply)
Discussion started by: discover
1 Replies

8. Filesystems, Disks and Memory

in Oracle Enterprise Linux not able to redirect pvscan output

hi, In Oracle Enterprise Linux I'm not able to redirect output of commands pvscan and vgscan into a file. File is coming blank Please suggest something Thanx. (1 Reply)
Discussion started by: discover
1 Replies

9. UNIX for Dummies Questions & Answers

how to pass values from oracle sql plus to unix shell script

how to pass values from oracle sql plus to unix shell script (2 Replies)
Discussion started by: trichyselva
2 Replies

10. UNIX for Dummies Questions & Answers

How to pass a oracle variable back to the shell script

Hi, I am calling an oracle function that returns a number (either 0 or 2), how do I pass that pass to the wrapping shell script as I would like to do other things based on the value returned by the oracle function. Your help will be appreciated. -------------------------- sqlplus / <<... (3 Replies)
Discussion started by: Jtrinh
3 Replies
Login or Register to Ask a Question