LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2004, 12:06 PM   #1
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Problems with this script


Hi,
I have be putzing with this script and I'm having a real brain cloud here.
Wonder if someone has an idea how to get the used partition sizes to add up
so they can be used in the final computation.

Code:
#!/bin/bash
##### This script calculates hard drive space.
##### example:  ./drive /dev/hda

##############################################
#  Ensure that root is running the script.
#
WHOAMI=`/usr/bin/whoami`
if [ $WHOAMI != "root" ]; then
	echo
	echo "You must be root to run this!"
	echo
      exit 1
fi
##############################################

usage()
{
        echo "Usage: $0 [/dev/hd#]"
        exit 1;
}

test "$1" || usage

if ! [ -e $1 ]
    then
    echo "$1 does not exist. Exiting."
    exit 1
fi

if [ -e $1 ]; then

drive=`/sbin/sfdisk -s $1`
echo
for i in `/sbin/sfdisk -l $1 | \
grep -e "^[/dev]" | awk '{print $1}'`;
do
a=`/sbin/sfdisk -s $i`
used=$((($used + $a) / 1024 ))
echo "Partition $i used $used MB"
done
echo

else
clear

echo "$1 is not a valid drive letter: "
echo
exit 1
fi
####
size=$(($drive / 1024 ))
used=$((($used + $a) / 1024 ))
free=$(($size - $used ))
#
echo " Total drive size    $size   MB"
echo " Partitioned size    $used   MB"
echo " Unpartitioned size  $free   MB"
echo
echo
#####End
 
Old 09-25-2004, 02:43 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
hope this example is good enough... you also seem to be running too many fdisk calls imho, which mine doesn't do... unless i'm missing somethign important...
Code:
blah=0
for i in `fdisk -l $1 | grep '^/dev' | cut -b 38-49`
do 
  blah=$((blah+$i))
  echo this partition = $i   total size = $blah
done
 
Old 09-25-2004, 02:59 PM   #3
idaho
Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: RedHat, Libranet
Posts: 438

Rep: Reputation: 30
Wouldn't something like:
parted -s /dev/sda print
do what you want?
 
Old 09-25-2004, 03:54 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Original Poster
Rep: Reputation: 61
thanks,

This is the output I would like to see.....

Partition /dev/hdb1 used 29996 MB
Partition /dev/hdb2 used 20032 MB
Partition /dev/hdb3 used 623 MB

Total drive size 76319 MB
Partitioned size 50603 MB
Unpartitioned size 25716 MB


And this is what I'm getting......

Partition /dev/hdb1 used 29996 MB
Partition /dev/hdb2 used 20032 MB
Partition /dev/hdb3 used 623 MB

Total drive size 76319 MB
Partitioned size 0 MB
Unpartitioned size 76319 MB
 
Old 09-25-2004, 05:07 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Please try if this works for you:
(changed parts are in red)
Code:
# [... left out argument checking ...]

if [ -e $1 ]; then

drive=`/sbin/sfdisk -s $1`
echo
for i in `/sbin/sfdisk -l $1 | \
grep -e "^[/dev]" | awk '{print $1}'`;
do
a=`/sbin/sfdisk -s $i`
used=$(($a / 1024 ))
totalused=$(($totalused + $a))
echo "Partition $i used $used MB"
done
echo

else
clear

echo "$1 is not a valid drive letter: "
echo
exit 1
fi
####
size=$(($drive / 1024 ))
free=$((($drive - $totalused ) / 1024))
#
echo " Total drive size    $size   MB"
echo " Partitioned size    $totalused   MB"
echo " Unpartitioned size  $free   MB"
echo
echo
#####End
 
Old 09-25-2004, 06:15 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Original Poster
Rep: Reputation: 61
Thanks Hko,

That works very nicely! I did notice the output is off by 1 MB but I'm not sure why.

Partition /dev/hdb1 used 29996 MB
Partition /dev/hdb2 used 20002 MB
Partition /dev/hdb3 used 604 MB

Total drive size 76319 MB
Partitioned size 50603 MB
Unpartitioned size 25715 MB <<<<< should be 25716

like this.......

Total drive size 76319 MB
Partitioned size 50603 MB
Unpartitioned size 25716 MB


Code:
#!/bin/bash
##### This script calculates hard drive space.
##### example:  ./drive /dev/hda

##############################################
#  Ensure that root is running the script.
#
WHOAMI=`/usr/bin/whoami`
if [ $WHOAMI != "root" ]; then
	echo
	echo "You must be root to run this!"
	echo
      exit 1
fi
##############################################

usage()
{
        echo "Usage: $0 [/dev/hd#]"
        exit 1;
}

test "$1" || usage

if ! [ -e $1 ]
    then
    echo "$1 does not exist. Exiting."
    exit 1
fi

if [ -e $1 ]; then

drive=`/sbin/sfdisk -s $1`
echo
for i in `/sbin/sfdisk -l $1 | \
grep -e "^[/dev]" | awk '{print $1}'`;
do
a=`/sbin/sfdisk -s $i`
part=$(($a / 1024))
totalused=$(($totalused + $a))
echo "Partition $i used $part MB"
done
echo

else
clear

echo "$1 is not a valid drive letter: "
echo
exit 1
fi
####
size=$(($drive / 1024))
used=$(($totalused / 1024))
free=$((($drive - $totalused) / 1024))
#
echo " Total drive size    $size   MB"
echo " Partitioned size    $used   MB"
echo " Unpartitioned size  $free   MB"
echo
echo
#####End
 
Old 09-26-2004, 05:03 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
BTW I noticed this:
Quote:
Code:
echo "$1 is not a valid drive letter: "
echo
What do you mean by "drive letter"?
Are you running bash under windows?
(just being curious)
 
