bash 如何了解bash中的值是偶数还是奇数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3418349/
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-17 22:26:30  来源:igfitidea点击:

How to learn if a value is even or odd in bash?

bashroundingmedian

提问by Gal

I am building a movie database and I need to find a median for ratings. I'm really new to bash (it's my first assignment).

我正在建立一个电影数据库,我需要找到评分的中位数。我对 bash 真的很陌生(这是我的第一个任务)。

I wrote:

我写:

let evencheck=$"(($Amount_of_movies-$Amount_of_0_movies)%2)"
if [ $evencheck==0 ] 
then
let median="(($Amount_of_movies-$Amount_of_0_movies)/2)"
else
let median="(($Amount_of_movies-$Amount_of_0_movies)/2)+1"
fi

When $amount_of_movies = 6and $amount_of_0_movies = 1. I would expect median to be 3. but it's 2. Why is that?

$amount_of_movies = 6$amount_of_0_movies = 1。我希望中位数是 3。但它是 2。为什么会这样?

回答by Luc Suryo

#!/bin/bash

if (( $# != 1 )) ; then
    echo "syntax:  `basename 
let evencheck="(($Amount_of_movies-$Amount_of_0_movies)%2)"
if [ $evencheck -eq 0  ] 
then
   let median="(($Amount_of_movies-$Amount_of_0_movies)/2)"
else
   let median="(($Amount_of_movies-$Amount_of_0_movies)/2)+1"
fi
` number" exit 255 else _value=$(expr % 2) (( $_value == 0 )) && exit 1 || exit 0 fi

回答by Doon

try this:

尝试这个:

let evencheck="$((($Amount_of_movies-$Amount_of_0_movies)%2))"

Removing the $, and then testing for numeric equality.

删除 $,然后测试数字相等。

回答by wilhelmtell

Your code doesn't parse. To evaluate expressions in Bash, you say

您的代码无法解析。要在 Bash 中计算表达式,您可以说

if [ $evencheck = 0 ]; then

That is, evaluate arithmetics like so: $(( ... ))

也就是说,像这样评估算术: $(( ... ))

That said, your problem is in the conditional. See man test.

也就是说,您的问题出在条件中。见man test

let evencheck=(Amount_of_movies-Amount_of_0_movies)%2
if [ $evencheck == 0 ]  
then
    let median=(Amount_of_movies-Amount_of_0_movies)/2
else
    let median=(Amount_of_movies-Amount_of_0_movies)/2+1
fi

You have to wrap the equality sign with whitespace on both sides. Use a single equality sign.

您必须在两边用空格包裹等号。使用一个等号。

回答by Paused until further notice.

The outermost parentheses and none of the quotes are necessary in what you've got (they may be in other circumstances). Also, in a letstatement you can omit the dollar sign from variable names.

最外面的括号和引号在你所拥有的东西中是不必要的(它们可能在其他情况下)。此外,在let语句中,您可以省略变量名称中的美元符号。

(( evencheck = ( Amount_of_movies - Amount_of_0_movies ) % 2 ))
if (( evencheck == 0 ))
then
    (( median = ( Amount_of_movies - Amount_of_0_movies ) / 2 ))
else
    (( median = ( Amount_of_movies - Amount_of_0_movies ) / 2 + 1 ))
fi

If you use the (())form instead of let, you can uses spaces to make your formulas more readable. You can also use them in ifstatements:

如果您使用(())表格而不是let,您可以使用空格使您的公式更具可读性。您还可以在if语句中使用它们:

[ "$evencheck" == 0 ]

回答by ennuikiller

try this in your if test:

在你的 if 测试中试试这个:

(6-1) % 2
5 % 2
1, not 0

回答by Eton B.

Does median have some sort of default value? The way I see it, it's not even going inside the if.

中位数是否有某种默认值?在我看来,它甚至没有进入 if。

##代码##