1

i am studying shell script right now. I started to learn how to work with more complex if statements. What's wrong with this code bellow? I read other similar questions here in stackoverflow, but i couldnt resolve my problem. Now im verifying if the first, second or third argument is null. In the future i pretend to verify based in some regex or something like that.
Thanks!!

The code (line 9):

if [ "$1" -eq "" ] || [ "$2" -eq "" ] || [ "$3" -eq "" ] then ...

line 9: [: : integer expression expected
line 9: [: : integer expression expected
line 9: [: : integer expression expected

Barmar
  • 741,623
  • 53
  • 500
  • 612

2 Answers2

2

-eq performs an arithmetic comparison between two numbers. Use = for string comparisons. Or better yet, use [[ and ==.

[[ $1 == "" ]]
[ "$1" = "" ]

You can also use -z and -n to directly test whether a value is empty/non-empty.

[[ -n $value ]]    # [[ $value != "" ]]
[[ -z $value ]]    # [[ $value == "" ]]
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
-1

use [[ and ]] for the more modern / complex operators. This is a bashism, so beware.

Jake H
  • 1,720
  • 1
  • 12
  • 13