0
#! /bin/bash

number=$1
if [ $number -gt 9 -o $number -lt 100 ]
        then
            if [ $number -eq 10 ]
            then
                echo Ten
            exit
            elif [ $number -eq 11 ]
            then
                echo Eleven
            else
                echo Thirteen
            fi
fi

This is my code but only in case input value is 10 it is printed, 11 does not show up in case I input 11 and also with other values as well. Do you know the reason here?

-----------edited

number=$1
output=""

if [ $number -lt  0 -a $number -gt 999 ]
then
    echo put the right input between 0 and 999
fi

case "$number"
in
    [0-9])
        if [ $number -eq 0 ]
        then
            echo Zero
        elif [ $number -eq 1 ]
        then
            echo One
        elif [ $number -eq 2 ]
        then
            echo Two
        elif [ $number -eq 3 ]
        then
            echo Three
        elif [ $number -eq 4 ]
        then
            echo Four
        elif [ $number -eq 5 ]
        then
            echo Five
        elif [ $number -eq 6 ]
        then
            echo Six
        elif [ $number -eq 7 ]
        then
            echo Seven
        elif [ $number -eq 8 ]
        then
            echo Eight
        elif [ $number -eq 9 ]
        then
            echo Nine

        fi
    ;;
    10|99)
        if [ $number -gt 9 -a $number -lt 100 ]
        then
            if [ $number -eq 10 ]
            then
                echo Ten
            elif [ $number -eq 11 ]
            then
                echo Eleven
            else
                echo Thirteen
            fi
        fi
    ;;
    100|999)
    if [ $number -gt 99 -a $number -lt 1000 ]
        then
            echo Two
        fi
    ;;

esac

Here ( 10|99 ) also does not show up the value except 10..

5
  • 3
    I copy/pasted your code. works here for 10, 11 or 12 (which prints Thirteen). But the -o (or) in the first test should probably be a -a (and) to make more sense: any number is always greater than 9 or lower than 100
    – A.B
    May 22, 2018 at 23:05
  • 1
    You'll get Thirteen for an input of 10000 too. The first if will always be true for an integer value. Was that -o supposed to be -a? May 22, 2018 at 23:09
  • Oh,, my code works well? I am using vmware workstation, do you know why it happened for me ? May 22, 2018 at 23:10
  • ^^ Not unless you show us (in your question) how you're calling the code. May 22, 2018 at 23:11
  • I edited the code same as previous part, actually when I tried the previous code it worked, but next code is also not working now except 10.. May 22, 2018 at 23:33

1 Answer 1

1

This will always be true for numbers, since the condition is or:

if [ $number -gt 9 -o $number -lt 100 ]

That first scriptlet recognizes Ten and Eleven and prints Thirteen for all other values.


This will always be false, since the condition is and:

if [ $number -lt  0 -a $number -gt 999 ]

I'm not sure why you're building the rest with both a case and the ifs. If you just want to map the numbers to words, one at a time, a single case would do:

case "$number" in
     0) echo Zero ;;
     1) echo One ;;
# ...
     9) echo Nine ;;
    10) echo Ten ;;
    11) echo Eleven ;;
esac

That won't recognize numbers with leading zeroes (but all arithmetic operations would take them as base-8 numbers anyway, unless you're careful to force the base manually each time).


If you want to go higher than twenty, matching one at a time isn't going to be very nice, and you can't match arbitrary number ranges with case. In case, 10|99 will match exactly those two strings (not 12, or 123), and [0-9] matches single digits, so e.g. [0-100] is just the same as [01]. So an if might be better:

if   [[ $number -ge 20 -a $number -lt 30 ]]; then
    echo Twenty-something
elif [[ $number -ge 20 -a $number -lt 30 ]]; then
    echo Thirty-something
fi

You should use [[ .. -a .. ]] or [ .. ] && [ .. ] here, as [ .. -a .. ] as issues with some values (like "!"). If you do use [ .. ], quote the variables, i.e. [ "$number" -lt 123 ] instead of [ $number -lt 123 ].

This should work, though, but only because you can match a particular digit position:

case $number in
    2?) echo Twenty-something ;;
    3?) echo Thirty-something ;;
esac

If you actually want to give names to all numbers from zero to one thousand, you'll need to split the problem in parts (ones, tens, how to combine them) and preferably use functions for that.

1
  • Thank you for detailed explanation, but what I want to implement is the thing you referred as a last part, I need to implement all numbers from zero to one thousand and those have to show for the words as well, but I am still doubt for that how to implement...Could you give me an idea for that? Thanks.. May 23, 2018 at 15:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .