4

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 ?

3
  • consider using bash's argument handling. It will help you be more consistent with other programs. Commented Aug 5, 2015 at 13:28
  • 3
    Not part of this problem, but I'd put quotes around that variable ("$filename") in case, e.g., you end up with spaces in there. Commented Aug 5, 2015 at 13:36
  • 1
    @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. Commented Aug 5, 2015 at 14:26

1 Answer 1

9

[ 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.

2
  • It is traditional to click the tick if the answer works for you, no need to up-vote. Commented Aug 5, 2015 at 13:41
  • 1
    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. Commented Aug 5, 2015 at 14:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.