syntax error: operand expected (error token is "<="

I wrote a script to execute the factorial command, but I'm getting this error

((: 1 <= : syntax error: operand expected (error token is "<=")

I can't fix it. The <= symbol was tried with both spaces and no spaces, but the same all the time. Below is the entire script to which the error indicated above is displayed on the 5th line

!/usr/bin/bash i=1 #nie-zero bo tworzy iloczyn wynik=1 # nie-zero bo tworzy iloczyn while (($i <= $1)) do wynik=$(($wynik * $i)) i=$(($i + 1)) done echo $wynik

Proszę o pomoc;(

4

1 Answer

You forgot to pass a parameter to your script:

$ bash -xc 'i=1; (($i <= $1))' bash 2 ; echo $?
+ i=1
+ (( 1 <= 2 ))
0

but

$ bash -xc 'i=1; (($i <= $1))' bash ; echo $?
+ i=1
+ (( 1 <= ))
bash: ((: 1 <= : syntax error: operand expected (error token is "<= ")
1

There is a lesson here about validating inputs...

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like