What is the problem with the following shell script? I'm getting
[: too many arguments
error
#!/bash
var1=10;
var2=20;
if [ $var1 % 2 -eq 0 ] -a [ $var2 %5 -eq 0 ];
then
#something
fi
|
feedback
|
|
First, the
However, since you appear to be writing your script for bash, you can use a different command and avoid the extra step:
And second, you cannot chain commands with Three different variants:
Always remember, the syntax of | |||
|
feedback
|
|
You seem to be confusing Conditional Evaluation with Arithmetic Evaluation. Not to mention that your shebang line is wrong, unless for some reason you installed a copy of bash at the root level of your boot drive. Last but not least, a newline is the same as a semicolon. You don't need to end a line with a semicolon in shell scripting. The semicolon just exists to allow you to put two statements on one line. Here's a cleaned up version of your script:
#!/bin/bash
VAR1=10
VAR2=20
if (( (VAR1 % 2 == 0) && (VAR2 % 5 == 0) )); then
: #something (colon is a no-op)
fi
| |||||
|
feedback
|
%and%5are valid arguments for[? – Ignacio Vazquez-Abrams Jan 6 at 7:06