Link to home
Start Free TrialLog in
Avatar of toooki
toooki

asked on

How can I automate this UNIX script

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
Avatar of David VanZandt
David VanZandt
Flag of United States of America image

"automate the script" -- what exactly do you expect us to do for you -- what do you think this should do in the end?
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
Avatar of toooki
toooki

ASKER

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.
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
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of toooki

ASKER

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.
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?
Avatar of toooki

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, <<EOF should have been <<ENDOFSQL
Avatar of toooki

ASKER

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.
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.
Avatar of toooki

ASKER

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.