带有数字范围的Bash case语句

时间:2015-09-30 01:04:31

标签: bash range controls case statements

我需要一些帮助,我不能为我的生活似乎理解bash案例陈述。我想要做的是从用户那里获得投入,他们的年收入,并回复他们各自的税收支持。但我似乎无法让案件与一系列数字一起使用。任何帮助是极大的赞赏。谢谢。到目前为止,这是我的代码:

#!/bin/bash

echo -e "What is your annual income? "
read annual_income

case $annual_income in 
    0) echo "You have no taxable income." ;;
    [24100-42199]) echo "With an annual income of $annual_income, you are in the lowest income tax bracket with a total effective federal tax rate of 1.5%." ;;
    [42200-65399]) echo "With an annual income of $annual_income, you are in the second income tax bracket with a total effective federal tax rate of 7.2%." ;;
    [65400-95499]) echo "With an annual income of $annual_income, you are in the middle income tax bracket with a total effective federal tax rate of 11.5%." ;;
    [95500-239099]) echo "With an annual income of $annual_income, you are in the fourth income tax bracket with a total effective federal tax rate of 15.6%." ;;
    [239100-1434900]) echo "With an annual income of $annual_income, you are in the highest income tax bracket with a total effective federal tax rate of 24%." ;;
esac

1 个答案:

答案 0 :(得分:2)

案例陈述理解shell patterns而不是数字范围。您可以按如下方式重写您的程序:

#!/bin/bash

echo -e "What is your annual income? "
read annual_income

if (($annual_income == 0)) 
then
    echo "You have no taxable income."
elif ((24100 <= $annual_income && $annual_income <= 42199))
then
    echo "With an annual income of $annual_income, you are in the lowest income tax bracket with a total effective federal tax rate of 1.5%."
elif ((42200 <= $annual_income && $annual_income <= 65399)) 
then
    echo "With an annual income of $annual_income, you are in the second income tax bracket with a total effective federal tax rate of 7.2%."
elif ((65400 <= $annual_income && $annual_income <= 95499))
then
    echo "With an annual income of $annual_income, you are in the middle income tax bracket with a total effective federal tax rate of 11.5%."
elif ((95500 <= $annual_income && $annual_income <= 239099)) 
then
    echo "With an annual income of $annual_income, you are in the fourth income tax bracket with a total effective federal tax rate of 15.6%."
elif ((239100 <= $annual_income && $annual_income <= 1434900)) 
then 
    echo "With an annual income of $annual_income, you are in the highest income tax bracket with a total effective federal tax rate of 24%."
fi