3
#!/bin/bash
A='';B='';C='';D=''
function returnFunctionWebService()
{
        touch test.json
        curl http://localhost:9099/BelattarProject/StudentMail.php -o test.json
        for line in `cat test.txt`;do
                echo $line
                A=$(echo $line | cut -d ',' -f1)
                echo $A
                B=$(echo $line | cut -d ',' -f2)
                echo $B
                C=$(echo $line | cut -d ',' -f3)
                echo $C
                D=$(echo $line | cut -d ',' -f4)
                echo $D
        done
}
returnFunctionWebService
echo $A $B $C $D
if [ -n $A ]; then
        if(( "$A" = "\"test1\"" )); then
                echo -e "c'est juste"
                exit
        else
                echo -e "c'est pas juste"
                exit
        fi
fi

i have an error at this end which is ./scriptWeb.sh: line 22: ((: "test1" = "test1" : syntax error: operand expected (error token is ""test1" = "test1" ") some help please

codeforester
  • 39,467
  • 16
  • 112
  • 140
Omar Amaoun
  • 526
  • 1
  • 3
  • 20

1 Answers1

4

(( )) can only be used for integer arithmetic, not for string comparisons. For strings, use [[ ]] construct, this way:

if [[ "$A" = "\"test1\"" ]]; then

There are many issues in your script. Get it checked at shellcheck


See also:

codeforester
  • 39,467
  • 16
  • 112
  • 140
  • 4
    @RavinderSingh13 No, `=` indeed does string comparison :) – PesaThe Mar 04 '18 at 17:40
  • @PesaThe, really, could you please paste me the link(I will also learn from it) :), honestly didn't know it. – RavinderSingh13 Mar 04 '18 at 17:45
  • 2
    @RavinderSingh13 Many results if you search "bash compare string". https://askubuntu.com/questions/351765/bash-if-statement-compare-two-strings – llllllllll Mar 04 '18 at 17:47
  • i still have the same problem it dosen't solve it yet ^^ – Omar Amaoun Mar 04 '18 at 17:49
  • 1
    Not just "should"; it can *only* be used for arithmetic. `((...))` is the arithmetic compound command. – chepner Mar 04 '18 at 18:12
  • 2
    @OmarAmaoun If you made this change, you should not have the *same* problem. However, your script has many other problems that you need to work through. Follow thatotherguy's advice and use `jq` to parse json, and codeforster's advice to use shellcheck.net to spot common script errors. Also, read the answers to ["How to debug a bash script?"](https://stackoverflow.com/questions/951336/how-to-debug-a-bash-script/), and look through the many in the [bash tag info entry](https://stackoverflow.com/tags/bash/info). – Gordon Davisson Mar 04 '18 at 21:49