在 Bash 中对一系列数字使用大小写 [英] Using case for a range of numbers in Bash
问题描述
我正在尝试在 Bash(在 Linux 中)中使用 case
执行以下操作.
I am trying to do the following using case
in Bash (in Linux).
如果X在460到660之间,输出X信息.
If X is between 460 and 660, output X information.
如果 X 介于 661 和 800 之间,请执行其他操作.
If X is between 661 and 800, do something else.
等等
现在这就是我所拥有的:
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
但不知何故,许多 480p、720p 或 1080p 的视频都以 DVD 结尾.变量 $MovieRes
是一个简单的列表,每行显示一个介于 1 和 1200 之间的数字.根据值,case
决定应用哪个case".
But somehow many of the ones that are 480p, 720p or 1080p are ending with DVD instead. The variable $MovieRes
is a simple list that shows, for each line, a number between 1 and 1200. Depending on the value, case
decides which "case" to apply.
我想知道如何实际使用 case
来完成此操作,因为在处理这样的范围时有点混乱.
I would like to know how to actually use case
to accomplish this since it is a bit confusing when dealing with ranges like this.
推荐答案
在bash中,可以使用算术表达式
:((...))
In bash, you can use the 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
这篇关于在 Bash 中对一系列数字使用大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!