Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected Operator error in shell script

Tags:

bash

shell

This is the shell code I am running:

#!bin/bash

while true
do
        req=$(curl http://localhost/devcalls/camerarequest.php)

        if [ "$req" == "1" ]
        then
                sudo bash /home/ckoy-admin/HAS_system/camera/cam.sh
        fi
done

and this is the error I get when I execute:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100     1  100     1    0     0     56      0 --:--:-- --:--:-- --:--:--    58
CAM.sh: 7: [: 1: unexpected operator

Please let me know what is wrong here.

like image 797
Debopam Parua Avatar asked Sep 13 '25 14:09

Debopam Parua


2 Answers

if [ "$req" = 1 ]

or even better

if [ "$req" -eq 1 ]

See the syntax and operators in man test.

like image 190
phd Avatar answered Sep 16 '25 09:09

phd


To compare INTEGER

if [ "$req" -eq 1 ]

To compare STRING

if [ "$req" = "string" ]
like image 37
AliRNazari Avatar answered Sep 16 '25 09:09

AliRNazari