?
Solved

How can I automate this UNIX script

Posted on 2012-12-29
13
Medium Priority
?
701 Views
Last Modified: 2012-12-31
I want to automate the following commands so that I could run it as a cronjob on my Solaris10 server:

I have a source directory :
/var/www/temp1/
The files are like
1356652700.file1.dat.bz2.cor
1356652700.file2.dat.bz2.cor
1356652700.file3.dat.bz2.cor

I know the file name part except for the timestamp part (1356652700). Each time the cron is run, the timestamp part of the set of files will be different. In the next run I will probably have these in the source: 1356658200.file1.dat.bz2.cor , 1356658200.file2.dat.bz2.cor and 1356658200.file1.dat.bz2.cor.

So I do:

#!/bin/sh
DIR1=/var/www/temp1/
DIR2=/var/www/process/

Now I manually run:
$cd /var/www/process/
$mv 1356652700.file1.dat.bz2.cor 1356652700.file1.dat.bz2
$mv 1356652700.file2.dat.bz2.cor 1356652700.file2.dat.bz2
$mv 1356652700.file3.dat.bz2.cor 1356652700.file3.dat.bz2
$bzip2 -dc 1356652700.file1.dat.bz2 > 1356652700.file1.dat
$bzip2 -dc 1356652700.file2.dat.bz2 > 1356652700.file2.dat
$bzip2 -dc 1356652700.file3.dat.bz2 > 1356652700.file3.dat
$rm *.bz2
$mv 1356652700.file1.dat tab1.dat
$mv 1356652700.file2.dat tab2.dat
$mv 1356652700.file2.dat tab3.dat
$./my_cron.cron
$sqlplus mylogin/mypwd@myserver.com/SOMETHING <<ENDOFSQL
create or replace procedure my_prod as
begin
  declare
    db_package.db_proc1('1356652700.file1.dat');
    db_package.db_proc2('1356652700.file2.dat');
    db_package.db_proc2('1356652700.file3.dat');
  end;
end load_con;
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL
0
Comment
Question by:toooki
[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
  • 5
  • 2
  • +1
13 Comments
 
LVL 23

Expert Comment

by:David
ID: 38730078
"automate the script" -- what exactly do you expect us to do for you -- what do you think this should do in the end?
0
 
LVL 79

Expert Comment

by:arnold
ID: 38730116
Why not create the files with the correct naming convention in the first place?

not clear what this script is supposed to do or why you feel the need to rename the file when you are decompressing it.

i.e. bzip2 -cd < filename.whateverextentionyouwant > file.dat
it will be decompressed as long as it was created/compressed with bzip2.with
0
 

Author Comment

by:toooki
ID: 38730158
Thanks a lot.
I want to write a UNIX shell script that will do these:
1. If the file name like '*file1.dat.bz2.cor' then unzip/rename it to tab1.dat
likewise:
2. If the file name like '*file2.dat.bz2.cor' then unzip/rename it to tab2.dat
3. If the file name like '*file3.dat.bz2.cor' then unzip/rename it to tab3.dat

Then I have a pre-written different script ($./my_cron.cron) that expects the files  tab1.dat,  tab2.dat and  tab2.dat created above in the directory. I can take care of the rest part. Except How could I extract the filename's timestamp part (1356652700) so that I could pass that value to the rest part?
Thank you.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 79

Expert Comment

by:arnold
ID: 38730183
You are providing information halfway through the process.
What creates these files in the first place?

It might be possible to chain the process together.

are the three files the only ones in the process directory?
On one line you rename timestamp.filex.dat to tabx.dat
your run your my_cron process, then in your sqlplus script you reference a file you moved/renamed  earlier.

try the following
cd /var/www/process
ls | while read a; do
echo $a
done

you can use this loop to check whether $a has the filename convention you are looking for and then act accordingly

pattern matching example
http://stackoverflow.com/questions/3643436/bash-script-to-extract-all-matches-of-a-regex-pattern
0
 
LVL 84

Accepted Solution

by:
ozo earned 1200 total points
ID: 38730188
#!/bin/sh
DIR1=/var/www/temp1/
DIR2=/var/www/process/

cd $DIR2
for f in *.file1.dat.bz2.cor ;  do
b=`basename $f .file1.dat.bz2.cor`
mv $b.file1.dat.bz2.cor $b.file1.dat.bz2
mv $b.file2.dat.bz2.cor $b.file2.dat.bz2
mv $b.file3.dat.bz2.cor $b.file3.dat.bz2
bzip2 -dc $b.file1.dat.bz2 > $b.file1.dat
bzip2 -dc $b.file2.dat.bz2 > $b.file2.dat
bzip2 -dc $b.file3.dat.bz2 > $b.file3.dat
rm *.bz2
mv $b.file1.dat tab1.dat
mv $b.file2.dat tab2.dat
mv $b.file2.dat tab3.dat
./my_cron.cron
sqlplus mylogin/mypwd@myserver.com/SOMETHING <<EOF
ENDOFSQL
create or replace procedure my_prod as
begin
  declare
    db_package.db_proc1('$b.file1.dat');
    db_package.db_proc2('$b.file2.dat');
    db_package.db_proc2('$b.file3.dat');
  end;
end load_con;
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL
EOF
0
 

Author Comment

by:toooki
ID: 38731594
Thanks a lot to all.

I could automate unzip/rename using ozo's script. But the sqlplus command is not working.
I found way to get my work done without creating the procedure  my_prod.

So I am now trying:

./my_cron.cron
sqlplus mylogin/mypwd@myserver.com/SOMETHING <<EOF
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL
sqlplus mylogin2/mypwd2@myserver2.com/SOMETHING <<EOF
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL

In the above script the first part works. (exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);). The job actually starts after the script is run. But the second one does not work.
(exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);)

