[Gambas-user] How to determine the order of magnitude of a Long variable

Tobias Boege tobs at taboege.de
Thu May 13 17:06:51 CEST 2021


On Thu, 13 May 2021, Bruce wrote:
> I need to determine how many blocks (sectors, writeable chunks, whatever)
> are available on a USB "drive" (stick, sdd, whatever).
> 
> I can get a reasonably accurate value Shell'ing the lsblk command. However
> the value could be anywhere between 0 and the total number of usable blocks
> on the device (i.e. the "formatted size").
> 
> In order to determine whether the data I am about to write to the device
> will fit I need to know the order of magnitude of these free blocks - kB,
> mB, gB, TB, etc etc.
> 
> This is because the utilities that do the writing wont give the size of the
> data in formats other than the so-called "human-readable format" e.g 3.25G
> or 4GB or any number of other variants, which may by the way be based on
> 1024 or 1000 byte scales. (Grrr!)
> 
> In that first case, that 3.25G could be any number of bytes between (3.0
> gigabytes)+1 and (3.5 gigabytes)-1 but that might not fit onto the drive if
> it tries to write into the "last block on the device".
> 
> My goal, in case you are wondering, is to keep the maximum number of daily
> backups as possible on the drive and the Gambas project will be run by a
> cron job (so there is np user interaction).
> 
> To be safe I thought I'd take the biggest "about to write" size i.e the
> smalllest scale (I think) convert that to a "Required Blocks" value and
> compare it to the actual AvailableBlocks. This does not work for various
> mathematical reasons. So I need to do it the other way around i.e. is the
> number of AvailableBlocks absolutely larger than the needed blocks.
> 
> I have searched the internet for something like this with poor results. So
> in desparation... does anyone have any idea how to get a reliable
> comparison?
> 

I would first like to ask back if you can use `df` instead of the tools
you mentioned. Depending on how your backups work, you have to mount the
drive (or you don't) before starting the backup. If the filesystem is
always mounted on /mnt/backup, you can use

  $ LC_ALL=C df --output=avail /mnt/backup
     Avail
  1234567890

This number is in bytes and the path to the mountpoint for your drive
will be available to your script anyway.

Also, `man lsblk` tells me that the tool understands a --bytes switch
to print bytes instead of the human-readable string with unclear units
which you have trouble with.

Best,
Tobias

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk


More information about the User mailing list