在 Bash 中减去两个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8385627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:17:32  来源:igfitidea点击:

Subtract two variables in Bash

bashshellunix

提问by toop

I have the script below to subtract the counts of files between two directories but the COUNT=expression does not work. What is the correct syntax?

我有下面的脚本来减去两个目录之间的文件数,但COUNT=表达式不起作用。什么是正确的语法?

#!/usr/bin/env bash

FIRSTV=`ls -1 | wc -l`
cd ..
SECONDV=`ls -1 | wc -l`
COUNT=expr $FIRSTV-$SECONDV  ## -> gives 'command not found' error
echo $COUNT

回答by anubhava

Try this Bash syntax instead of trying to use an external program expr:

试试这个 Bash 语法,而不是尝试使用外部程序expr

count=$((FIRSTV-SECONDV))

BTW, the correct syntax of using expris:

顺便说一句,使用的正确语法expr是:

count=$(expr $FIRSTV - $SECONDV)

But keep in mind using expris going to be slower than the internal Bash syntax I provided above.

但请记住,使用expr会比我上面提供的内部 Bash 语法慢。

回答by Aaron McDaid

You just need a little extra whitespace around the minus sign, and backticks:

你只需要在减号和反引号周围多加一点空格:

COUNT=`expr $FIRSTV - $SECONDV`

Be aware of the exit status:

注意退出状态:

The exit status is 0 if EXPRESSION is neither null nor 0, 1 if EXPRESSION is null or 0.

如果 EXPRESSION 既不是空也不是 0,则退出状态为 0,如果 EXPRESSION 为空或 0,退出状态为1

Keep this in mind when using the expression in a bash script in combination with set -ewhich will exit immediately if a command exits with a non-zero status.

将 bash 脚本中的表达式与set -e结合使用时请记住这一点,如果命令以非零状态退出,它将立即退出。

回答by paxdiablo

You can use:

您可以使用:

((count = FIRSTV - SECONDV))

to avoid invoking a separate process, as per the following transcript:

避免调用单独的过程,按照以下记录:

pax:~$ FIRSTV=7
pax:~$ SECONDV=2
pax:~$ ((count = FIRSTV - SECONDV))
pax:~$ echo $count
5

回答by Pureferret

This is how I always do maths in Bash:

这就是我总是在 Bash 中做数学的方式:

count=$(echo "$FIRSTV - $SECONDV"|bc)
echo $count

回答by Karoly Horvath

White space is important, exprexpects its operands and operators as separate arguments. You also have to capture the output. Like this:

空格很重要,expr期望其操作数和运算符作为单独的参数。您还必须捕获输出。像这样:

COUNT=$(expr $FIRSTV - $SECONDV)

but it's more common to use the builtin arithmetic expansion:

但更常见的是使用内置算术扩展:

COUNT=$((FIRSTV - SECONDV))

回答by Shawn Chin

For simple integer arithmetic, you can also use the builtin letcommand.

对于简单的整数运算,您还可以使用内置的let命令。

 ONE=1
 TWO=2
 let "THREE = $ONE + $TWO"
 echo $THREE
    3

For more info on let, look here.

有关更多信息let,请查看此处

回答by another.anon.coward

Alternatively to the suggested 3 methods you can try letwhich carries out arithmetic operations on variables as follows:

除了建议的 3 种方法外,您还可以尝试let对变量进行算术运算,如下所示:

let COUNT=$FIRSTV-$SECONDV

let COUNT=$FIRSTV-$SECONDV

or

或者

let COUNT=FIRSTV-SECONDV

let COUNT=FIRSTV-SECONDV

回答by Victoria Stuart

Use Python:

使用 Python:

#!/bin/bash
# home/victoria/test.sh

START=$(date +"%s")                                     ## seconds since Epoch
for i in $(seq 1 10)
do
  sleep 1.5
  END=$(date +"%s")                                     ## integer
  TIME=$((END - START))                                 ## integer
  AVG_TIME=$(python -c "print(float($TIME/$i))")        ## int to float
  printf 'i: %i | elapsed time: %0.1f sec | avg. time: %0.3f\n' $i $TIME $AVG_TIME
  ((i++))                                               ## increment $i
done

Output

输出

$ ./test.sh 
i: 1 | elapsed time: 1.0 sec | avg. time: 1.000
i: 2 | elapsed time: 3.0 sec | avg. time: 1.500
i: 3 | elapsed time: 5.0 sec | avg. time: 1.667
i: 4 | elapsed time: 6.0 sec | avg. time: 1.500
i: 5 | elapsed time: 8.0 sec | avg. time: 1.600
i: 6 | elapsed time: 9.0 sec | avg. time: 1.500
i: 7 | elapsed time: 11.0 sec | avg. time: 1.571
i: 8 | elapsed time: 12.0 sec | avg. time: 1.500
i: 9 | elapsed time: 14.0 sec | avg. time: 1.556
i: 10 | elapsed time: 15.0 sec | avg. time: 1.500
$