Operating System - HP-UX
1752574 Members
4415 Online
108788 Solutions
New Discussion юеВ

Re: Any help for this script appreciated

 
SOLVED
Go to solution
KRS_1
Frequent Advisor

Any help for this script appreciated

This the script to delete files older than 240 days.
#!/usr/bin/sh
find /tmp -mtime +240 -exec rm {} \;

Here i have to pass the days value interactively. please help me to solve this.

Rajesh
6 REPLIES 6
Devender Khatana
Honored Contributor
Solution

Re: Any help for this script appreciated

Hi Rajesh,

I am not an expert in scripting but it should work.
===========================================
#!/usr/bin/sh
read var1
echo $var1
find /tmp -mtime +$var1 -exec rm {} \;
exit
===========================================

HTH,
Devender
Impossible itself mentions "I m possible"
Devesh Pant_1
Esteemed Contributor

Re: Any help for this script appreciated

Rajesh,
I think what Devender has suggested should be fine. Since you have mentioned you want to pass the value interactively, here is a modification
#!/usr/bin/sh
echo "Please enter the number of days: \c"
read var1
echo "files older than $var1 days will be deleted "
find /tmp -mtime +$var1 -exec rm {} \;
exit

thanks
DP
Patrick Wallek
Honored Contributor

Re: Any help for this script appreciated

I would actually go one step further:

#!/usr/bin/sh

echo "Please enter the age of files you want to delete in days:"
read DAYS

find /tmp -mtime +${DAYS} -type f -exec ls -ld {} \;

echo "Are you sure you want to delete all of these files?"

read ANSWER

if [[ ${ANSWER} = "Y" -o ${ANSWER} = "y" ]] ; then
find /tmp -mtime +${DAYS} -type f -print -exec rm -f {} \;
fi


This script will first list the files you want to delete and then prompt you to make sure you want to delete them. If you answer with anything other than Y or y, then it won't delete the files.
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Any help for this script appreciated

It looks like you need to pass many values.
That's why you need to pass them interactively.

Just add a while loop in Patrick's script

echo "Continue"
read choice
While ($choice='y')
Patric's script
read choice
done
Vibhor Kumar Agarwal
Muthukumar_5
Honored Contributor

Re: Any help for this script appreciated

You can try by setting an environment variable as #export FINDDAY=240
#find /tmp -mtime +${FINDDAY} -exec rm {} \;

If you want to change it then,

# export FINDDAY=100

hth.

Easy to suggest when don't know about the problem!
A. Clay Stephenson
Acclaimed Contributor

Re: Any help for this script appreciated

My approach to this would not be asking the user or setting an environment variable but simply passing in the argument on the command line --- and like real UNIX applications, it assumes that the users knows what he is doing and doesn't ask confimation questions.

#!/usr/bin/sh

PROG=${0}
typeset -i STAT=255
if [[ ${#} -ge 1 ]]
then
typeset -i DAYS=${1}
shift
find /tmp -mtime +${DAYS} -exec rm \;
STAT=${?}
else
echo "Usage: ${PROG} requires 1 arg." >&2
fi
exit ${STAT}

You would use it like this:
myscript.sh 100

If you like, you could write another little script that prompts for the value and then pass this value into myscript.sh.

If it ain't broke, I can fix that.