Old 09-26-2004, 07:57 AM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Original Poster
Rep: Reputation: 61
Quote:
What do you mean by "drive letter"?
Are you running bash under windows?
(just being curious)
That's just left over from a previous version of this script and I haven't gotten around to clean up yet. I don't run windows unless I'm chasing a virus for someone or trying to figure out some other problem.

"Unpartitioned size 25715 MB <<<<< should be 25716"
In regards to the other problem, I noticed that it is consistant so I'm not too worried about it. Anyway drive size depends on who is doing the calculating.
For example.....
Qtparted shows my 10gig partition as 9.77GB which indicates they devide by 1048
Where fdisk shows it as 10001MB which indicates they devide by 1024
If I want to feel really good about my drive size, I devide by 1000

Thanks for your help!
 
Old 09-27-2004, 01:04 PM   #9
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Original Poster
Rep: Reputation: 61
This script works perfectly on my main computer but,
I tried it on other computers with only one partition
and instead of listing /dev/hda2 as "0" , it pops these error messages.
The error messages don't effect the math so I would like to make them gone.
I can get rid of the first message.....
/dev/hda2: No such device or address
with this...
a=`/sbin/sfdisk -s $i 2> /dev/null`

I just don't know how to get rid of the second message.
./drive: line 39: / 1024: syntax error: operand expected (error token is "/ 1024")

Any ideas????

Code:
 
drive=`/sbin/sfdisk -s $1`
echo
for i in `/sbin/sfdisk -l $1 | \
grep -e "^[/dev]" | awk '{print $1}'`;
do
a=`/sbin/sfdisk -s $i`
part=$(($a / 1024))
totalused=$(($totalused + $a))
echo "Partition $i used $part MB"
done

[root@sony home]# ./drive /dev/hda

Partition /dev/hda1 used 10040 MB
/dev/hda2: No such device or address <<<<<<<<<<<<

sfdisk: cannot open /dev/hda2 for reading
./drive: line 39: / 1024: syntax error: operand expected (error token is "/ 1024") <<<<<<<<<
Total drive size 25941 MB
Partitioned size 10040 MB
Unpartitioned size 15901 MB



Code:
  
drive=`/sbin/sfdisk -s $1`
echo
for i in `/sbin/sfdisk -l $1 | \
grep -e "^[/dev]" | awk '{print $1}'`;
do
a=`/sbin/sfdisk -s $i 2> /dev/null`
part=$(($a / 1024))
totalused=$(($totalused + $a))
echo "Partition $i used $part MB"
done
[root@sony home]# ./drive /dev/hda

Partition /dev/hda1 used 10040 MB
./drive: line 38: / 1024: syntax error: operand expected (error token is "/ 1024") <<<<<<<<
Total drive size 25941 MB
Partitioned size 10040 MB
Unpartitioned size 15901 MB

Last edited by homey; 09-27-2004 at 01:06 PM.
 
Old 09-27-2004, 10:18 PM   #10
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Original Poster
Rep: Reputation: 61
Here is a fix for the error messages. It may be ugly but I don't have any other ideas off hand.

Code:
drive=`/sbin/sfdisk -s $1`
echo
for i in `/sbin/sfdisk -l $1 | \
grep -e "^[/dev]" | awk '{print $1}'`;
do
a=`/sbin/sfdisk -s $i 2> /dev/null`
part=$((($a + 0) / 1024))
totalused=$(($totalused + ($a + 0)))
echo "Partition $i used $part MB"
done
Now the out put looks like this on both computers.

[root@sony home]# ./drive /dev/hda

Partition /dev/hda1 used 10040 MB
Partition /dev/hda2 used 0 MB
Partition /dev/hda3 used 0 MB
Partition /dev/hda4 used 0 MB

Total drive size 25941 MB
Partitioned size 10040 MB
Unpartitioned size 15901 MB

Code:
#!/bin/bash
##### This script calculates hard drive space.
##### example:  ./drive /dev/hda

##############################################
#  Ensure that root is running the script.
#
WHOAMI=`/usr/bin/whoami`
if [ $WHOAMI != "root" ]; then
	echo
	echo "You must be root to run this!"
	echo
      exit 1
fi
##############################################

usage()
{
        echo "Usage: $0 [/dev/hd#]"
        exit 1;
}

test "$1" || usage

if ! [ -e $1 ]; then
    echo "$1 does not exist. Exiting."
    exit 1
fi

if [ -e $1 ]; then

drive=`/sbin/sfdisk -s $1`
echo
for i in `/sbin/sfdisk -l $1 | \
grep -e "^[/dev]" | awk '{print $1}'`;
do
a=`/sbin/sfdisk -s $i 2> /dev/null`
part=$((($a + 0) / 1024))
totalused=$(($totalused + ($a + 0)))
echo "Partition $i used $part MB"
done
echo

else
    exit 1
fi

#####
size=$(($drive / 1024))
used=$(($totalused / 1024))
free=$((($drive - $totalused +1) / 1024))
#
echo " Total drive size    $size   MB"
echo " Partitioned size    $used   MB"
echo " Unpartitioned size  $free   MB"
echo
echo
#####End
 
  


Reply



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
shell script problems BC1 Linux - General 1 05-01-2005 11:42 PM
Problems with simple script.... jong357 Programming 6 12-20-2004 06:08 AM
Big problems with script going away.. aggscott Linux - Newbie 4 08-22-2004 06:50 PM
script problems Lindows45 Linux - Newbie 9 03-04-2004 03:18 PM
Stupid script problems hayduke Mandriva 4 10-03-2003 09:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:51 AM.

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
Open Source Consulting | Domain Registration