arithmetic syntax error in string compare

10,511

[ bla bla bla ] is equivalent to test bla bla bla.

From man test

   STRING1 = STRING2
          the strings are equal

   STRING1 != STRING2
          the strings are not equal

   INTEGER1 -eq INTEGER2
          INTEGER1 is equal to INTEGER2

Therefore you need = not -eq.

Share:
10,511

Related videos on Youtube

chhaya vishwakarma
Author by

chhaya vishwakarma

Updated on September 18, 2022

Comments

  • chhaya vishwakarma
    chhaya vishwakarma almost 2 years

    I have written a shell script which takes file name as parameter e.g user/test.txt. I want to make this file parameter optional if user does not wish to provide file name he/she can give as "None"

    Inside script I'm just checking if filename parameter contains "None"

    if [ $filename -eq "NONE" ];then
    cmd;
    fi
    

    When "None" is passed as parameter script works fine but when user/test.txt is passed I get below error message which I don't want to print on console

    arithmetic syntax error
    

    Can somebody help ?

    • ctrl-alt-delor
      ctrl-alt-delor almost 9 years
      consider using bash's argument handling. It will help you be more consistent with other programs.
    • Stéphane Chazelas
      Stéphane Chazelas almost 9 years
      @goldilocks, spaces or any character in$IFS, or wildcard characters. Funny how the quotes are put where they're not needed ("NONE") and not where they're needed (typical beginner mistake as that's true that it's counter-intuitive to anyone used to other kinds of languages). Note that [ itself is a glob character, though because it's not matched in a single word (or as a special case for zsh), it doesn't need to be quoted.
  • chhaya vishwakarma
    chhaya vishwakarma almost 9 years
    Ah!! I did silly mistake ..thanks for your answer
  • ctrl-alt-delor
    ctrl-alt-delor almost 9 years
    It is traditional to click the tick if the answer works for you, no need to up-vote.
  • Stéphane Chazelas
    Stéphane Chazelas almost 9 years
    You upvote if it's a good answer. You accept if it's the best/fittest answer that answers your question. Typically, you'll want to upvote the answer you accept unless you think it's not a good answer.