Problem with using if/then statement: `((: == : syntax error: operand expected (error token is “== ”)`











up vote
0
down vote

favorite












I try to implement positional parameters in bash script and check them, but have some issue in if/then switch statement.



Script:



#set "default" parameter
query=""

while [ "$1" != "" ]; do
case $1 in
-q | --query ) shift
query=$1
;;
esac
shift
done

#query cannot be empty
if (($query == ""))
then
echo 'no query'
exit
fi

echo "query - $query"


All works well when I set the correct parameters:



$ ./script.sh -q request
query - request


-q parameter cannot be empty and I need to validate it. So, when the command line looks like:



$ ./script.sh -q


or



$ ./script.sh


I get this error:



./main.sh: line 13: ((: == : syntax error: operand expected (error token is "== ")
query -


How to correctly implement if operator in this case?










share|improve this question




















  • 1




    This script lacks a shebang.
    – dessert
    Nov 15 at 22:29










  • Who and why lowered my question ??
    – Valentyn Hruzytskyi
    Nov 15 at 22:40










  • @wjandrea, thanks. Comments I did edit. But all this code I need to put, because if statement have different error in different parameters in command line (if parameter exist - I did not get an error in if... line), so I showed all.
    – Valentyn Hruzytskyi
    Nov 15 at 23:06












  • The edits are a huge improvement, thanks. I've deleted my earlier comment.
    – wjandrea
    Nov 15 at 23:15















up vote
0
down vote

favorite












I try to implement positional parameters in bash script and check them, but have some issue in if/then switch statement.



Script:



#set "default" parameter
query=""

while [ "$1" != "" ]; do
case $1 in
-q | --query ) shift
query=$1
;;
esac
shift
done

#query cannot be empty
if (($query == ""))
then
echo 'no query'
exit
fi

echo "query - $query"


All works well when I set the correct parameters:



$ ./script.sh -q request
query - request


-q parameter cannot be empty and I need to validate it. So, when the command line looks like:



$ ./script.sh -q


or



$ ./script.sh


I get this error:



./main.sh: line 13: ((: == : syntax error: operand expected (error token is "== ")
query -


How to correctly implement if operator in this case?










share|improve this question




















  • 1




    This script lacks a shebang.
    – dessert
    Nov 15 at 22:29










  • Who and why lowered my question ??
    – Valentyn Hruzytskyi
    Nov 15 at 22:40










  • @wjandrea, thanks. Comments I did edit. But all this code I need to put, because if statement have different error in different parameters in command line (if parameter exist - I did not get an error in if... line), so I showed all.
    – Valentyn Hruzytskyi
    Nov 15 at 23:06












  • The edits are a huge improvement, thanks. I've deleted my earlier comment.
    – wjandrea
    Nov 15 at 23:15













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I try to implement positional parameters in bash script and check them, but have some issue in if/then switch statement.



Script:



#set "default" parameter
query=""

while [ "$1" != "" ]; do
case $1 in
-q | --query ) shift
query=$1
;;
esac
shift
done

#query cannot be empty
if (($query == ""))
then
echo 'no query'
exit
fi

echo "query - $query"


All works well when I set the correct parameters:



$ ./script.sh -q request
query - request


-q parameter cannot be empty and I need to validate it. So, when the command line looks like:



$ ./script.sh -q


or



$ ./script.sh


I get this error:



./main.sh: line 13: ((: == : syntax error: operand expected (error token is "== ")
query -


How to correctly implement if operator in this case?










share|improve this question















I try to implement positional parameters in bash script and check them, but have some issue in if/then switch statement.



Script:



#set "default" parameter
query=""

while [ "$1" != "" ]; do
case $1 in
-q | --query ) shift
query=$1
;;
esac
shift
done

#query cannot be empty
if (($query == ""))
then
echo 'no query'
exit
fi

echo "query - $query"


All works well when I set the correct parameters:



$ ./script.sh -q request
query - request


-q parameter cannot be empty and I need to validate it. So, when the command line looks like:



$ ./script.sh -q


or



$ ./script.sh


I get this error:



./main.sh: line 13: ((: == : syntax error: operand expected (error token is "== ")
query -


How to correctly implement if operator in this case?







command-line bash scripts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 at 2:29









wjandrea

7,73642258




7,73642258










asked Nov 15 at 22:11









Valentyn Hruzytskyi

2009




2009








  • 1




    This script lacks a shebang.
    – dessert
    Nov 15 at 22:29










  • Who and why lowered my question ??
    – Valentyn Hruzytskyi
    Nov 15 at 22:40










  • @wjandrea, thanks. Comments I did edit. But all this code I need to put, because if statement have different error in different parameters in command line (if parameter exist - I did not get an error in if... line), so I showed all.
    – Valentyn Hruzytskyi
    Nov 15 at 23:06












  • The edits are a huge improvement, thanks. I've deleted my earlier comment.
    – wjandrea
    Nov 15 at 23:15














  • 1




    This script lacks a shebang.
    – dessert
    Nov 15 at 22:29










  • Who and why lowered my question ??
    – Valentyn Hruzytskyi
    Nov 15 at 22:40










  • @wjandrea, thanks. Comments I did edit. But all this code I need to put, because if statement have different error in different parameters in command line (if parameter exist - I did not get an error in if... line), so I showed all.
    – Valentyn Hruzytskyi
    Nov 15 at 23:06












  • The edits are a huge improvement, thanks. I've deleted my earlier comment.
    – wjandrea
    Nov 15 at 23:15








1




1




This script lacks a shebang.
– dessert
Nov 15 at 22:29




This script lacks a shebang.
– dessert
Nov 15 at 22:29












Who and why lowered my question ??
– Valentyn Hruzytskyi
Nov 15 at 22:40




Who and why lowered my question ??
– Valentyn Hruzytskyi
Nov 15 at 22:40












@wjandrea, thanks. Comments I did edit. But all this code I need to put, because if statement have different error in different parameters in command line (if parameter exist - I did not get an error in if... line), so I showed all.
– Valentyn Hruzytskyi
Nov 15 at 23:06






@wjandrea, thanks. Comments I did edit. But all this code I need to put, because if statement have different error in different parameters in command line (if parameter exist - I did not get an error in if... line), so I showed all.
– Valentyn Hruzytskyi
Nov 15 at 23:06














The edits are a huge improvement, thanks. I've deleted my earlier comment.
– wjandrea
Nov 15 at 23:15




The edits are a huge improvement, thanks. I've deleted my earlier comment.
– wjandrea
Nov 15 at 23:15










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










(( ... )) is for arithmetic evaluation.



You should use [ ... ] (just like you did in the while condition) or bash's extended test syntax [[ ... ]] - and remember to quote "$query" and leave whitespace around the [ and ] operators:



if [ "$query" == "" ]


Alternatively, use the -z empty string test:



if [ -z "$query" ]




For general help with questions like this, try www.shellcheck.net or install the shellcheck package from the universe repository.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1093318%2fproblem-with-using-if-then-statement-syntax-error-operand-expected%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    (( ... )) is for arithmetic evaluation.



    You should use [ ... ] (just like you did in the while condition) or bash's extended test syntax [[ ... ]] - and remember to quote "$query" and leave whitespace around the [ and ] operators:



    if [ "$query" == "" ]


    Alternatively, use the -z empty string test:



    if [ -z "$query" ]




    For general help with questions like this, try www.shellcheck.net or install the shellcheck package from the universe repository.






    share|improve this answer

























      up vote
      1
      down vote



      accepted










      (( ... )) is for arithmetic evaluation.



      You should use [ ... ] (just like you did in the while condition) or bash's extended test syntax [[ ... ]] - and remember to quote "$query" and leave whitespace around the [ and ] operators:



      if [ "$query" == "" ]


      Alternatively, use the -z empty string test:



      if [ -z "$query" ]




      For general help with questions like this, try www.shellcheck.net or install the shellcheck package from the universe repository.






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        (( ... )) is for arithmetic evaluation.



        You should use [ ... ] (just like you did in the while condition) or bash's extended test syntax [[ ... ]] - and remember to quote "$query" and leave whitespace around the [ and ] operators:



        if [ "$query" == "" ]


        Alternatively, use the -z empty string test:



        if [ -z "$query" ]




        For general help with questions like this, try www.shellcheck.net or install the shellcheck package from the universe repository.






        share|improve this answer












        (( ... )) is for arithmetic evaluation.



        You should use [ ... ] (just like you did in the while condition) or bash's extended test syntax [[ ... ]] - and remember to quote "$query" and leave whitespace around the [ and ] operators:



        if [ "$query" == "" ]


        Alternatively, use the -z empty string test:



        if [ -z "$query" ]




        For general help with questions like this, try www.shellcheck.net or install the shellcheck package from the universe repository.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 at 22:36









        steeldriver

        64.6k11100171




        64.6k11100171






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1093318%2fproblem-with-using-if-then-statement-syntax-error-operand-expected%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Ellipse (mathématiques)

            Quarter-circle Tiles

            Mont Emei