ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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
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.
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
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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 ?
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.