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

x
?
Solved

Shell Script

Posted on 2008-10-08
15
Medium Priority
?
779 Views
Last Modified: 2013-12-26
SHELL SCRIPT

We have to create a shell script with an Oracle INSERT command to insert rows into Table B from Table A every night with the following two conditions:

1. where users NOT IN XX and

2. based on SYSDATE

This shell script will be scheduled to run every night through the cron.

This is an urgent task.  Please respond at your earliest convenience.

Thanks!
0
Comment
Question by:as93is
[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
  • 7
  • 3
  • 2
  • +2
15 Comments
 
LVL 1

Expert Comment

by:rgoble
ID: 22675521
Assuming that XX isn't changing you can modify the following SQL to use the correct table names and adjust the where clause.

INSERT INTO TABLE_B (SELECT * FROM TABLE_A WHERE users NOT IN XX);

After updating that line save it to a somefile.sql (name it something meaningful so you know whats its for later).

Next create your shell script (I know this works on windows, the syntax my differ on linux) with the following line:

sqlplus username/password @"somefile.sql"
0
 

Author Comment

by:as93is
ID: 22677050
The suggestions are useful.

Please give me a sample of a complete shell script.  I am new to AIX.

I want to use this shell script in the cron job.

I want to try this script and finalize it today.  This is an high priority task.

Thanks!!!
0
 
LVL 6

Expert Comment

by:peter991
ID: 22677094
This is on way..

#! /bin/ksh
sqlplus username/password >> /var/tmp/sqllogg.log 2>&1 <<!
@/oracle/sqlscripts/somefile.sql
exit
!
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:as93is
ID: 22677132
I am new to AIX

Please explain what the following code will do.  This is code from your example:

>> /var/tmp/sqllogg.log 2>&1 <<!  

Also, could you please suggest another way in case the above doesn't work in my environment.

Thanks!!!
0
 
LVL 6

Expert Comment

by:peter991
ID: 22677309
/var/tmp/sqllogg.log is the logfile from the sqlscript you are running.
In other words.. You can check how the sql-job went.
It's good to create a log if you are running the job through the crontab.
0
 

Author Comment

by:as93is
ID: 22677332
Thank you very much for your quick reponse with useful sample code and explanation.

I will try this and get back to you.

Thanks!!!
0
 
LVL 10

Expert Comment

by:dbmullen
ID: 22678168
why is the schell script really a requirement?  
you can create a procedure and schedule a dbms_job.
then, it's in the database

CREATE OR REPLACE PROCEDURE why_use_shell_script
IS
BEGIN
 
  INSERT INTO TABLE_B (SELECT * FROM TABLE_A WHERE users NOT IN XX);
  commit;
  
END;
/
 
DECLARE
  X NUMBER;
BEGIN
  SYS.DBMS_JOB.SUBMIT
  ( job       => X 
   ,what      => 'why_use_shell_script;'
   ,next_date => to_date('20081009','yyyymmdd')
   ,interval  => 'TRUNC(SYSDATE) + 1 + 8/24 + 30/1440'
   ,no_parse  => FALSE
  );
  SYS.DBMS_OUTPUT.PUT_LINE('Job Number is: ' || to_char(x));
COMMIT;
END;
/

Open in new window

0
 

Author Comment

by:as93is
ID: 22685318
There is TABLE_A in the ORACLE database.  There is a DATABASE VIEW based on TABLE_A.  
The platform is AIX.

Here are the REQUIREMENTS:

1.  EXPORT TABLE_A data EVERY NIGHT.

2.  EVERY NIGHT AFTER THE EXPORT

     INSERT into TABLE_B (SELECT a, b, c
                         FROM VIEW
                         WHERE user NOT IN XXX);

3.  EVERY NIGHT AFTER THE INSERT
    TRUNCATE TABLE_A.

STEP 1 - CREATE THREE SHELL SCRIPTS

I thought that I could create a shell script for each of the above requirements as follows:

1.  One SHELL SCRIPT for EXPORT

2.  One SHELL SCRIPT for INSERT with the INSERT statement in a SQL script

3.  One SHELL SCRIPT for TRUNCATING TABLE_A.

STEP 2 - SET UP THREE CRON JOBS

SET UP THREE CRON JOBS TO RUN THE ABOVE THREE SHELL SCRIPTS EVERY NIGHT WITH A GAP OF HALF AN HOUR.

Please let me know if I've to SET VARIABLES in each of the above shell scripts.

Please let me know how best to meet the above requirements with examples/sample code ASAP.  
This is a high priority task with a strict deadline - 10-13-2008.

Thanks.
0
 
LVL 6

Expert Comment

by:peter991
ID: 22685611
Hello!

"Please let me know if I've to SET VARIABLES in each of the above shell scripts."

Yes!

Regards.
0
 

Author Comment

by:as93is
ID: 22691531
Hi peter991,

This is NOT THE SOLUTION that I expected.  

The solution that I expected was help to create the SHELL SCRIPTS for STEP 1 and STEP 2.

My question WAS NOT "Please let me know if I've to SET VARIABLES in each of the above shell scripts."

There is TABLE_A in the ORACLE database.  There is a DATABASE VIEW based on TABLE_A.  
The platform is AIX.

Here are the REQUIREMENTS:

1.  EXPORT TABLE_A data EVERY NIGHT.

2.  EVERY NIGHT AFTER THE EXPORT

     INSERT into TABLE_B (SELECT a, b, c
                         FROM VIEW
                         WHERE user NOT IN XXX);

3.  EVERY NIGHT AFTER THE INSERT
    TRUNCATE TABLE_A.

STEP 1 - CREATE THREE SHELL SCRIPTS

1.  One SHELL SCRIPT for EXPORT

2.  One SHELL SCRIPT for INSERT with the INSERT statement in a SQL script

3.  One SHELL SCRIPT for TRUNCATING TABLE_A.

STEP 2 - SET UP THREE CRON JOBS

SET UP THREE CRON JOBS TO RUN THE ABOVE THREE SHELL SCRIPTS EVERY NIGHT WITH A GAP OF HALF AN HOUR.

I need help to CREATE THE SHELL SCRIPTS FOR STEP 1 and STEP 2.  

Please let me know how best to meet the above requirements with examples/sample code ASAP.  
This is a high priority task with a strict deadline - 10-13-2008.

CAN SOMEONE HELP ME WITH THE ABOVE ASAP TO MEET MY DEADLINE?


Thanks.
0
 
LVL 35

Expert Comment

by:johnsone
ID: 22693932
Why 3 scripts?  As time goes on and things get bigger, these individual tasks may take more than 30 minutes to complete.

I just threw this together and it is not tested.  Some of the syntax may be incorrect as well, but it should be a good start.

You are going to have to fill in the ? with all the correct values for your system.
#!/bin/ksh
 
# Set Oracle environment
ORACLE_HOME=?
ORACLE_SID=?
PATH=${PATH}:${ORACLE_HOME}/bin
export ORACLE_HOME ORACLE_SID PATH
 
logfile="nightly_script.log.`date`"
 
(
#
# Export TABLE_A
#     NOTE:  This should log in as the owner of the table
#
exp userid=user/pass file=? tables=table_a compress=n
 
#
# Copy data to TABLE_B and truncate TABLE_A
#     NOTE:  This should log in as the owner of the table and the owner of both tables
#            is assumed to be the same
sqlplus << EOF
user/pass
WHENEVER SQLERROR EXIT;
INSERT into TABLE_B
     (SELECT a, b, c
        FROM VIEW
       WHERE user NOT IN XXX
     ); 
truncate table table_a;
exit
EOF 
) > "$logfile" 2>&1

Open in new window

0
 

Author Comment

by:as93is
ID: 22694573
Hi JOHNSONE,  

This is an excellent solution.  This is the kind of solution that I am looking for.  I will include all the correct values for my system and test it thoroughly.

I was thinking of three shell scripts for my three requirements.  You have combined all the three requirements into one shell script.  This is good.

WHAT WILL HAPPEN EXPORT or the INSERT FAILS in the shell script?

The EXPORT of TABLE_A AND INSERT INTO TABLE_B SHOULD to be successfully completed before I TRUNCATE TABLE_A.  

I will be in trouble if the EXPORT and INSERT fail and TABLE_A is truncated.  This is why I was thinking of three shell scripts instead of one.

There is a STEP 2 in my requirement.  I NEED HELP with the following:

SCHEDULE CRON JOB TO RUN the nightly_script.sh EVERY NIGHT AT 1 A.M.

I have to use the nightly_script.sh and schedule a cron job to run this shell script every night at 1 a.m.

Please help me with the CRON JOB code.  I have complete this by 10-13-200 (Monday).

I am thankful to you for your timely help!!!









0
 

Author Comment

by:as93is
ID: 22695815
WHAT WILL HAPPEN EXPORT or the INSERT FAILS in the shell script?

The EXPORT of TABLE_A AND INSERT INTO TABLE_B SHOULD to be successfully completed before I TRUNCATE TABLE_A.  

I will be in trouble if the EXPORT and INSERT fail and TABLE_A is truncated.  This is why I was thinking of three shell scripts instead of one.

There is a STEP 2 in my requirement.  I NEED HELP with the following:

SCHEDULE CRON JOB TO RUN the nightly_script.sh EVERY NIGHT AT 1 A.M.

I have to use the nightly_script.sh and schedule a cron job to run this shell script every night at 1 a.m.

Please help me with the CRON JOB code.  I have complete this by 10-13-200 (Monday).



0
 
LVL 35

Accepted Solution

by:
johnsone earned 2000 total points
ID: 22710524
You will need to check the return code and exit if the exp fails.  Check the documentation, as there are different codes returned by exp that have different meanings.  You may not want to fail if you do get some of the codes.

If the insert fails, the script should exit.  That is what the WHENEVER statement should do.  If you want more robust error handling, encase the statements in an anonymous PL/SQL block and handle all the errors there.

This should be the line you need to put in the cron file:

0 1 * * * /full_path/nightly_script.sh
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Progress
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

610 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