LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Regular expression and tools (https://www.linuxquestions.org/questions/linux-newbie-8/regular-expression-and-tools-180607/)

jonathanztaub 05-12-2004 04:47 AM

Regular expression and tools
 
I hope I'm posting this message to the correct group.

I need to find the amount of disk space currently occupied in percentages.
This is relatively easy:

/bin/df -k /

It outputs the following:

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda1 60476036 9084940 48319068 16% /


From within a bash shell script, I have to get the number 16.
Meaning, I have to look for digits before a percent sign.
I know I have to use regular expressions and something called a backreference but
I haven't been able to accomplish that.

Please provide good links if you know about and a code sample for a solution if possible.

jonathanztaub 05-12-2004 05:39 AM

#!/bin/bash

diskSpace=`/bin/df -k / | perl -ne '/ (\d+)% /; print $1'`
echo "diskSpace=$diskSpace"

if [ $diskSpace -gt 90 ]; then
echo "Disk space is running low."
fi

hw-tph 05-12-2004 05:55 AM

Without Perl, this is one way of doing the above:
Code:

#!/bin/bash
diskspace=`df -k / | grep ^/ | gawk '{ print $5 }' | tr -d '%'`
echo "diskspace = $diskspace"
if [ $diskspace -gt 90 ]
then
    echo "Disk space is running low."
fi


Håkan


All times are GMT -5. The time now is 10:10 PM.