The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to include RETURN KEY with Background process "&" in Shell Script racbern Shell Programming and Scripting 1 03-11-2008 07:30 AM
passing a list of dynamic names to a "PS" command in shell script? sachin.tendulka Shell Programming and Scripting 3 11-21-2007 04:18 AM
Q: Recording shell script screen output using "script" command ? lalfonso.gomez Shell Programming and Scripting 4 01-18-2007 09:31 PM
Shell Script fails with "can't connect to client" sseenu79 HP-UX 2 12-20-2006 10:47 AM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 04:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-04-2005
alan's Avatar
alan alan is offline
Registered User
  
 

Join Date: Jul 2003
Location: Los Angeles
Posts: 53
[Oracle] "Dynamic" sql / Shell script

Greetings!

I wrote this shiny (serenitymovie.com) shell script and wanted to share it with the rest of you. My configuration is Ksh93 on HP-UX v11. Here it goes


#!/usr/bin/ksh
...

# Declare environment parameters, logons, functions...
#----------------------------------------------------#

. $HOME/lib/ETL.lib
...

# Gets TODAY'S DATE from main db instance.
# This will be used to manipulate at runtime the WHERE clause of a SQL \
# statement that gets run.
#------------------------------------------------------#

rcode1=`${ORACLE_HOME}/bin/sqlplus -s <<EOF1
${SRC_ORACLE_LOGON} # DB uname/password in .lib
set heading off;
select to_char(sysdate, 'MMDDYYYY') from dual;
exit;
EOF1`

# Gets YESTERDAY'S DATE from main db instance.
# This will be used to manipulate at runtime the WHERE clause of a SQL \
# statement that gets run.
#-------------------------------------------------------#

rcode2=`${ORACLE_HOME}/bin/sqlplus -s <<EOF2
${SRC_ORACLE_LOGON} # DB uname/password in .lib
set heading off;
select to_char(sysdate-1, 'MMDDYYYY') from dual;
exit;
EOF2`
...

# Sets properties of datum1 & datum1 to rcode1 & rcode2.
# This will initialize the conditions (i.e. dates) in the WHERE clause \
# as well as the file name the query results get spooled out to.
#---------------------------------------------------------#

export rcode1 rcode2 ;
export ORACLE_SID=another_oracle_db_instance_name ;
export filler="080000" ;
export datum1="$rcode1$filler" ; export datum2="$rcode2$filler"
...
# Set properties for file management.
#----------------------------------------------------------#

export suffix=".uld" ; export label1="SALES_REVENUE_"

# Download of SALES REVENUE data.
# Exporting `salesrevdatafname` will allow building dynamically \
# the output file name with a timestamp I obtained earlier.
#----------------------------------------------------------#

export salesrevdatafname="$label1$rcode3$suffix"
${ORACLE_HOME}/bin/sqlplus -s <<! > $LOG/sql.og
${TGT_ORACLE_LOGON} # DB uname/password in .lib
SET ARRAYSIZE 5000 COLSEP '|'
spool $DATA_TGT/$salesrevdatafname;
SELECT ... FROM SALES
WHERE trx_start_time >= to_date('$datum2','MMDDYYYYHH24MISS') AND
trx_start_time < to_date('$datum1','MMDDYYYYHH24MISS');
spool off;
exit;
!
...


That's it. The main reason why I am posting this is that I went thru a lot of trial & error to get what I was looking for (this). Who knows, this code may be of help to someone with similar need.

NB: I was able to tweak this code to make it run on IBM DB2 on AIX as well.

At the risk of sounding like a pompous *ss...This bit of code is in the public domain. Modify it as you please.
  #2 (permalink)  
Old 10-04-2005
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
Nice job especially since it works so well for you.

One question. Why are you exporting every one of your variables?

By the way, you can give yourself a bit of a short cut and avoid the unnecessary second sqlplus session by using a little different technique. This one uses an array.

Code:
set -A DATE_ARRAY $(
sqlplus -s /nolog <<EOF
connect user/password
set pages 0 feedback off verify off echo off head off
select to_char(sysdate,   'MMDDYYYY') || '235959'
      ,to_char(sysdate-1, 'MMDDYYYY') || '000000'
from   dual;
EOF
)

