KB到MB使用bash [英] KB To MB using bash

查看:109
本文介绍了KB到MB使用bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用命令来获取远程文件夹的大小,它的运行后,它会返回

I use a command to get the size of a remote folder after it's run it returns

120928312 http://blah.com

数在KB大小我想要做的是什么让它在MB产量和删除,我猜greping到一个文件,但不知道如何去做。在HTTP部分

the number is size in KB what i'd like to do is have it output in MB and the http part removed, I'm guessing greping to a file but not sure how to go about it.

推荐答案

您可以用shell内建命令去做

You can do it with shell builtins

some_command |while read KB dummy;do echo $((KB/1024))Mb;done

下面是一个更有用的版本:

Here is a more useful version:

#!/bin/sh
human_print(){
while read B dummy; do
  [ $B -lt 1024 ] && echo ${B} bytes && break
  KB=$(((B+512)/1024))
  [ $KB -lt 1024 ] && echo ${KB} kilobytes && break
  MB=$(((KB+512)/1024))
  [ $MB -lt 1024 ] && echo ${MB} megabytes && break
  GB=$(((MB+512)/1024))
  [ $GB -lt 1024 ] && echo ${GB} gigabytes && break
  echo $(((GB+512)/1024)) terabytes
done
}

echo 120928312 http://blah.com | human_print

这篇关于KB到MB使用bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