Syntax Error: Operand Expected (Error Token Is "

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

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

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.