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
.
Related videos on Youtube
Author by
chhaya vishwakarma
Updated on September 18, 2022Comments
-
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 almost 9 yearsconsider using
bash
's argument handling. It will help you be more consistent with other programs. -
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 forzsh
), it doesn't need to be quoted.
-
-
chhaya vishwakarma almost 9 yearsAh!! I did silly mistake ..thanks for your answer
-
ctrl-alt-delor almost 9 yearsIt is traditional to click the tick if the answer works for you, no need to up-vote.
-
Stéphane Chazelas almost 9 yearsYou 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.