LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   test number within a range (https://www.linuxquestions.org/questions/programming-9/test-number-within-a-range-608342/)

tostay2003 12-21-2007 08:15 AM

test number within a range
 
I am trying to test whether a number is assigned to a variable and is it within certain range (in a single line statement).

But the below code is giving an error saying -z is an unknow operator. Any clues on how to implement or mistake i have made

Code:

[[-z "$Id"] -o [!grep -q $Id [1-8]]] && _Function1

gnashley 12-21-2007 08:30 AM

When using complex tests (-o or -a) you can't use the double brackets. Try somethiong like this:
Code:

[[ -z "$Id" ]] || [[ ! $(grep -q $Id [1-8]) ]] && _Function1
or:
Code:

[ -z "$Id" -o ! $(grep -q $Id [1-8]) ] && _Function1
When you do use double brackets you have to leave a space between the brackets and the enclosed code.

ghostdog74 12-21-2007 08:42 AM

Quote:

Originally Posted by tostay2003 (Post 2998054)
I am trying to test whether a number is assigned to a variable and is it within certain range (in a single line statement).

But the below code is giving an error saying -z is an unknow operator. Any clues on how to implement or mistake i have made

Code:

[[-z "$Id"] -o [!grep -q $Id [1-8]]] && _Function1

KISS and take it step by step
Code:

id=8
case $id in
    [1-8]) function1 ;;
    *) echo "no";;
esac



All times are GMT -5. The time now is 09:23 AM.