0

EDIT: I think that it's not picking up the $1 as the operand. I tried storing it in a variable, then trying the assignment but it seems that has no effect.

EDIT 2: provided a minimal reproducible script as requested.

The error generating part of the code is the following:

check() {
if [ $1 -lt $2 ]; then
 for((var=$1; var<$2; var++)); do
  if [ $((var%2)) -eq 0 ]; then
   echo "it's an even number"
   fi
  done
fi
}

if [ $# -eq 2 ]; then 
check
fi

the rest of the function's code will be attached below. The function is rather long to re-type out; I'm using ubuntu through a VM which doesn't allow for items to be copied into or pasted out of the VM, but this just may be a settings thing.

part 1 of the function

part 2 of the function

11
  • 1
    Try quoting all your variables and see if it helps. Commented Apr 10, 2020 at 4:41
  • Q: What is an example value for $1 when it fails? Add set -x to your script to observe each statement, and each variable. Commented Apr 10, 2020 at 4:42
  • it fails in ever scenario $1 is just an interger number passed through the command line anywhere from 0 - what ever number the user goes up to. Commented Apr 10, 2020 at 4:45
  • I tried quoting variables seems to have no effect. Commented Apr 10, 2020 at 4:47
  • Please provide a minimal verifiable example of the code showing the problem. That is, take that one line of code and put it into a minimal script that reproduces the problem. Commented Apr 10, 2020 at 4:56

1 Answer 1

1

You are invoking your function by

check

i.e. you don't pass any parameter, Hence, $1 and $2 are empty, so this can't work. You would have to write

check "$@"

or

check "$1" "$2"

depending on what exactly you want to achieve.

However, with your original code, you should then get for your if statement an error message

[: -lt: unary operator expected

UPDATE: As GordonDavisson pointed out in his comment, you won't get this syntax error here, because both operands are missing in your case and -lt then looses its meaning of being treated as operator.

BTW, if you had written the test as

if (( $1 < $2 )); then

you would have received a syntax error (bash: ((: < : syntax error: operand expected (error token is "< "))

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much the script is now working, I didn't receive that error message for my if statement for some reason.
If I were programming this, I would research why there is no error message on the [. A system not reporting an obvious error, is a bit creepy, and certainly not something I would ignore. You may want to post a new question for this topic, if you can't find it out by yourself.
@Nasri: BTW, you can write your tests on var simpler as, i.e. if (( var%2 == 0 )); then.
@user1934428 You get the "unary operator expected" error if one of the operands is missing. If both are missing, [ -lt ] is interpreted as a valid expression meaning "is the string '-lt' nonempty?", and the answer is yes, so it actually returns success. In other words, it is completely misinterpreting what the expression is supposed to mean. This is yet another example of why you should put double-quotes around variable/parameter references.
@GordonDavisson : Good point. I will edit my answer in this respect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.