sqlplus  /nolog <<EOF > /dev/null
connect user/password@yourotherdatabase
set ... your output parameters
spool ...
SELECT ...
FROM   yourtable
WHERE  date_column  between to_date('${DATE_ARRAY[2]}', 'MMDDYYHH24MISS')
                    and     to_date('${DATE_ARRAY[1]}', 'MMDDYYHH24MISS');
spool off
EOF
or, with only one database connection:
Code:
sqlplus  /nolog <<EOF > /dev/null
connect user/password@yourotherdatabase
set ... your output parameters

REM This will create variables that can be used in the second SQL
COLUMN BDATE NEW_VALUE BDATE
COLUMN EDATE NEW_VALUE EDATE

SELECT to_char(sysdate,   'MMDDYYYY') || '235959' EDATE
      ,to_char(sysdate-1, 'MMDDYYYY') || '000000' BDATE
FROM   DUAL;

spool yourfilename&&BDATE...
SELECT ...
FROM   yourtable
WHERE  date_column  between to_date('&&BDATE', 'MMDDYYHH24MISS')
                    and     to_date('&&EDATE', 'MMDDYYHH24MISS');
spool off
EOF

Last edited by tmarikle; 10-05-2005 at 11:19 AM.. Reason: Left out the real point of example 2 where the variable is used for the filename.
  #3 (permalink)  
Old 10-05-2005
alan's Avatar
alan alan is offline
Registered User
  
 

Join Date: Jul 2003
Location: Los Angeles
Posts: 53
[Oracle] "Dynamic" sql / Shell script

Quote:
Originally Posted by tmarikle
Nice job especially since it works so well for you.

One question. Why are you exporting every one of your variables?
Three simple, really not fancy, reasons:

1) Configuration management
1.A) I first code/test my work using the env variables of my dev/qa server/environment such Oracle DB user name/password, Oracle TNS name, directory name(s) etc.
1.B) One my qa is completed, I push my shell script to my prod server. The variables still have identical names as in `dev`; however, I can/do change the values of each variable to match my `prod` environment e.g. Oracle DB user name/password, Oracle TNS name, directory name(s) etc.

2) Security
2.A) The shell scripts have a chmod value which allows anybody that's not me (ie. my Unix id) to read the code. However, the chmod value of my ETL.LIB file make it so that it can only view/modified by me and the root user. Stuff like passwords, file/directory names etc is hidden from most people.

3) Simplicity
3.A) I have 214 shell scripts like the one I posted. Some are wrappers to SQL*Loader jobs, others to SQL*scripts...It is a lot easier for me to manage changes by editing one single file (ETL.LIB...as opposed to each one of them) whenever our DBA feels like changing passwords, TNS names or when our sysadmin decides to mount a new file system etc...

That's about it.

PS: Thanks for the kind comments.
  #4 (permalink)  
Old 10-05-2005
alan's Avatar
alan alan is offline
Registered User
  
 

Join Date: Jul 2003
Location: Los Angeles
Posts: 53
Thumbs up

Quote:
Originally Posted by tmarikle
sqlplus /nolog <<EOF > /dev/null
connect user/password@yourotherdatabase
set ... your output parameters

REM This will create variables that can be used in the second SQL
COLUMN BDATE NEW_VALUE BDATE
COLUMN EDATE NEW_VALUE EDATE

SELECT to_char(sysdate, 'MMDDYYYY') || '235959' EDATE
,to_char(sysdate-1, 'MMDDYYYY') || '000000' BDATE
FROM DUAL;

spool yourfilename&&BDATE...
SELECT ...
FROM yourtable
WHERE date_column between to_date('&&BDATE', 'MMDDYYHH24MISS')
and to_date('&&EDATE', 'MMDDYYHH24MISS');
spool off
EOF

I like this. it is really clever example since you're able to accompliish the same thing as me in one single shot.

Thanks for your input!!

Alan
  #5 (permalink)  
Old 03-13-2009
RubinPat RubinPat is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 75
Hi Alan or tmarkille or anybody,

we just came across a requirement whereby we need a unix or perl script and need to change the password of different [around 10]oracle database schemas every year. Is it possible and can you'll plz guide me how to do it plz? Honestly i have no idea how to do it.
Thanks,
Rubin
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0