From: Karyn Williams (karyn@calarts.edu)
Date: Thu Feb 24 2005 - 16:03:20 EST
Thanks to:
Chris Schroen
Christ Clark
Michael DeSimone
Matthew Stier
David DEAVES
SteinAxt
Rob McMahon
John F Wall
Alex Stade
John Julian
Stephen Barnett
John Leadeham
Damir Delija
Mats Oberg
Jonathan Birchall
Alan Crosby
Hutin Bertrand
Doc G (Tim)
Luc Suryo
Thad MacMillan
Reggie Beavers
The problem with the original script was that read was running in a
subshell and the variables do not survive when it closes. ksh handles this
differently than sh and bash which are the shells I tested before posting.
David Deaves and Christ Clark said it well "In bourne shell (/bin/sh) the
head of the pipeline is the current shell, and the tail is a sub shell. So
in this case, the sub shell happily evaluates the input and allocates it to
it's environment variables, then exits." and "Being part of a pipe, the
'read' is executed in a subshell. The variables it sets are lost when the
subshell terminates and not propagated back to the parent shell."
The solution for sh I chose to use is from Chris Schroen and it is to use
set and then the positional parameters ($1 $2 ..$9) to pull the data that I
wanted.
#!/bin/sh
while read i
do
set `quota -v $i | tail -1`
mailx -s "subject line" -r address $i <<EOT
You have $5 $6 left until your mail will stop being delivered to your
mailbox. Your quota is $3 Kb and you
r current usage is $2 Kb.
EOT
done </usr/local/tools/gtest.users
A variation on this is:
line="`quota -v $i | tail -1`"
set - $line
fs=$1; usage=$2; etc...
Another solution add a while/do:
#!/bin/sh
for i in `cat /usr/local/tools/gtest.users` ; do
quota -v $i | tail -1 | while read fs usage qta limit time left files
quota limit1
do
mailx -s "subject line" -r address $i <<EOT
You have $time $left left until your mail will stop being delivered to your
mailbox. Your quota is $qta Kb and you
r current usage is $usage Kb.
EOT
done
done
Another workaround is to run everything in a subshell,
for ...; do
quota -v $i | tail -1 | ( read fs usage qta limit time left files
mailx -s "subject line" -r address $i <<EOT
You have $time $left left until your mail will stop being delivered to your
mailbox. Your quota is $qta Kb and you
r current usage is $usage Kb.
EOT
)
done
A couple of web sites recommended with good info on scripting:
http://www.unixguide.net/
http://laku19.adsl.netsonic.fi/era/unix/award.html
Mats Oberg and Alex Stade sent versions in Perl. I have not tested them.
#!/path/to/perl
$name=$ARGV[0];
if ( $name eq "" ) {die "Usage: $0 <filesystem>\n";}
open(QUOTA,"/usr/sbin/repquota $name |") or die "Unable to execute the
repquota command...\n";
$i=0;
print "$name \n";
while(<QUOTA>)
{
@A=split;
if( $A[1]=~/\+\-/ ) {
if ( $A[5]=~/EXPIRED/ ) {
open(MAIL,"|mailx -s \"$A[0], Your quota gracetime
has expired\" $A[0]");
print MAIL "You have exceeded the quota limit for
your unix home-directory. \nThe limit of your home-directory is: $A[3] kb,
you currently occupy: $A[2] kb\n\n";
print MAIL "The gracetime has expired, until you
have decreased the data to less than $A[3] kilobytes, you can no longer
write anything to your home-directory.\n(If you can't remove enough data on
your own, please contact the helpdesk)\n";
}
elsif ( $A[5]=~/NOT/ ) {
open(MAIL,"|mailx -s \"$A[0], is over the quota
limit\" obergmat");
print MAIL "You still have time to decrease the
amount of data.\nWhen the gracetime expires you will not be able to write
anything to your home-directory.\n(You could possibly be thrown out and be
unable to log in.)\n";
}
else {
open(MAIL,"|mailx -s \"$A[0], You are over the
quota limit\" $A[0]");
print MAIL "You have exceeded the quota limit for
your unix home-directory. \nThe limit of your home-directory is: $A[3] kb,
you currently occupy: $A[2] kb\n\n";
print MAIL "You still have $A[5] $A[6] to decrease
the amount of data.\nWhen the gracetime expires you will not be able to
write anything to your home-directory.\n(You could possibly be thrown out
and be unable to log in.)\n";
}
print MAIL "\n\nFor more info about quotas see
http://some.adress.com ---- Servicedesk tel: our phonenr...\n\n";
close(MAIL);
}
}
close(QUOTA);
#!/usr/bin/perl -w
$file = "/usr/local/tools/gtest.users";
die "Can't open $file: $!\n"
unless open (FILE, "$file");
while ($line = <FILE>)
{
chomp $line;
my @junk = `quota -v $line`;
@quota = split (/\s+/, $junk[2]);
if ($quota[1] >= $quota[2])
{
open (MAIL, "| Mail \"Your account on Muse is
over-quota\" $line");
print MAIL "You are over your quota on Muse. You are
currently in the grace period which still allows for delivery of your
e-mail. Please remove files or delete old mail. Lack of action will
result in lost e-mail and the inability to upload files to your account.
You have $quota[5] day(s) left until your e-mail will stop being
delivered to your mailbox.\nThe Helpdesk can be reached at (661)
253-7887 or at helpdesk\@calarts.edu.\nThank you.";
}
}
A few others gave general tip s for scripting which I will keep for future
reference. Thanks again to all who replied.
Original message:
>Below is a script I am trying to make work but I just can't seem to make it
>right. The purpose is to send an e-mail to a user who is over their quota
>and still in the grace period to alert them. I want to include some user
>specific details like timeleft usage and quota. The catted file is just
>some test names I set up.
>
>I do not seem to get any values using read. Everything seems to be null.
>This user is over-quota (I made it so). Below is the output to quota -v
>user. The echos were just to test the vars. Solaris 8. Any help would be
>appreciated.
>
>#!/bin/sh
>
> for i in `cat /usr/local/tools/gtest.users` ; do
>
> quota -v $i | tail -1 | read fs usage qta limit timeleft files
>quota limit1
>
> echo "$REPLY"
> echo "$fs"
> echo `$usage`
> echo '$qta'
> echo ${limit}
> echo $timeleft
> echo $files
>
> mailx -s "Your account on Muse is over-quota" -r
>helpdesk@calarts.edu $i <<EOT
>
>You are over your quota on Muse. You are currently in the grace period
>which still allows delivery of your e-mail. Please remove files or delete
>old mail. Lack of action will result in lost e-mail and the inability to
>upload files to your account. You have $timeleft left until your mail will
>stop being delivered to your mailbox.
>
>The Helpdesk can be reached at 661 253 7887 or at helpdesk@calarts.edu.
>Thank you.
>
>EOT
>
>done
>
>
>
># quota -v jcraford
>Disk quotas for jcraford (uid 2709):
>Filesystem usage quota limit timeleft files quota limit
>timeleft
>/export 25734 20000 55000 6.9 days 73 0 0
-- Karyn Williams Network Services Manager California Institute of the Arts karyn@calarts.edu http://www.calarts.edu/network _______________________________________________ sunmanagers mailing list sunmanagers@sunmanagers.org http://www.sunmanagers.org/mailman/listinfo/sunmanagers
This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 23:30:13 EDT