Solved

How can I automate this UNIX script

Posted on 2012-12-29
13
691 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
  • 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 76

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

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 300 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 76

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 76

Assisted Solution

by:arnold
arnold earned 200 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 76

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

760 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

20 Experts available now in Live!

Get 1:1 Help Now