It seems to be some mixup with exit; ENDOFSQL syntax or so.

Thank you.
0
 
LVL 79

Expert Comment

by:arnold
ID: 38731636
ozo's script based on lack of information from you includes errors.
One particular deals with creating a loop where one is unneeded. The other is not closing the for loop (done is missing)
i.e. if there are four files, the loop when closed will run four times, three more than necessary.
do you know what creates these files? Is that an automated process?

your script can be modified

#!/bin/sh
DIR1=/var/www/temp1/
DIR2=/var/www/process/

if [ $# -ne '1' ] ; then
     echo "Usage $0 <filename>";
     exit;
elsif [ ! -e "$DIR2/$1" ]; then
     echo "The filename you provided $1 does not exist in $DIR2" 
     exit;
fi
filename="$DIR2/$1"
outputfile="file1.dat"
Now I manually run:
$cd $DIR2
bzip2 -cd < $filename > $outputfile
$./my_cron.cron

$sqlplus mylogin/mypwd@myserver.com/SOMETHING <<ENDOFSQL
create or replace procedure my_prod as
begin
  declare
    db_package.db_proc1($filename);
  end;
end load_con;
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL 

Open in new window


You can add a two file requirement i.e.
command file1 outputfile
This way your script will be passed timestamp.file1.bz2.cor file1.dat on the first pass.

You can also use the command to define which proc will run if they exist.

to this day, I have no idea what the purpose of the move to file1.dat is or whether your sqlplus is what generates these files when the schedule is executed?
0
 

Author Comment

by:toooki
ID: 38731688
Thank you.
This is the actual script that worked for me:
#!/bin/sh
DIR=/var/temp
cd $DIR
#/bin/cp $DIR/backup/*.cor $DIR/
for f in *.file1.dat.bz2.cor ;  do
b=`basename $f .file1.dat.bz2.cor`
mv $b.file1.dat.bz2.cor $b.file1.dat.bz2
bzip2 -dc $b.file1.dat.bz2 > $b.file1.dat
/bin/mv $b.file1.dat.bz2 $DIR/processed/ >/dev/null 2>&1
/bin/mv $b.file1.dat tab1.dat >/dev/null 2>&1
done

I write the above "for done" for 4 file types.
Now the problem is with this part of the script:

sqlplus mylogin/mypwd@myserver.com/SOMETHING <<EOF
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL
sqlplus mylogin2/mypwd2@myserver2.com/SOMETHING <<EOF
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL

The first part (sqlplus command) works but the second part does not. If I write this way:
sqlplus mylogin2/mypwd2@myserver2.com/SOMETHING <<EOF
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL
sqlplus mylogin/mypwd@myserver.com/SOMETHING <<EOF
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL

Then the first part works. Seems some syntax issue with the exit;
ENDOFSQL command.
Thank you.
0
 
LVL 79

Assisted Solution

by:arnold
arnold earned 800 total points
ID: 38731768
You have EOF and ENDSQL mixture.

You have to match the <<tag
tag
0
 
LVL 84

Expert Comment

by:ozo
ID: 38731771
Sorry, <<EOF should have been <<ENDOFSQL
0
 

Author Comment

by:toooki
ID: 38731780
Thank you. I tried with this:

sqlplus mylogin/mypwd@myserver.com/SOMETHING <<ENDOFSQL
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL
sqlplus mylogin2/mypwd2@myserver2.com/SOMETHING <<ENDOFSQL
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL

But it did not work either (it started the first scheduled job but not the second one).
Thanks.
0
 
LVL 79

Expert Comment

by:arnold
ID: 38731790
You are asking about something not working at least myself who has no information of your setup,
Does the oratab where this script is being run has connection info for serv2, server3?

Can you run the sqlplus to server2?


Usually when running sqlplus, you have to define an instance that sqlplus will reference when starting.
0
 

Author Comment

by:toooki
ID: 38733399
Thank you. arnold, your comment helped, I had EOF and ENDSQL mixture. And I was not actually well aware of the tag usage. I updated the script this way and it worked:

sqlplus mylogin/mypwd@myserver.com/SOMETHING <<ENDOFSQL1
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL1
sqlplus mylogin2/mypwd2@myserver2.com/SOMETHING <<ENDOFSQL2
exec DBMS_SCHEDULER.RUN_JOB('MY_JOB', FALSE);
exit;
ENDOFSQL2

Thank you all very much.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

764 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