Bash 中一系列数字的用例

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

Using case for a range of numbers in Bash

bashswitch-statement

提问by Luis Alvarado

I am trying to do the following using casein Bash (in Linux).

我正在尝试case在 Bash(在 Linux 中)中执行以下操作。

If X is between 460 and 660, output X information.

如果 X 在 460 到 660 之间,则输出 X 信息。

If X is between 661 and 800, do something else.

如果 X 介于 661 和 800 之间,请执行其他操作。

Etc.

等等。

Right now this is what I have:

现在这就是我所拥有的:

case $MovieRes in
    [461-660]*) echo "$MovieName,480p" >> moviefinal ;;
    [661-890]*) echo "$MovieName,720p" >> moviefinal ;;
    [891-1200]*) echo "$MovieName,1080p" >> moviefinal ;;
    *) echo "$MovieName,DVD" >> moviefinal ;;
esac

But somehow many of the ones that are 480p, 720p or 1080p are ending with DVD instead. The variable $MovieResis a simple list that shows, for each line, a number between 1 and 1200. Depending on the value, casedecides which "case" to apply.

但不知何故,许多 480p、720p 或 1080p 都以 DVD 结尾。该变量$MovieRes是一个简单的列表,它为每一行显示一个 1 到 1200 之间的数字。根据值,case决定应用哪种“案例”。

I would like to know how to actually use caseto accomplish this since it is a bit confusing when dealing with ranges like this.

我想知道如何实际使用case来实现这一点,因为在处理这样的范围时有点混乱。

回答by kev

In bash, you can use the arithmetic expression: ((...))

在 bash 中,您可以使用arithmetic expression((...))

if ((461<=X && X<=660))
then
    echo "480p"
elif ((661<=X && X<=890))
then
    echo "720p"
elif ((891<=X && X<=1200))
then
    echo "1080p"
else
    echo "DVD"
fi >> moviefinal

回答by dogbane

The bash casestatement doesn't understand number ranges. It understands shell patterns.

bashcase语句不理解数字范围。它理解shell 模式

The following should work:

以下应该工作:

case $MovieRes in
    46[1-9]|4[7-9][0-9]|5[0-9][0-9]|6[0-5][0-9]|660) echo "$MovieName,480p" >> moviefinal ;;
    66[1-9]|6[7-9][0-9]|7[0-9][0-9]|8[0-8][0-9]|890) echo "$MovieName,720p" >> moviefinal ;;
    89[1-9]|9[0-9][0-9]|1[0-1][0-9][0-9]|1200)       echo "$MovieName,1080p" >> moviefinal ;;
    *)                                               echo "$MovieName,DVD" >> moviefinal ;;
esac

However, I'd recommend you use an if-else statement and compare number ranges as in the other answer. A caseis not the right tool to solve this problem. This answer is for explanatory purposes only.

但是,我建议您使用 if-else 语句并比较其他答案中的数字范围。Acase不是解决这个问题的正确工具。此答案仅用于说明目的。

回答by Alvaro

Just for the pleasure of subverting case to do as you want, you can use $((...))

只是为了颠覆案例的乐趣,你可以使用 $((...))

case 1 in
    $(($MovieRes<= 460)))echo "$MovieName,???";;
    $(($MovieRes<= 660)))echo "$MovieName,480p";;
    $(($MovieRes<= 890)))echo "$MovieName,720p";;
    $(($MovieRes<=1200)))echo "$MovieName,1080p";;
                       *)echo "$MovieName,DVD";;
esac >> moviefinal

回答by Mike Q

Similar issue which might be useful to someone ... Random additional thing I just tried out where it checks also that it is an integer, for me I wanted it to have a preset value, let the user change it, if they input the wrong data it sets to default.

类似的问题可能对某人有用......我刚刚尝试了随机附加的东西,它也检查它是否是一个整数,对我来说我希望它有一个预设值,如果他们输入错误,让用户更改它它设置为默认值的数据。

    func_set_num_files(){
        echo "How many files do you want to create? (input a number 1-10000)"
        read X
        # 1, is it a number, #2 is it within max range?
        if [[ $X != *[!0-9]* ]]; then

            if ((1<=X && X<=10000)) ;then
                        echo "NUM_FILES=$X"
                        NUM_FILES=$X
                else
                        echo "Invalid input, setting to default value [ $NUM_FILES ].";sleep 3
            fi

        else
            echo "Invalid input, non-numeric values entered, setting to default value [ $NUM_FILES ].";sleep 3
        fi

    }

Another example using 'case' to check that a variable is in a range of integers :

另一个使用 'case' 检查变量是否在整数范围内的示例:

check that $MAX is a number and that it's between 50-100 :

检查 $MAX 是一个数字并且它在 50-100 之间:

            case $MAX in
                ''|*[!0-9]*)
                    echo "The value $MAX is not a number !"
                    exit 1
                ;;
                *)
                    if [ $MAX -lt 50 ] || [ $MAX -gt 100 ] ;then
                        echo "The value $MAX is not between 50-100"
                        exit 1
                    fi
                    echo "Looks like we are good !"
                ;;
            esac

回答by TheRusko0

I was looking for the simplest solution and found it difficult to use a case statementwith a number range.

我正在寻找最简单的解决方案,发现很难使用带有数字范围case 语句

Finally i found a really simple solution for zsh:

最后我找到了一个非常简单的zsh解决方案:

  #!/usr/bin/zsh 

 case "${var}" in

 <0-5461>)

 printf "${var} is between 0 and 5461"

 ;;

 <5462-10922>)

printf "${var} is between 5462-10922"

 ;;

 esac

and for Bash:

对于Bash

  #!/bin/bash

 case $((   

(var >= 0 && var <= 5461)      * 1 +   

(var > 5462 && var  <= 10922)   * 2)) in   

(1) printf "${var} is between 0 and 5461";; 

(2) printf "${var} is between 5461 and 10922";; 

esac

Hope it helps someone .

希望它可以帮助某人。