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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-31-2005, 05:10 PM
|
#1
|
|
Senior Member
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249
Rep:
|
how to detect size of IDE device
Hi. I am writing a script to detect the sizes of the IDE devices connected to my system. What is the most accuate way of doing it. Right now I am mouting the device on one of my directory adn then give the command df -h to check the size.
Is there any other way by which I can check the total size of device with mounting them??
Thanks
|
|
|
|
10-31-2005, 05:22 PM
|
#2
|
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
Try the command: sfdisk -s /dev/hda
|
|
|
|
10-31-2005, 05:48 PM
|
#3
|
|
Senior Member
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249
Original Poster
Rep:
|
Quote:
Originally posted by homey
Try the command: sfdisk -s /dev/hda
|
hey that worked. Also, it gives the output in KBs. Is there any way(through a command) by which i can divide it with 1000(just corrected) so that I get the output in MB straightaway. Some thing like:
sfdisk -s /dev/hdc | (divide operation here).
Thank you once again.
Last edited by kushalkoolwal; 10-31-2005 at 06:30 PM.
|
|
|
|
10-31-2005, 06:05 PM
|
#4
|
|
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,466
Rep:
|
1mb is actually 1024kb not 100
of course this is file system size for a meg and not the hardware industry standard meg
to make this more confusing blocks change size too with file system.
as far as what command can be used mabye calc but im not sure at this very moment what the baisc program is for math operations with bash.
|
|
|
|
10-31-2005, 06:46 PM
|
#5
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
If you don't care about the remainder, you can perform the division in bash.
mbsize=(( $kbsize/1024 ))
|
|
|
|
10-31-2005, 07:11 PM
|
#6
|
|
Senior Member
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249
Original Poster
Rep:
|
Yeah I understand what you are saying . But in the first place how can I get the output of the command sfdisk -s /dev/hdc into the variable $kbsize?
Thanks
|
|
|
|
10-31-2005, 08:03 PM
|
#7
|
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
I made a script a while back to show how much un-partitioned space was left on a drive.
Here is a snip for what you want....
Code:
#!/bin/bash
# Example: ./test /dev/hda
drive=`/sbin/sfdisk -s $1`
size=$(($drive / 1024))
echo " Total drive size $size MB"
Here is the original script.
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) / 1024 +1))
#
echo " Total drive size $size MB"
echo " Partitioned size $used MB"
echo " Unpartitioned size $free MB"
echo
echo
#####End
|
|
|
|
11-06-2005, 02:38 PM
|
#8
|
|
Senior Member
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249
Original Poster
Rep:
|
That;sGreat!!! Thanks you so much. 
|
|
|
|
11-06-2005, 02:47 PM
|
#9
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
To know the total size in MB you could also do :
Code:
sfdisk -s -uM /dev/hda
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:26 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|