2

I am trying to use case to run this function

if [[ $input -gt 0 || $input -eq 0 ]]; 

Is it possible to put in case to test the input for greater than 0 or equal to 0, or even 0 and less than 0, in case.

1
  • Yes. tldp.org/LDP/abs/html/comparison-ops.html if [ "$a" -ge "$b" ] means "greater than or equal to". So you can just do if [[ "${input}" -ge 0 ]];. But beware that Bash treats empty strings and all non-numeric strings as "0" (basically anything that cannot be coerced into a number), which can really bite you if you are truly looking for "0"! Commented Oct 27, 2024 at 4:48

4 Answers 4

3

This sample sh script validates that a variable is an integer.

#!/bin/sh num=$1 if [ $num -eq $num ] 2> /dev/null then case 1 in $(($num < 0)) ) echo $num is negative.;; $(($num <= 100)) ) echo $num is between 0 and 100.;; $(($num <= 1000)) ) echo $num is between 100 and 1000.;; * ) echo $num is greater than 1000.;; esac else echo $num is not a number. fi 

We verify the variable is a number by using the -eq operator. We need to suppress its output to stderr in case it's not a number.

If the variable contains an integer, the case will then evaluate ranges in the laid out form. The expressions $(( some comparison )) will either evaluate to "true" (1) or "false" (0); this way, you can test any given list of ranges.

3
  • I’ll admit that the other answers to this question are poor examples of this, but we like our answers to include an explanation. Why are you using expr when you don’t need to?  Why are you using a constant word and a variable pattern in your case statement. This seems unnecessarily complicated and convoluted. Commented Oct 14, 2017 at 1:43
  • I've modified the script to make it clear and added an explanation of what it does. Hope it helps. Commented Oct 14, 2017 at 16:40
  • Shellcheck: SC2004 (style): $/${} is unnecessary on arithmetic variables. May lead to "subtle bugs": shellcheck.net/wiki/SC2004 Commented Jul 27, 2023 at 11:27
0

If only integers need to be handled and -0 is not need to be handled correctly, the following works:

case "$input" in ''|*[!0-9-]*|[0-9-]*-*) echo "invalid input" ;; [0-9]*) echo "input >= 0" ;; -[1-9]*) echo "input < 0" ;; *) echo "invalid input" ;; esac 

But it is usually better to use if .. then .. elif ... then .. else .. fi constructs for distinction of cases with more complicated expressions than case with pattern matching.

6
  • This won't work for something like 123asd, -1aa. Commented Jan 15, 2015 at 9:45
  • @cuonglm Is there other ways to catch those, i'm doing a input for stock, if the stock is 0 or less than 0, the command will fail, but if the user keys in other characters like string + integer, how do i catch those Commented Jan 15, 2015 at 9:47
  • @jofel: No, your solution can't catch 123ad. Commented Jan 15, 2015 at 9:51
  • @jofel if i were to use if, is it possible to combine -ge and -eq together Commented Jan 15, 2015 at 10:00
  • @Zac - combine them...? You mean [ num -gt -1 ]? Commented Jan 15, 2015 at 10:06
0
[ "$input" -gt -1 ] || exit 

...or...

case ${input:?NULL} in (?*[!0-9]*|[!-0-9]*|[!0-9]) ! : ;; (-*[!0]*) echo \<0 ;; (*[!0]*) echo \>1 ;; (0*) echo 0 ;; esac 
3
  • @jofel - to which alternative do you refer? If you mean (*) matching zero then have you tested this? The first var expansion exits the shell for a null value. The next in line matches any char which is not 0-9, the next any which is not 0 - 1 or more 0 characters is the only possibility there. Commented Jan 15, 2015 at 10:21
  • sorry, I was wrong. Thought your are using bashims. Commented Jan 15, 2015 at 10:27
  • @jofel - I don't think so - that should be fully POSIX portable syntax. Besides - I never much liked bash. Commented Jan 15, 2015 at 10:40
0
case $([[ $input =~ ^-?[0-9]+$ ]] && [[ $input -gt 0 || $input -eq 0 ]] ; echo $?) in 0) echo "good" ;; 1) echo "bad" ;; *) echo "weird" ;; esac 

If you do not like the complexity inside the case, at least the regex matching works well and simple.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.