bash 检查一个数字是否在 shell 中

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

Check if a number is even in shell

bashmath

提问by rabotalius

I need to check if a number is even.

我需要检查一个数字是否是偶数。

Here's what I've tried.

这是我尝试过的。

newY="281"
eCheck=$(( $newY % 2 ))

echo $newY
echo $eCheck
while [ $eCheck -eq 0 ]; do
        newY=$((newY-1))
        eCheck=$(( $newY % 2 ))
        echo $newY
done

... returns eCheck = 1how can it be? 281/2 = 140.5

……回来eCheck = 1怎么可能?281/2 = 140.5

i've also tried using bc, but it went into an infinite loop eCheck=$(echo "scale=1;$newY%2" | bc)

我也试过使用bc,但它进入了无限循环eCheck=$(echo "scale=1;$newY%2" | bc)

回答by jderefinko

Nici is right, "%" is the modulo, and gives you the remainder of the division.

Nici 是对的,“%”是模数,并为您提供除法的余数。

Your script can be simplified as follows :

您的脚本可以简化如下:

if [[ $((var % 2)) -eq 0 ]];
   then echo "$var is even"; 
   else echo "$var is odd"; 
fi

回答by blackSmith

You can do a simple :

你可以做一个简单的:

eCheck=$(( $newY & 1 ))

to utilize the bitwise operators in bash.

在 bash 中使用按位运算符。

回答by rici

The %operator computes the remainder. So 281 % 2is 1, because 281divided by 2is 140with a remainder of 1.

%运算符计算余数。所以,281 % 21的,因为281除以2就是140用剩余1

回答by Devaashish Sharma

#!/usr/bin/env bash

[[ $( expr  % 2 ) -eq 0 ]] && echo "Even Number" || echo "Odd Number"

回答by Jeff N

You are so close! Think of it like this. There are only two possible answers for Y in the expression

你离得那么近!像这样想。表达式中 Y 只有两个可能的答案

Y = X % 2

for ALL values of X. What are they? Play with a few values of X to see if you can come up with the values for Y.

对于 X 的所有值。它们是什么?试玩几个 X 值,看看你是否能想出 Y 的值。

Next, is there anything you can determine about what the value of Y says about the value of X? That is, can you use the value of Y to answer the problem you are trying to solve?

接下来,您是否可以确定 Y 的值对 X 值的影响?也就是说,你能不能用 Y 的值来回答你试图解决的问题?

回答by OM Prakash Singh

As you mention you are checking even for single no, So there is no need to use a loop. Here is my bit of code.

正如您所提到的,您甚至在检查单个否,因此无需使用循环。这是我的代码。

read -p "Enter a number: " num
if [ $((num%2)) -eq 0 ]
then
  echo "Entered Number is even:"
else
  echo "Entered Number is odd:"
fi

回答by codeforester

Since this question is tagged as Bash, the right way to check if a number is even in Bash is:

由于此问题被标记为 Bash,因此在 Bash 中检查数字是否为偶数的正确方法是:

if ((num%2 == 0)); then
    echo "The number is even"
fi

or, more even shorter:

或者,更短:

if ((num % 2)); then
    echo "The number is even"
fi

We don't need to use [[ ... ]]in this case.

[[ ... ]]在这种情况下我们不需要使用。



See also:

也可以看看:

回答by Harpreet Khanduja

#!bin/bash
echo "Type the input integer, followed by [Enter]:"
read x
if [ $((x%2)) -eq 0 ]; then 
  echo "$x is even"
else
  echo "$x is odd"
fi

Yes "%" is modulo, it gives you the remainder like others have mentioned

回答by Saurabh

This can be done using expr

这可以使用 expr 来完成

evenCheck=$(expr $newY % 2)
if [ $evenCheck = 0 ] ;
    then echo "number is even"; 
else echo "number is odd";
fi 
done