LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-12-2004, 05:47 AM   #1
jonathanztaub
LQ Newbie
 
Registered: May 2004
Posts: 14

Rep: Reputation: 0
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.
 
Old 05-12-2004, 06:39 AM   #2
jonathanztaub
LQ Newbie
 
Registered: May 2004
Posts: 14

Original Poster
Rep: Reputation: 0
#!/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
 
Old 05-12-2004, 06:55 AM   #3
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with regular expression aecaudel Programming 6 11-04-2005 06:28 AM
Need help with Regular Expression subaruwrx Linux - Newbie 6 09-04-2004 08:48 PM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 11:08 AM
Regular Expression slizadel Programming 4 07-28-2003 06:16 AM
regular expression gumby Programming 3 07-15-2003 01:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration