Operating System - HP-UX
1837981 Members
1775 Online
110124 Solutions
New Discussion

Shell Scripts help..........

 
SOLVED
Go to solution
Rajkumar_3
Regular Advisor

Shell Scripts help..........

Hai all

I need a help to develop a shell script as per the Document Attached..

Thanks for the Advance in Help..

Regards
Raj..
Oracle DBA
20 REPLIES 20
Rajkumar_3
Regular Advisor

Re: Shell Scripts help..........

Hai ,

Any SysAdmins, please respond

Thanks

Raj
Oracle DBA
Robin Wakefield
Honored Contributor

Re: Shell Scripts help..........

Hi Raj,

How about:

=====================================
#!/bin/ksh

FILE1=/home/test/bacuss/bac1.log
FILE2=/home/test/bacuss/deletion.props

grep FILE_NAME $FILE2 | cut -d= -f2 | read FN
grep RETAIN_PEND $FILE2 | cut -d= -f2 | read RP
grep BACK_FILE_EXT $FILE2 | cut -d= -f2 | read BFE
grep RETAIN_SEND $FILE2 | cut -d= -f2 | read RS

cat $FILE1 | while read DIR ; do
find $DIR -name "*$FN" -mtime +$RP | xargs rm
find $DIR -name "*$BFE" -mtime +$RS | xargs rm
done
======================================

Rgds, Robin
Rajkumar_3
Regular Advisor

Re: Shell Scripts help..........

Hai Robin,

When i run the script which you have given to me ........ITS HANGING.....What could be the reason???

Thanks & Regards
Raj
Oracle DBA
Rajkumar_3
Regular Advisor

Re: Shell Scripts help..........

Hai Robin,

Its working now...
Is it possible to write the status to "SYSLOG" file using logger command??? I mean whether it is success or unsuccessfulll ???

Raj
Oracle DBA
Deepak Extross
Honored Contributor

Re: Shell Scripts help..........

Well, you could write a small executable in C, which would do an openlog(), syslog() and closelog().
And then call this exe from your script, passing to it the message to be logged.

HTH
Rajkumar_3
Regular Advisor

Re: Shell Scripts help..........

Hai Robin,

One more clarification reagarding script....

While executing the below script it has to delete only "DAT" files only.
It is deleting the "DAT" as well as "X_" files..Is is possible to read only specific whose files starting with "X_" ?????
==============================
#!/bin/ksh
FILE1=/home/test/bacuss/bac1.log
FILE2=/home/test/bacuss/deletion.props
grep FILE_NAME $FILE2 | cut -d= -f2 | read FN
grep RETAIN_PEND $FILE2 | cut -d= -f2 | read RP
cat $FILE1 | while read DIR ; do
find $DIR -name "*$FN" -mtime +$RP | xargs rm
done
==============================

Waiting for your reply..

R
Oracle DBA
Robin Wakefield
Honored Contributor

Re: Shell Scripts help..........

Raj,

If you change:

find $DIR -name "*$FN" -mtime +$RP | xargs rm

to

find $DIR -name "X_*$FN" -mtime +$RP | xargs rm

is that what you're after?

Rgds, Robin.
Rajkumar_3
Regular Advisor

Re: Shell Scripts help..........

Hai Robin,

I mean if i execute the above script it has to delete only "DAT" files but it is deleting "X_" files also...I dont want to delete the files starting with prefix "X_"
My requirement according to above script is I want to delete the files having the extention "DAT" only...

These are the files which i am having.....

Nov 6 09:06 X_file06.DAT
Nov 6 09:06 X_file06.DAT
Nov 7 09:05 X_file07.DAT
Nov 7 09:05 X_file07.DAT
Nov 8 09:04 X_file08.DAT
Nov 8 09:04 X_file08.DAT
Nov 9 09:03 X_file09.DAT
Nov 9 09:03 X_file09.DAT
Nov 10 09:02 X_file10.DAT
Nov 10 09:02 X_file10.DAT
Nov 11 09:01 X_file11.DAT
Nov 11 09:01 X_file11.DAT
Nov 6 09:00 file06.DAT
Nov 7 09:00 file07.DAT
Nov 8 09:00 file08.DAT
Nov 9 09:00 file09.DAT
Nov 10 09:00 file10.DAT

I mean the script has to ignore the files starting with "X_" and has to delete only extention "DAT" files..

Please help me...

