English 中文(简体)
字母缩略语(数字)
原标题:Subtracting strings (that are numbers) in a shell script

我用手法写了一张彩票,显示名录的大小,以人类可读格式(例如,802M或702K)。 我想计算面积之间的差额。

我的发言稿:

#!/bin/bash

current_remote_dir_size=789M
new_remote_dir_size=802M
new_size=`echo ${new_remote_dir_size} | grep -o [0-9]*`
current_size=`echo ${current_remote_dir_size} | grep -o [0-9]*`

echo "${new_size}-${current_size}"

但文字的输出只是这样。

-

我怎样才能使减员工作?

最佳回答

您可以通过在<代码>(和)中填入“仪表”中的表述方式,以印本做基本分类数。

$ echo $(( 5 + 8 ))
13

在你的具体情况下,我的工作如下:

$ echo "${new_size}-${current_size}"
802-789
$ echo $(( ${new_size}-${current_size} ))
13

归根结底,你的产出是微不足道的。 检查灰色表达实际上会产生预期产出。 如果不是的话,你也许需要把定期的表述打上引号。

问题回答

你们不需要打碎你方言的 letters:

current_remote_dir_size=789M
current_size=${current_remote_dir_size%[A-Z]}
echo $current_size  # ==> 789

new_remote_dir_size=802M
new_size=${new_remote_dir_size%[A-Z]}
echo $new_size      # ==> 802

)。

你们都需要:

echo $((${new_remote_dir_size/M/}-${current_remote_dir_size/M/}))
  • $((a+b - 4)) can be used for arithmetic expressions
  • ${string/M/} replaces M in string with nothing. See man bash, Section String substitution, for more possibilities and details.




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签