I was trying to write a shell-script that would print all the arguments passed to the script. The condition is, the argument has to either be an uneven number or smaller than 23
#!/bin/bash
for (( i=0; i<$#; i++ ));
do
if [[ $i % 2 -ne 0 ]] || [[ $i -lt 23 ]];
then
echo $i
fi
done
When I try to Run this script as followed sh myScript.sh 1 3 4 6 24 23 22
My expected output would be 1 , 3 , 4 , 6 , 22 , 23
However I get the following Error:
myScript.sh: line 4: conditional binary operator expected
myScript.sh: line 4: syntax error near `%'
myScript.sh: line 4: ` if [[ $i % 2 -ne 0 ]] || [[ $i -lt 23 ]];'
Can anyone help me understand what the error means?
Thank you!
%is not an operator in [[...]]. Useif ((i % 2 != 0 || i < 23)).myScript.sh: line 5: syntax error near unexpected token `then' myScript.sh: line 5: ` then'bash myScript.sh 1 3 ...if ((i % 2 != 0 || i < 23)); then echo $i; fishould work.