Ra
Oracle DBA
Rajkumar_3
Regular Advisor

Re: Shell Scripts help..........

Hai Robin,

Once again

While deleting the files having the extension "DAT" it has to ignore the files starting with "X_"

In the script There must be some condition to eliminate the prefix "X_" files while deleting the files extension "DAT"

And "X_" & "DAT" should be read only from the parameter file "deletion.props".It should not be hardcoded in the script.....

Please kindly cooperate to solve the problem...

Raj
Oracle DBA
Robin Wakefield
Honored Contributor

Re: Shell Scripts help..........

Hi Raj,

A couple of ways:

find $DIR -name "*$FN" -mtime +$RP | grep -v X_ | xargs rm

or

find $DIR \( -name "*$FN" -a ! -name "X_*" \) -mtime +$RP | xargs rm

Rgds, Robin
Rajkumar_3
Regular Advisor

Re: Shell Scripts help..........

hai Robin,

I used what ever you have given ,still its showing all the "DAT" files along with "X_" files..

Raj
Oracle DBA
Robin Wakefield
Honored Contributor

Re: Shell Scripts help..........

Hi Raj,

You may need to put "-print" at the end of the find command.

Can you post the latest version of all your files please?

Rgds, Robin.
Rajkumar_3
Regular Advisor

Re: Shell Scripts help..........

Hai Robin,

I have attached all the script s..Please review...

Thanking you..

Raj
Oracle DBA
Robin Wakefield
Honored Contributor

Re: Shell Scripts help..........

Hi Raj,

I'd remove the "#!/bin/sh" from the top of bac1.log. This is causing the find to "fail" and the xargs to list all the files in the current directory (is this what you're seeing?).

Apart from that, I can see no problems.

Rgds, Robin.

Rajkumar_3
Regular Advisor

Re: Shell Scripts help..........

Hai robin,,

Fanastic,,Its working fine..

I have attached the scipts which i have executed independently..

Waiting for your responce..

Regards
Raj
Oracle DBA
Robin Wakefield
Honored Contributor
Solution

Re: Shell Scripts help..........

Hi Raj,

Try this:

FILE1=/home/rajkumar/in_dir/bac1.log
FILE2=/home/rajkumar/in_dir/deletion.props
grep FILE_NAME $FILE2 | cut -d= -f2 | read FN
grep RETAIN_PEND $FILE2 | cut -d= -f2 | read RP
grep BACK_FILE_EXT $FILE2 | cut -d= -f2 | read BFE
grep RETAIN_SEND $FILE2 | cut -d= -f2 | read RS

COUNT=0

cat $FILE1 | while read DIR ; do
if [ ! -d "$DIR" ] ; then
logger "$0 : $DIR not found"
continue
fi
find $DIR -name "*$FN" -mtime +$RP | grep -v $BFE | while read FILE ; do
rm $FILE && logger "$0 : for directory $DIR, $FILE deleted"
COUNT=$(expr $COUNT + 1)
done

find $DIR -name "$BFE*" -mtime +$RS | while read FILE ;do
rm $FILE && logger "$0 : for directory $DIR, $FILE deleted"
COUNT=$(expr $COUNT + 1)
done

if [ "$COUNT" -gt 0 ] ; then
logger "$0 : for directory $DIR, $COUNT files removed"
else
logger "$0 : for directory $DIR, no matching files found"
fi
done
exit 0

Rgds, Robin
Rajkumar_3
Regular Advisor

Re: Shell Scripts help..........

Hai Robin,

The message status of the deletion files only i am able to see..If there are no files there is no message status in the SYSLOG...
i mean files not found like that....

Can you please review???

Thanks
Raj
Oracle DBA
Rajkumar_3
Regular Advisor

Re: Shell Scripts help..........

Hai robin ,

I have attached the STATUS3.log file ..

Thanks
Raj
Oracle DBA
Robin Wakefield
Honored Contributor

Re: Shell Scripts help..........

Hi Raj,

I'm not sure I know what you mean - the find command will only find files that exist, so you shouldn't see "file not found".

You may want to move "COUNT=0" to after the first "while ...", so that it gets reset for each directory.

Please explain further if this is not correct.

Rgds, Robin.
Rajkumar_3
Regular Advisor

Re: Shell Scripts help..........

Hai Robin,

Exactly,You are correct...

Thank you for your support...

If any problem occurs i will post it in this FAQ only..

Regards
Rajkumar
Oracle DBA