Bash 中两个变量相减

发布于 2024-12-19 16:58:07 字数 250 浏览 1 评论 0原文

我有下面的脚本来减去两个目录之间的文件计数,但 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

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?

#!/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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

生死何惧 2024-12-26 16:58:07

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

count=$((FIRSTV-SECONDV))

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

count=$(expr $FIRSTV - $SECONDV)

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

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

count=$((FIRSTV-SECONDV))

BTW, the correct syntax of using expr is:

count=$(expr $FIRSTV - $SECONDV)

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

风筝在阴天搁浅。 2024-12-26 16:58:07

您只需要在减号和反引号周围添加一些额外的空格:

COUNT=`expr $FIRSTV - $SECONDV`

请注意退出状态:

如果 EXPRESSION 既不为 null 也不为 0,则退出状态为 0;如果 EXPRESSION 为 null 或 0,则退出状态为 1。

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

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.

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

深海不蓝 2024-12-26 16:58:07

您可以使用:

((count = FIRSTV - SECONDV))

来避免调用单独的进程,如以下记录所示:

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

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
装迷糊 2024-12-26 16:58:07

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

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

This is how I always do maths in Bash:

count=$(echo "$FIRSTV - $SECONDV"|bc)
echo $count
做个少女永远怀春 2024-12-26 16:58:07

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

COUNT=$(expr $FIRSTV - $SECONDV)

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

COUNT=$((FIRSTV - SECONDV))

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

COUNT=$(expr $FIRSTV - $SECONDV)

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

COUNT=$((FIRSTV - SECONDV))
南街九尾狐 2024-12-26 16:58:07

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

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

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

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

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

For more info on let, look here.

韶华倾负 2024-12-26 16:58:07

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

let COUNT=$FIRSTV-$SECONDV

let COUNT=FIRSTV-SECONDV

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

let COUNT=$FIRSTV-$SECONDV

or

let COUNT=FIRSTV-SECONDV

彼岸花ソ最美的依靠 2024-12-26 16:58:07

区分实数正数

diff_real () {
  echo "df=($1 - $2); if (df < 0) { df=df* -1}; print df" | bc -l;
}

用法

var_a=10
var_b=4

output=$(diff_real $var_a $var_b)
# 6

#########


var_a=4
var_b=10

output=$(diff_real $var_a $var_b)
# 6

Diff Real Positive Numbers

diff_real () {
  echo "df=($1 - $2); if (df < 0) { df=df* -1}; print df" | bc -l;
}

Usage

var_a=10
var_b=4

output=$(diff_real $var_a $var_b)
# 6

#########


var_a=4
var_b=10

output=$(diff_real $var_a $var_b)
# 6
乖不如嘢 2024-12-26 16:58:07

使用 BASH:

#!/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

输出

$ ./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
$

Use BASH:

#!/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
$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文