1833750 Members
2448 Online
110063 Solutions
New Discussion

Environment Variables

 
SOLVED
Go to solution
Rajkumar_3
Regular Advisor

Environment Variables

Hai all,

I have attached a complete document of my requirement regarding the shell script and environment variables.

Its urjent..Please provide me a help..

Thanking you

Rajkumar
Oracle DBA
10 REPLIES 10
Eugen Cocalea
Respected Contributor

Re: Environment Variables

find . -type d -exec <script> {} \;

and modify the script so it gets the directory as an argument.

aha, and make sure you are on the top directory of the directory structure that you want to clean.
To Live Is To Learn
Thierry Poels_1
Honored Contributor

Re: Environment Variables

Hi,

isn't it sufficient for you to run just this part of the script (tune the location to your needs):


COUNT=0

find /Database/J??/* -name "X_*" -mtime +3 | while read FILE ; do
rm $FILE && logger "$0 : $FILE deleted"
(( COUNT = COUNT + 1 ))
done

if [ "$COUNT" -gt 0 ] ; then
logger "$0 : $COUNT files removed"
else
logger "$0 : No matching files found"
fi


regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Rajkumar_3
Regular Advisor

Re: Environment Variables

Hai,

I didnt understand what you are telling me..

My query is to delete the files from the multiple directories..Can you give me complete picture

Thanks
Rajkumar
Oracle DBA
Rajkumar_3
Regular Advisor

Re: Environment Variables

Hai Thierry,

That means it will search all the files from the database directory untill this files starting with X_ founds. Is that true???

I am trying out with your script..

Thanks
Rajkumar
Oracle DBA
Thierry Poels_1
Honored Contributor

Re: Environment Variables

hi Rajkumar,

find /Database/.... -name "X_*" ....
will give you *ALL* files starting with "X_" in /Database and ALL its subdirectories. You do not have to specify directory per directory, unless you want to skip some specific directories.
BTW it's always a good idea to test those find&delete scripts first with find&display ;)

good luck,
Thierry
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Robin Wakefield
Honored Contributor
Solution

Re: Environment Variables

Hi Rajkumar,

Personally, if you want to control the directories externally, I'd put them into a file, and read the file in the script:

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

cat file_containing_list_of_directories | while read DIR ; do

if [ ! -d "$DIR" ] ; then
logger "$0 : $DIR not found"
exit 1
fi

COUNT=0

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

if [ "$COUNT" -gt 0 ] ; then
logger "$0 : $DIR - $COUNT files removed"
else
logger "$0 : No matching files found"
fi

done

exit 0
===========================================

Then you can leave the cronjob as it is, and edit the external file to suit.

Rgds, Robin.
Eugen Cocalea
Respected Contributor

Re: Environment Variables

Hi,

Please provide more input. Are there any other directories under /Database that you don't want to touch? Do you only want to erase from the specific directories you have in the file? I believe this questions you can answer, right?

E.
To Live Is To Learn
Rajkumar_3
Regular Advisor

Re: Environment Variables

Hai,

Yes . I have a standard directories which i have to delete..But some times the directories may change..At that time the script dosent workout,because we are declaring the Environment variable and passing it into the script..I have also input parameters.Suppose i have a file which was created on 1st 09:00 AM..So this file has to be deleted for every 3 days and it should be depend on time and date also..
That means Preservation period : (3 days to delete a file). If they want to change it to 4 days ..

Please advise me.

Thanks & Regards
Rajkumar

Oracle DBA
Sridhar Bhaskarla
Honored Contributor

Re: Environment Variables

Rajkumar,

You can use the same script to achive what you need.

You can provide inputs to this script and it works as well with some modifications.

#!/usr/bin/ksh

if[ $# -ne 3 ]
then
echo "Usage: $0 directory expiry match"
exit 1
fi

DIR=${X:-$1}
EXPIRE=${E:-$2}
MATCH=${M:-$3}

DIRs=`ll -d $DIR`

for I in $DIRs
do
if [ -d $I ]
then
echo $I >> /tmp/valid$$
fi
done

TOTALDIR=`wc -l /tmp/valid$$|awk '{print $1}'`

if [ $TOTALDIR -eq 0 ]
then
logger "$0: No valid directories found"
exit 1
else
logger "$0: $TOTALDIR number of dirs found.. deleting"
fi

for I in `cat /tmp/valid$$`
do
COUNT=0
find $I -name $MATCH -mtime $EXPIRE|while read FILE
do
rm $FILE && logger "$0 : $FILE deleted"
COUNT=$(expr $COUNT+1)
done
logger "$0: deleted $COUNT files in $I"
done

rm /tmp/valid$$

exit 0

//END of the script

Now this script will take three arguments

#input.ksh directory expiry match_string

For ex.,

#input.sh /database/X* 4 X_*

will delete all the files under /database/X* directories that haven't been modified since 4days that match the string X_*.

I suggest you use this script on some temporary directories and see if it works for permitations and combinations of inputs. I don't have a system in front of me. It doesn't do error checking. For ex., input.ksh test test new will give out errors and may also do damage. So, you need to be careful. To do error checking is a big business.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Rajkumar_3
Regular Advisor

Re: Environment Variables

Hai
Once again

I need to pass two parameters:
a) The files stating with "X_" and
b) Retained time for deleting the renamed file (or) Retained time for deleting the non-renamed file.
(ie.,Time based)
Note: That means users will FTP from the remote server to the local server.So once the file is in the local server it will be renamed to "X_".

so If i pass the paramters like
variable1= X_ and time=1 (ie 1 hour)

It has to check the two conditions and file has to be deleted.

Is it possible like that to develop a shell script like that..

Regards
Rajkumar
Oracle DBA