使用Bash将KB转换为MB [英] Convert KB To MB using Bash

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

问题描述

运行命令后,我使用命令获取远程文件夹的大小

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

120928312 http://blah.com

该数字的大小为字节.我想做的是将其输出为 MB ,并删除了 http 部分.我正在猜测文件,但不确定如何处理.

The number is size in bytes. 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

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

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