1

I'm new to bash scripting and I don't understand what's wrong with this specific part of my script. im trying to select all the exit that have in my script. and trying to put in conditon that if all those condition is not met then its not valid and else its valid.

if [[ `echo $?` -ne {0..4} ]]
then
         echo "Its a Valid Date."
 else
         echo " Not valid date."
fi

When I run it, I get:

kpatel138@matrix:~/Lab2> ./cal2 1994 5 4
./cal2: line 40: unexpected argument `(' to conditional binary operator
./cal2: line 40: syntax error near `(0'
./cal2: line 40: `if [[ `echo $?` -ne (0..4) ]]'
2
  • 1
    Hi and welcome to the site. You have a few obvious syntax errors but for more help, you need to tell us what you are actually trying to do. Your logic is wrong so I can't guess what it is that you want. Please edit your question and explain your objective. – terdon Oct 9 '14 at 17:51
  • 2
    And figure out whether it's {0..4} or (0..4) (braces and parentheses are not the same). Also, BTW, you can probably change `echo $?` to just plain $?. – G-Man Says 'Reinstate Monica' Oct 9 '14 at 17:52
2

`echo $?` is a convoluted way of writing $?. Don't do things the complicated way when there's an obvious simpler way.

I think you want to test whether $? is one of the values 0, 1, 2, 3 or 4. The syntax you're using doesn't make sense. You can test all five equality cases:

if [[ $? -eq 0 || $? -eq 1 || $? -eq 2 || $? -eq 3 || $? -eq 4 ]]; then …

But that's overly verbose. Since you want to allow the range 0–4, test that $? is more than the minimum value and less than the maximum value.

if [[ $? -ge 0 && $? -le 4 ]]; then …

$? is never negative, so the test $? -ge 0 is superfluous. Thus:

if [[ $? -le 4 ]]; then …

This can also be written using an arithmetic expression. Note that the operator is different: conditional constructs in bracketts use -eq, -ne, -le, -ge, -lt and -gt to compare integers, while arithmetic expressions have a C-like syntax.

if (($? <= 4)); then …

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.