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.
I'm creating a bash script to check how much free space is left in /var directory then, if it hits a certain threshold, delete certain files with numbers for extensions (e.g. fileA.1, fileA.2 fileA.3, and fileA.4, fileB.1, fileB.2 fileB.3, and fileB.4 ). Here's a snippet from my script:
if [ $PARTUSE -ge $ALERT ]; then
cat /dev/null > /var/log/fileA.* 2>&1 &&
cat /dev/null > /var/log/fileB.* 2>&1 &&
cat /dev/null > /var/log/fileC.* 2>&1 &&
echo "Reclaimed disk space on \"$PARTITION\" now ($PARTUSE% full) on $(hostname) as of $(date)" |
mail -s "Reclaimed disk space - $PARTITION on $(hostname)" $ADMIN
fi
If I use a * as a wildcard for the number extension, the script fails. Maybe regex would work here, but I'm not particularly accomplished at it. Or some other construct.
You cannot redirect the standard output to multiple files at once. You can use a loop to empty every single file, e.g.
Code:
for file in /var/log/fileA.? /var/log/fileB.? /var/log/fileC.?
do
> $file
done
Note you don't need cat /dev/null. In bash/ksh the redirection symbol alone creates/overwrite a file as if the null command was executed. Hope this helps.
Distribution: Ubuntu, Debian, Fedora, Oracle Linux
Posts: 109
Rep:
Quote:
Originally Posted by Diggy
Hello, all.
I'm creating a bash script to check how much free space is left in /var directory then, if it hits a certain threshold, delete certain files with numbers for extensions (e.g. fileA.1, fileA.2 fileA.3, and fileA.4, fileB.1, fileB.2 fileB.3, and fileB.4 ). Here's a snippet from my script:
if [ $PARTUSE -ge $ALERT ]; then
cat /dev/null > /var/log/fileA.* 2>&1 &&
cat /dev/null > /var/log/fileB.* 2>&1 &&
cat /dev/null > /var/log/fileC.* 2>&1 &&
echo "Reclaimed disk space on \"$PARTITION\" now ($PARTUSE% full) on $(hostname) as of $(date)" |
mail -s "Reclaimed disk space - $PARTITION on $(hostname)" $ADMIN
fi
If I use a * as a wildcard for the number extension, the script fails. Maybe regex would work here, but I'm not particularly accomplished at it. Or some other construct.
I used a poor example (apologies) in decribing what I want to do. I really want to delete files along the lines of Afile.1, Afile.2, Bfile.1, Bfile.2. Given that, How would I write the construct you suggested if, in fact, it would still work in this case?
Distribution: Ubuntu, Debian, Fedora, Oracle Linux
Posts: 109
Rep:
Quote:
Originally Posted by Diggy
colucix,
I used a poor example (apologies) in decribing what I want to do. I really want to delete files along the lines of Afile.1, Afile.2, Bfile.1, Bfile.2. Given that, How would I write the construct you suggested if, in fact, it would still work in this case?
Diggy
And why you don't just use a rm command for that?
Too risky?
May it's not so clear (to me!) the phrase
Quote:
I really want to delete files along the lines of Afile.1, Afile.2, Bfile.1, Bfile.2
I'm sure that your solution 1) works, and 2) is "elegant". I (ashamedly) just don't have a lot of scripting experience. That said, would my entire script look like this?:
#!/bin/bash
#
# Shell script to reclaim disk space
# It will clear out certain files if the percentage of partition space used is >= % set in ALERT variable,
# thus reducing partition size, and will send an email to the system administrator(s)
# -------------------------------------------------------------------------
# Written by Me
# -------------------------------------------------------------------------
#
# set admin email address(es)
ADMIN="root@localhost"
#
# set alert level at 90%. If used space exceeds this, send alert
ALERT=80
#
# check partition space and, if necessary, send alert
df -H | grep '/var' | awk '{ print $5 " " $6 }' | while read output;
do
PARTUSE=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
PARTITION=$(echo $output | awk '{ print $2 }' )
if [ $PARTUSE -ge $ALERT ]; then
for file in /var/log/Afile.? /var/log/Bfile.? /var/log/Bfile.?
do
> $file
done &&
echo "Reclaimed disk space on \"$PARTITION\" now ($PARTUSE% full) on $(hostname) as of $(date)" |
mail -s "Reclaimed disk space - $PARTITION on $(hostname)" $ADMIN
fi
done
Please bear with me, I'm learning, and this will be invaluable.
Please bear with me, I'm learning, and this will be invaluable.
Hey, no problem at all! I was just trying to clarify!
Regarding your script it looks good. I would avoid some redundancy, anyway. For example, to extract the usage of the /var partition you might use a more compact code. In place of:
df -P | awk '/\/var/{ sub(/%/,"",$5); print $5, $6 }' | while read PARTUSE PARTITION
do
Notes:
the -H option of df is not really necessary, since you're not intersted in the actual sizes but only in the percentage. Instead the -P option is useful, since it causes the df output to be in a more standard format. There are situations where the device name is very long and a line of output can be splitted in two parts. The -P option prevents this behaviour.
awk is a very powerful language that manages regular expressions, so that the grep command is not necessary. Moreover it can remove the % sign, so that you can avoid the cut command.
the read statement accept multiple variable names. The input will be splitted to fill all the specified variables, so that you can assign PARTUSE and PARTITION simultaneously.
Finally I don't see the need for a while loop, since you're reading a single line. To assign the output of a command using the read statement, process substitution is your friend:
What you suggest looks just fine, but I'm not sure how the final product would look. Based on my knowledge, and the suggestions in your last post, I'm not seeing where/how the files are being concatenated.
I understand that this piece:
df -P | awk '/\/var/{ sub(/%/,"",$5); print $5, $6 }' | while read PARTUSE PARTITION
do
It simply replaces the extraction of information from the df output and it does the assigment to PARTUSE and PARTITION in a different way, but the rest stays untouched:
Code:
#!/bin/bash
#
# Shell script to reclaim disk space
# It will clear out certain files if the percentage of partition space used is >= % set in ALERT variable,
# thus reducing partition size, and will send an email to the system administrator(s)
# -------------------------------------------------------------------------
# Written by Me
# -------------------------------------------------------------------------
#
# set admin email address(es)
ADMIN="root@localhost"
#
# set alert level at 90%. If used space exceeds this, send alert
ALERT=80
#
# check partition space and, if necessary, send alert
read PARTUSE PARTITION < <(df -P | awk '/\/var/{ sub(/%/,"",$5); print $5, $6 }')
if [ $PARTUSE -ge $ALERT ]; then
for file in /var/log/fileA.? /var/log/fileB.? /var/log/fileC.?
do
> $file
done &&
echo "Reclaimed disk space on \"$PARTITION\" now ($PARTUSE% full) on $(hostname) as of $(date)" |
mail -s "Reclaimed disk space - $PARTITION on $(hostname)" $ADMIN
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.