- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell Scripts
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2001 11:32 PM
10-14-2001 11:32 PM
I created a directory called /apps ..In this directory so many files will be created daily..
So i need to clean up only those files which starts with the letter "X_" in the /apps
Now i want to write a shell script using environment variable...
For Eg:
-------
1)#x=/apps
2)#vi del_file.sh
If $x then
else
endif;
3)So finally this script will be called using the CRONJOB.So it will automatically calls the DEL_FILE.sh script to delete the files..
Condition:
----------
a) every file has to be deleted for every 3 days starting with the prefix " X_ ",
b) And the status has to be written to syslog file automatically using the LOGGER Command...
If i declare an environment variable like... #x=/apps
That variable " x " has should be used in the shell script to satisfy the conditions..
Can any one can provide me a scipts ,,,Its urgent please...
Thanks & Regards
Rajkumar
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2001 11:56 PM
10-14-2001 11:56 PM
Re: Shell Scripts
Something like this should do. This accepts either an argument or, if set and exported, the variable $X.
==============================================
#!/bin/ksh
DIR=${X:-$1}
if [ -z "$DIR" ] ; then
logger "$0 : X not set, and no argument given"
exit 1
fi
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 : $FILE deleted"
COUNT=$(expr $COUNT + 1)
done
if [ "$COUNT" -gt 0 ] ; then
logger "$0 : $COUNT files removed"
else
logger "$0 : No matching files found"
fi
exit 0
=============================================
Just create this script, chmod it, and put it into cron to run, say, at 3:00 every morning, e.g.:
0 3 * * * /usr/local/bin/del_file.sh /apps
Rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2001 11:59 PM
10-14-2001 11:59 PM
Re: Shell Scripts
check out this..
find /apps -name X_* >>/tmp/filesX
a=`cat /tmp/fileX`
for a in $a
do
/usr/bin/rm $a
done
The file list which the scipts deletes remains in /tmp/fileX
Hope this helps.
Cheers !!!
Mathew
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 12:15 AM
10-15-2001 12:15 AM
Re: Shell Scripts
Thank you for your reply...
That means what i understood is if i declare the Environment variable X=/apps and exported it..
and then if mention this script name in the CRONJOB it will automattically it will delete the files starting with the name X_* in the /apps directory ???
Is my understanding is correct ????
Thanks & Regards
Rajkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 12:45 AM
10-15-2001 12:45 AM
Re: Shell Scripts
Thank you for your reply...
I think yours script will run because you have hardcoded the directory name in the script where the files has to be deleted..But what i want is The script has to collect the directory name from the Environment variable and delete the files accordingly...
Suppose the user may change the directory name then your script dosent work...
If the user wants to change the directory he will change it in the Environment variable not in the script....
So we have to give that compatibility..
Thanks & Regards
Rajkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 12:48 AM
10-15-2001 12:48 AM
Re: Shell Scripts
You'll have to either put the directory on the command line in cron, as shown in my previous reply, or source an external file and edit this accordingly.
So near the top of the script add something like:
. ~/.dirfile
and in this file you'll have:
X=/apps
Rgds, Robin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 12:56 AM
10-15-2001 12:56 AM
Re: Shell Scripts
I think you want to have a generic script that takes the directory (ex /apps) and cleanout the files that startwith X_.
#clean_it /apps
#!/usr/bin/ksh
if [ $# -ne 1 ]
then
echo "Usage:$0 directory"
exit 1
fi
DIR=$1
if [ !-d $DIR ]
logger "$0: No such directory"
exit 1
else
logger "$0 : `date` : started deletion"
fi
printdelete()
{
logger "$0: $1 is deleted"
}
find $DIR -name "X_*" -mtime +3 >> /tmp/list$$
NUM=`wc -l /tmp/list$$|awk '{print $1}'`
for FILE in `cat /tmp/list$$`
do
rm $FILE
printdelete $FILE
done
logger "$0: `date` : End deletion of $NUM files"
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 01:04 AM
10-15-2001 01:04 AM
Re: Shell Scripts
Thank you very much.
That means i have add this " . ~/.dirfile " at the beginning of the file..
If i have to immeditely test the script whether its working or not??? How can I please.. ??
Thanks & Regards
Rajkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 01:11 AM
10-15-2001 01:11 AM
Re: Shell Scripts
Can you please explain me about your script step by step??
Thanks & Regards
Rajkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 01:20 AM
10-15-2001 01:20 AM
Re: Shell Scripts
You could run it with an at job:
at now
your_script
Create yourself another window in which you tail the syslog:
tail -f /var/adm/syslog/syslog.log
You may want to substitute "echo" for "rm" while you are testing it, just in case.
Rgds, Robin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 01:37 AM
10-15-2001 01:37 AM
Re: Shell Scripts
I created the 2 files(X_file1.dat, X_file2.dat) in the /apps directory..
I have set the environment variable X=/apps and i exported it #export X
After that i run the script its displaying an error in the syslog file saying that no matching files found..
What could be the problem???
Thanks & Regards
Rajkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 01:44 AM
10-15-2001 01:44 AM
Re: Shell Scripts
So the better way to do it would be to set up the cron job as follows:
0 3 * * * /usr/local/bin/del_file.sh /apps
And of course you can test it out using at, i.e.
at now < /usr/local/bin/del_file.sh
Hope this helps.
-Santosh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 01:51 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 01:56 AM
10-15-2001 01:56 AM
Re: Shell Scripts
The first if function checks if there is any argument passed to the script (should be like clean_it /apps) otherwise will give out syntax error.
Next the argument ($1) is assigned to the variable DIR (here it is /apps)
Next if condition checks if this directory is existing or not. If not, printouts the error otherwise will start logging the deletion.
printdelete is a function that just logs into the syslog taking an argument. For ex., printdelete X_121212 will log "clean_it : X_121212 deleted"
Find command gets a list of all files taht start with X_ that haven't been modified since 3days. We take the number of such files using wc and assign it to NUM.
For loop deletes all the files in the list calling the function printdelete for each file.
At the end, we log again the completion with date and the number of files.
YOu just need to add one more step at the end
rm /tmp/list$$
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 02:15 AM
10-15-2001 02:15 AM
Re: Shell Scripts
Instead of removing the -mtime +s in the script is there any thing that we can create files using
" touch " command using exactly 3 days before date to delete the files now??
Thanks & Regards
Rajkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 04:13 AM
10-15-2001 04:13 AM
Re: Shell Scripts
I am able to run your script successfully..I eliminated the -mtime +3 and also created the files in the /apps directory using the touch command..With this command i created 10 files on 12th date.. Now Its working fine..
Thank you very much Robin...
Thank you very much all of you for helping me...
Thanking you
Regards
Rajkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2001 11:32 PM
10-15-2001 11:32 PM
Re: Shell Scripts
I have posted a old query with the new requirement please review..
Thanking you
Rajkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2001 12:27 AM
10-16-2001 12:27 AM
Re: Shell Scripts
find /app -name x_* |xargs rm
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2001 04:07 AM
10-17-2001 04:07 AM
Re: Shell Scripts
here we are using " -mtime +3 " to delete the file for every 3 days..
suppose If i want to delete the files "OLDER THAN 3 DAYS"
what could be the option ???
Thanks & Regards
Rajkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2001 04:50 AM
10-17-2001 04:50 AM
Re: Shell Scripts
"-mtime +3" DOES mean delete if older than 3 days!! Are you saying that you want cron to run your script every 3 days?
Rgds, Robin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2001 12:55 AM
10-19-2001 12:55 AM
Re: Shell Scripts
You are correct.It is for every 3 days..I have another scripts which has to be read from the oracle tables...I will post a new query soon..
Thank you all for helping me..
Thanks
Rajkumar