LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash : test how can I refine the conditions ? (https://www.linuxquestions.org/questions/programming-9/bash-test-how-can-i-refine-the-conditions-4175546428/)

lwv962 06-25-2015 11:51 AM

Bash : test how can I refine the conditions ?
 
Hi,

The 'if' statement works fine in selecting a number from 0 to 68.
What I cannot fathom is how to give the option of getting a suffixed
number eg. 56c to work.I've searched high and low.
I should be most grateful for a solution .

Code:

echo
read -p  "Choose a number,space then Enter >:  "  no  word
echo


if [[ $no -le 68 ]];then
        grep -irw --colour=auto "$word" ${bib[$no]}
        echo
        cwd="$(grep -oiw "$word" ${bib[$no]} |wc -l)"
        echo "$cwd word/s as entered"
elif [[ $no -le  68 +[a-z]      ]];then
        echo
        grep  -rw --colour=auto "$word"${bib[$no]}
        dwd="$(grep -ow "$word" ${bib[$no]} |wc -l)"
    #    echo "$dwd word/s as entered "
fi


millgates 06-25-2015 12:03 PM

Quote:

Originally Posted by lwv962 (Post 5382902)
What I cannot fathom is how to give the option of getting a suffixed
number eg. 56c to work.

And what exactly is it supposed to do? "56c" is not a number. You can't compare it with -le.
Why would you want to have an alphabetic character in a number? What are you trying to accomplish in the first place?

danielbmartin 06-25-2015 03:10 PM

Quote:

Originally Posted by lwv962 (Post 5382902)
What I cannot fathom is how to give the option of getting a suffixed
number eg. 56c to work.

Is 58c a temperature in Centigrade? If so, you must separate the numeric from the C (or F for Fahrenheit) and test only the numeric portion.

See (for example) http://tldp.org/LDP/abs/html/string-manipulation.html

Daniel B. Martin

Ranamon 06-26-2015 01:41 AM

Quote:

Originally Posted by lwv962 (Post 5382902)
Hi,

The 'if' statement works fine in selecting a number from 0 to 68.
What I cannot fathom is how to give the option of getting a suffixed
number eg. 56c to work.I've searched high and low.

This is done quite easily:
Code:

RawData="56c"
Num=$(expr $RawData : '\([0-9]*\)[a-z]*')

echo $Num

Should return 56. You can work that into your script.

rtmistler 06-26-2015 06:30 AM

This all depends as to whether you need to check for valid entry first versus parse something such as your example.

You haven't indicated yet whether or not "56c" is a legal or illegal input value.

If it is legal input, then how do you interpret it? Hexadecimal? Or something else?

So let us know if you're looking for how to deal with HEX numbers in a script, or if you need advice on how to qualify input a valid versus not.

It's also confusing because your initial test validate between 0 and 68, and then suddenly you throw 56c into the mix ... that would be a way larger number if this were HEX, and if it's a meaningful punctuation character like "degrees C" then there's the option of stripping out the non-numeric parts like Ranamon says.

You really have to qualify what range of input you wish to allow and determine how you intend to deal with illegal input.


All times are GMT -5. The time now is 03:18 AM.