22

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.

6
  • 1
    All I can think of is that there might be some invisible characters in your file. Did you type that in with vi? Commented Jun 17, 2017 at 12:44
  • 2
    How do you run the script? Commented Jun 17, 2017 at 12:44
  • 1
    Show content of echo "$req" | hexdump -C. Check your file for special characters with cat -A file or cat -v file. Commented Jun 17, 2017 at 12:52
  • 7
    You are running your script with sh CAM.sh, and sh is a shell (probably dash) that doesn't recognize == as a valid operator with the [ command. Commented Jun 17, 2017 at 12:54
  • 1
    @chepner.. yes, changing the == to -eq worked! Commented Jun 18, 2017 at 5:37

5 Answers 5

28
if [ "$req" = 1 ]

or even better

if [ "$req" -eq 1 ]

See the syntax and operators in man test.

Sign up to request clarification or add additional context in comments.

3 Comments

@phd..Adding the -eq instead of == worked.. Thank you.. :D
same for me, also had to put -ne instead of !=
If the script were actually executed by Bash, it would actually work; but the shebang is broken, so the script will have been executed by sh after all, which does not portably accept == in lieu of the correct and portable =. For the record, for the shebang to be valid, it needs to start with exactly the two bytes #! followed by the absolute path to the desired interpreter (i.e. /bin/bash, not just bin/bash).
20

To compare INTEGER

if [ "$req" -eq 1 ]

To compare STRING

if [ "$req" = "string" ]

Comments

2

using bash run.sh instead of sh run.sh worked for me.

Comments

1

View shell:

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Aug 11 2021 /bin/sh -> dash

This problem occurs due to the incompatibility between dash and bash. There are two ways to solve it:

  (1): sudo dpkg-reconfigure dash select NO
Change the dash of ubuntu's default shell link to traditional bash
lrwxrwxrwx 1 root root 4 August 11 09:53 /bin/sh -> dash (before modification)
lrwxrwxrwx 1 root root 4 Aug 11 09:53 /bin/sh -> bash
This problem occurs due to the incompatibility between dash and bash. .

  (2): Change == to =: because the default judgment statement in dash is =. .
For specific dash and bash, you can use man to find out. .

Comments

-1

if [ "expr n%2 -eq 0" ]

if [ "expr n%2=0" ]

or

if [ 'expr n%2=0' ]

if [ 'expr n%2 -eq 0' ]

you can use any 4 method above mentioned i have tried all other answers but including every thing inside the "" gave me the output

1 Comment

This is complete hallucination. The quotes turn this into a check for a non-empty string, which is true by definition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.