-1

I am in need of some assistance I cannot for the life of me seem to understand bash case statements. What I'm trying to do is to get input from the user, their annual income, and reply back with their respective tax bracket. But i cannot seem to get the cases to work with a range of numbers. Any help is greatly appreciated. thanks. Here is my code so far:

#!/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
yoprogrammer
  • 91
  • 1
  • 6
  • `case` is for shell patterns, not number ranges. I think your question is a dupe of this one: http://stackoverflow.com/q/12614011/1426891 – Jeff Bowman Sep 30 '15 at 01:12

1 Answers1

2

The case statement understands shell patterns and not number ranges. You could rewrite your program as follows:

#!/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
Simon Fromme
  • 3,104
  • 18
  • 30