Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
The problem statement, all variables and given/known data:
Im still pretty new to this and could really use some help with this task
"Write a script to check in the directory specified by
the first argument, if there is enough free space specified by the second
argument. Provide a comprehensive examination of the arguments."
Relevant commands, code, scripts, algorithms:
The attempts at a solution (include all code and scripts):
Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
New Bulgarian University, Sofia, Bulgaria, for professor Simionov course number CSCB731
Thanks to everyone and this forum
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
Thanks alot for your hints and help.
This is the result I came up with, but my pc died yesterday and I am now only able to access internet club pc's and cannot test the code yet, can someone please give a look and tell me if it's wrong. Thanks alot guys
if [ -d directory ]
then
du -hs directoryname
else
echo Directory not found
fi
-s is not an option to df (there's nothing to summarise).
Your script also expects two arguments (call them $1 and $2), and you need to make sense of the df output to get what you need.
if [ -d "$1" ]; then
FREE=$(df -k "$1" | <some parsing to get free space field>)
[ $FREE -le $2 ] && echo Not enough space
else
echo Directory does not exist
(I don't think using -h is so great. It will change depending on how much space is available (could be in GB, MB, etc.))
I tried re-doing it, please tell me what you think and if you can find mistakes
I do a space check as follows basing my alert on available,
for dspace in `df -k /filesystem |tail -1 |awk '{print $1}'`
do
if [ $dspace -le "80" ]
then
echo $dspace % available
else
df -k /filesystem > /tmp/fsspace
cat /usr/local/bin/fswarn.txt /tmp/fsspace > /usr/local/bin/fswarning.txt
/usr/bin/mailx -s "FS exceeded 80% and is at $dspace % FULL needs attention" mymail@mydomain.tld < /usr/local/bin/export
warning.txt
fi
for dspace in `df -k /filesystem |tail -1 |awk '{print $1}'`
Your df here takes the last line (tail -1) and prints the first field (awk '{print $1}'. That should leave you with a single word, so the for-loop isn't really necessary.
And the output of df, which varies from system to system, probably wouldn't show the space free in the first field.
On my system (OS X), for example:
/Users/scott/scripts/tmp $ df -k .
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/disk0s2 976426672 838053612 138117060 86% /
the available space would be in "$4", and the used space as a percentage in "$5".
You're also "catting" a couple of files in the else part of your if statement. Where do they come from?
Assuming that /usr/local/fswarn.txt exists, and that /usr/local/bin/exportwarning.txt should be /usr/local/bin/fswarning.txt (!), you could simplify that a bit.
fsfile=/tmp/fsspace.txt # set it here, so I don't have to change it in more than one place
df -k $1 > $fsfile
dspace=$(tail -1 $fsfile | awk '{print 100-$5}') #100% - % used = % available
if [ $dspace -lt $2 ]; then
echo "$dspace % available"
else
cat /usr/local/bin/fswarn.txt /tmp/fsspace | /usr/bin/mailx -s "FS exceeded $2% ....." mymail@mydomain.tld
fi
It still needs work (primarily checking that the arguments are given, and valid, but I'm sure you can handle that
first of all thx a lot scottn your help if really great !!!
Your giving me confidence to continue trying to do this
So here is attempt number 3 I wrote that when waiting for a previous answer and don't want it to go to waste so here goes :
Assuming parameter '1' is a mount point and parameter '2' is a percent "free", then :
if [ -d "$1" ]; then
FREE=$(df -k $1 | |awk 'NR==2{print 100-substr($5,1,length($5)-1);}')
[ $FREE -le $2 ] && echo "Not enough space, ${FREE}% left"
else
echo "Directory $1 does not exist"
fi