[HOME] [BASH] [Window Managers]

Some arithmetics. Useless but educative

#!/bin/bash
#
## disksize
#
#
## Calculates the real size of your disks.
#

if [ "$(whoami)" != "root" ] ; then
	echo "Run as root."
	exit
fi

KIBIBYTE=$((2**10))
MIBIBYTE=$((2**20))
GIBIBYTE=$((2**30))

KILOBYTE=$((10**3))
MEGABYTE=$((10**6))
GIGABYTE=$((10**9))

echo 'Write the device name, ej. hda, sda (not necessarily mounted):'
read device

if [ ! -e /dev/$device ]; then

	echo "
If you just pluged the device in wait a second to kernel recognize it
and rerun the script.  Other posibility is the device doesn't exist."

	exit
fi
size=$( fdisk -l /dev/$device | awk '/Disk/ { print $5 }' )

# More than 1 gibibyte.
if [ $size -gt $GIBIBYTE ] ; then
	realsize=$( dc -e "1k$size $GIBIBYTE / p" )
	fakesize=$( dc -e "1k$size $GIGABYTE / p" )

	if [ `echo $fakesize | grep -o '\..*'` = ".0" ]; then
		fakesize=`echo $fakesize | sed 's/\..*//'`
	fi
	if [ `echo $realsize | grep -o '\..*'` = ".0" ]; then
		realsize=`echo $realsize | sed 's/\..*//'`
	fi
	echo "Your $fakesize gigabytes are really $realsize 'gibibytes'".

	exit
# More than 1 mibibyte.
elif [ $size -gt $MIBIBYTE ] ; then
	realsize=$( dc -e "1k$size $MIBIBYTE / p" )
	fakesize=$( dc -e "1k$size $MEGABYTE / p" )
	if [ `echo $fakesize | grep -o '\..*'` = ".0" ]; then
		fakesize=`echo $fakesize | sed 's/\..*//'`
	fi
	if [ `echo $realsize | grep -o '\..*'` = ".0" ]; then
		realsize=`echo $realsize | sed 's/\..*//'`
	fi
	echo "Your $fakesize megabytes are really $realsize 'mibibytes'".

	exit
# Less than 1 mibibyte.
else
	realsize=$( dc -e "1k$size $KIBIBYTE / p" )
	fakesize=$( dc -e "1k$size $KILOBYTE / p" )
	if [ `echo $fakesize | grep -o '\..*'` = ".0" ]; then
		fakesize=`echo $fakesize | sed 's/\..*//'`
	fi
	if [ `echo $realsize | grep -o '\..*'` = ".0" ]; then
		realsize=`echo $realsize | sed 's/\..*//'`
	fi
	echo "Your $fakesize kilobytes are really $realsize 'kibibytes'".
fi

## End disksize

<= Prev Next =>


[HOME] [BASH] [Window Managers]

You can mail me to eloi at roquesor.com.