Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-19-2004, 01:48 AM
|
#1
|
|
LQ Newbie
Registered: Nov 2004
Location: Philippines
Distribution: Red Hat Advanced Server
Posts: 16
Rep:
|
bash script to delete files
Hi to all. I've read the posts about script programming but i haven't found anything similar to my problem. I need a script to delete files but the files in a certain directory but before I delete those files, I need to check something from an oracle database and save it to a txt file. I already figured out the algorithm but I don't know the syntax and if its possible to put it in a script.
1. Query from the database and save the result in a txt file. (result.txt). The output of the query would be something like this:
file1_1_91000.log.
2. Read from the result.txt file and get the sequence number of the result (91000). I was hoping to use substr to get only 91000 from the original value file1_1_91000.log
3. From the result, I would be deleting all files in a directory whose filename is
less than the result.txt (file1_1_91000).
Hoping that someone could help me. Thanks in advance.
|
|
|
|
11-19-2004, 04:31 AM
|
#2
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
Try this:
Create some dummy files to test with:
Code:
bash$ for i in $(seq 10 200) ; do touch /tmp/file1_1_${i}.log ; done
bash$ for i in $(seq 10000 10200) ; do touch /tmp/file1_1_${i}.log ; done
bash$ for i in $(seq 90000 90200) ; do touch /tmp/file1_1_${i}.log ; done
bash$ for i in $(seq 91000 91200) ; do touch /tmp/file1_1_${i}.log ; done
Script:
Code:
#!/bin/bash
# Directory to delete file from
#
DIR="/tmp"
# Prefix for the files to delete
#
PREFIX="file1_1_"
# Get te query result
RESULT=$(cat result.txt)
# Extract the number from it.
NUMBER=${RESULT##*_}
NUMBER=${NUMBER%.log}
# Loop for files in DIR starting with PREFIX, ending with '.log':
for FILE in ${DIR}/${PREFIX}*.log ; do
# Extract the number from this file
NR=${FILE##*_}
NR=${NR%.log}
# If this NR is less than NUMBER: delete
if [ "$NR" -lt "$NUMBER" ] ; then
echo rm $FILE # Remove "echo" to really do the delete.
fi
done
|
|
|
|
11-19-2004, 04:38 AM
|
#3
|
|
Member
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430
Rep:
|
Filename=`tail -n 1 result.txt` #Hope the filename gets appended at end of result.txt
Length=${Filename##*_}
Length=${Length%%.*}
for (( i = 0 ; i < $Length ; i++ ))
do
if [ -f file1_1_"$i".log ]
then
## Before trying it out use echo instead of rm to verify if files names are OK
rm file1_1_"$i".log
fi
done
Cheers
Z
|
|
|
|
11-19-2004, 05:58 AM
|
#4
|
|
LQ Newbie
Registered: Nov 2004
Location: Philippines
Distribution: Red Hat Advanced Server
Posts: 16
Original Poster
Rep:
|
Thanks Hko and zulfilee for your replies, I've both tried your code and it worked. I adopted the code of Hko because I can understand it a little. I'll try to study harder to code in bash script. Thanks a lot.
|
|
|
|
12-03-2004, 09:46 AM
|
#5
|
|
LQ Newbie
Registered: Nov 2004
Location: Philippines
Distribution: Red Hat Advanced Server
Posts: 16
Original Poster
Rep:
|
bash script to delete files (Solaris)
Guys, I need help on this script. I would like to ask why this script won't run automatically usring crontab in Solaris,when I run it manually,there is no problem,but when I run it in crontab,it does not execute? What should be done? Please advice. Thanks in advance.
P.S. Got this coe from Hko.
#!/bin/bash
# Directory to delete file from
#
DIR="/tmp"
# Prefix for the files to delete
#
PREFIX="file1_1_"
# Get te query result
RESULT=$(cat result.txt)
# Extract the number from it.
NUMBER=${RESULT##*_}
NUMBER=${NUMBER%.log}
# Loop for files in DIR starting with PREFIX, ending with '.log':
for FILE in ${DIR}/${PREFIX}*.log ; do
# Extract the number from this file
NR=${FILE##*_}
NR=${NR%.log}
# If this NR is less than NUMBER: delete
if [ "$NR" -lt "$NUMBER" ] ; then
echo rm $FILE # Remove "echo" to really do the delete.
fi
done
|
|
|
|
12-03-2004, 06:45 PM
|
#6
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
I see no initial cd in your script.
My guess is cron is not executing in the directory you expect him to do.
|
|
|
|
12-04-2004, 06:49 AM
|
#7
|
|
LQ Newbie
Registered: Nov 2004
Location: Philippines
Distribution: Red Hat Advanced Server
Posts: 16
Original Poster
Rep:
|
jlliagre,
I already tried to add cd in the initial location but still to no effect. is the bash environment in Linux the same with Solaris 8?
|
|
|
|
12-04-2004, 08:00 AM
|
#8
|
|
Member
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430
Rep:
|
I ve got a few things to say
First the script you have given has an 'echo' and not 'rm'
So it will not echo when its run through crontab [crontab doesnt run on a terminal]. When run manually however you get the files list as you are running it on a terminal.
Second check if the file has exec permission.
Third kindly post the crontab entry you have put
Cheers
Z
|
|
|
|
12-04-2004, 03:41 PM
|
#9
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OL
Posts: 9,311
|
Quote:
|
is the bash environment in Linux the same with Solaris 8?
|
Not the same, but similar enough for your script to work the same.
Quote:
|
So it will not echo when its run through crontab [crontab doesnt run on a terminal].
|
It won't indeed echo on the terminal, but the job output (both stdout and stderr), if any, is mailed to the associated user. So c0d3, have you checked your local mail on this machine ?
|
|
|
|
12-05-2004, 10:45 PM
|
#10
|
|
LQ Newbie
Registered: Nov 2004
Location: Philippines
Distribution: Red Hat Advanced Server
Posts: 16
Original Poster
Rep:
|
Guys,I finally make it to work automatically on Solaris. I just debugged the script line per line and found out that it would work if I removed some characters. First, I removed the "${}" characters in my variables and still worked fine in Solaris, second I changed the ${} with "`" in the cat command program. I dont know what's the explaination why it worked when I changed it. By the way, the echo command was not removed because I am making the test in our production servers. I also made sure that I have execute permission on the file. my crontab entry looks like this: "30 9 * * * sh /log_data/scripts/deletefile.sh" Thanks for all the replies and support...
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:56 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|