1830939 Members
2746 Online
110017 Solutions
New Discussion

Re: Housekeeping Scripts

 
Rajkumar_3
Regular Advisor

Housekeeping Scripts

Hai All,

I have to write a script which has to collect the directory names from the two columns from the table and that files has to be deleted for very 3 days starting with the name "X_"

Can any one help about this..

Thankingyou
Rajkumar
Oracle DBA
9 REPLIES 9
Robin Wakefield
Honored Contributor

Re: Housekeeping Scripts

Hi,

Please provide an example of the table.

Rgds, Robin.
Rajkumar_3
Regular Advisor

Re: Housekeeping Scripts

Hai Robin,

Thank you for your reply...

My table structure is like this..

create table CARD(card_id varchar2(10),
In_dir varchar2(255),
out_dir varchar2(255))

In the columns "In_dir" & "out_dir" users will store nearly upto 10 directory names.

For Example:
---------------
In_dir : /test_in1/oracle
: /test_in2/oracle
: /test_in3/oracle......upto /test10/oracle

Out_dir : /test_out1/oracle
: /test_out2/oracle
: /test_out3/oracle......upto /test10/oracle

So my script has to read the paths from the directories from the 2 columns and delete the files starting with "X_" for every 3 days only....After deleting the files it has to be written to the syslog file about the status of those files whether it was successfull or not...

Ok is it possible to read from the Database table.
If possible how can we select the path names from the CARD table & delete the files using Shell Script in HP-Unix 11.0


Thanks for the advance in help..

Regards
Rajkumar
Oracle DBA
Robin Wakefield
Honored Contributor

Re: Housekeeping Scripts

Hi,

Sorry, one more question. Does this table exist as a text file, and if so, can you attach an example. I need to know if each line only contains 3 entries, or if there are multiple directories listed per line.

Thanks, Robin.
Rajkumar_3
Regular Advisor

Re: Housekeeping Scripts

Hai Robin,

I will give the complete picture
If i issue the select command the output will look like this....

SQL>select * from CARD;
card_id In_dir Out_dir
--------- --------- ----------
1d /test_in1/oracle /test_out1/oracle
2d /test_in1/oracle /test_out1/oracle
3d /test_in1/oracle /test_out1/oracle
4d /test_in1/oracle /test_out1/oracle
5d /test_in1/oracle /test_out1/oracle
6d /test_in1/oracle /test_out1/oracle
7d /test_in1/oracle /test_out1/oracle
8d /test_in1/oracle /test_out1/oracle
9d /test_in1/oracle /test_out1/oracle
10d /test_in1/oracle /test_out1/oracle

1)So the script has to pick up the directory names of the 2 columns from this table.
2)after that the shell script has to search for that directories path from the home directory of the system.
3)And it has to delete the files starting with "X_"

Is it possible to pickup from Oracle database table??

Thanks & Regards
Rajkumar
Oracle DBA
Robin Wakefield
Honored Contributor

Re: Housekeeping Scripts

Hi,

Something like this should do what you want:

cat tablename |grep oracle | while read cardid dir1 dir2 ; do
find $dir1 $dir2 -mtime +3 -name "X_*" | xargs rm
done

To check it is going to delete what you want, change the "xargs rm" to "xargs ls -ld"

I am not familiar with Oracle, so I can't answer your last question.

Rgds, Robin.
Volker Borowski
Honored Contributor

Re: Housekeeping Scripts

Rajkumar,

there are several things that might be difficult with this structure:

- you need to make sure, that SQLPLUS sets the CHARWIDTH to maximum, to avoid that it clips needed information
- you need to parse for ":" to delimit.

if you want to stay with this structure, I would recommend to parse the SQLPLUS-output with awk, using ":" as a delimiter, but I would favor the following:

- add a serialnumber to the key
- if needed add an in/out attribute field
- just have one directory column

this would make a table like this:

id, ser, type, dir

1d, 1, 'i', /test_in1/oracle
1d, 2, 'i', /test_in2/oracle
1d, 3, 'o', /test_out1/oracle

And now, you can do a fine selection:

select 'rm ' || dir || '/X_*' from table where ... (whatever you want, id=, type= and so on)

You might need to set some SQLPLUS options to get rid of headings and so, but as a result, you could spool the output and run it later.

Hope this helps
Volker
Volker Borowski
Honored Contributor

Re: Housekeeping Scripts

It is me once more :-)

I just came across your other thread, and now I imagine, what you want to achieve.

I would change my recomendation, and modify it the following way:

Have an additional status column in that table, which can be tagged as "obsolete" by the application, if it has REALLY processed the file. I never like to have some time based restrictions on these type of interfaces, because some day (likely to be when some public holiday are grouped around a weekend) what intended to work will suddely do not, and maybe cause damage.

Interfaces should work with handshakes, not with hope, that something is already processed :-)

So if the application tags your record in this table as "yes, I processed it", your job can delete it for sure. You can then create your spooled delete script with "where status='processed'".

Just my 0.02?
Volker
Jayaprakash_1
Advisor

Re: Housekeeping Scripts

Hi raj
I assumed your in_directory1 column has the name which has to be removed.

cat > s.sql
set head off
set feedback off
spool /tmp/x
select in_dir from card where in_dir like '/test_in/oracle/X_%';
spool off

cd /test_in/oracle
sed 's\/test_in\/oracle//' /tmp/x.lst>/tmp/x1.lst
for i in `cat /tmp/x1.lst`
do
rm i
done

Please repeat the same for out_dir

Regds
jp
jp
Rajkumar_3
Regular Advisor

Re: Housekeeping Scripts

Hai All,

Thank you for your replies..I have tried with all your options but to some extent i have reached..but here I am not deleting any columns in the table..So i have the altenative way like this..

Once again i am repeating my query...

1) In the /app/sales directory i have so many files starting with the Prefix " X_filename.extension "

In this directory daily nearly upto 100 files will be generated using some other application...

So now i want to clean up only those files which starts with the letter "X_" for every 3 days and the message should be written to syslog.log in the directory /var/adm/syslog using the LOGGER command

Now i want to write a shell script using this method..

1) I have set an Enviromnent variable in HP-Unix11.0 like..

#env_dir=/app/sales

2) Now i want to write a shell script using the Environment variable "env_dir" . Because in this variable i have /app/sales directory..

3) so finally my scripts has to delete the files as said above using the variable " env_dir "..And in this directory i have so many files which starts with "X_"

Can you please provide me a script.....

Thanks for the advance in help..

Regards
Rajkumar
Oracle DBA