0

I have an error that is hard to clear. All that is supposed to happen is to test two conditions and then do something. The goal is to is it is empty or if it's a match.

if [ -z "$_BRANCH"  ] || [[ "$_BRANCH" = "master" ]]; then
    _FOE_BRANCH="master"
    [ -s "$module_dir/branch.gitploy" ] && _FOE_BRANCH=$(cat "$module_dir/branch.gitploy")
    [ "$_FOE_BRANCH" -ne "master"] && _BRANCH=$_FOE_BRANCH
fi

for some reason I keep getting this error

[: missing `]'

for this line

if [ -z "$_BRANCH"  ] || [[ "$_BRANCH" = "master" ]]; then

but as I read up to try to clear it I keep going in loops. I expected everything to be ok based on https://stackoverflow.com/a/16138899/746758 .

I wanted to head this off that I did look at the like posts and didn't post this with out many failed corrections, so as far as I see it atm, this is not a duplicate to Bash if statement [: missing `]' error or Brackets in if condition: why am I getting syntax errors without whitespace? , as far as I can tell there is no white space issue as they had in the answers. I believe I have a match on the lexicon and I would think it's a different issue and need a solution to it.

Quantum
  • 761
  • Are you sure the error is not for the fourth line, where you definitely have missing whitespace? – Michael Homer Jan 18 '15 at 02:38
  • I humbly disagree with "a problem that can't be reproduced". A whitespace issue is a lexicon issue that is related to the shells that would be in Linux and very reproducible. If this SO is not the right place then which SO is this question better suited? – Quantum Jan 20 '15 at 00:53
  • The key part is "or went away when a typo was fixed". The close reason is not a suggestion that you did something wrong — the question's just not a useful resource for anybody else. – Michael Homer Jan 20 '15 at 02:27
  • I would normally agree, but this is no typo, this is a lexicon issue. It's be the same as if I put a question up about "JS if statement failing" and put up code "if{x=x}(console.log('match'));" which would be a total fail but it's not a typo really. Here, whitespace at points can be super critical. If it was a var name typo I would 100% agree. you'll mark it as you wish but imho this is not typo as much and googling for this didn't really help highlight the real information I needed which was in [] there must be a space between each part. just my2cents form some one learning – Quantum Jan 20 '15 at 06:44
  • Both of the questions you linked provide that information. – Michael Homer Jan 20 '15 at 07:05

1 Answers1

5

Are you sure the error is for the first line? The last test is missing a space. Change

[ "$_FOE_BRANCH" -ne "master"] && _BRANCH=$_FOE_BRANCH

to

[ "$_FOE_BRANCH" -ne "master" ] && _BRANCH=$_FOE_BRANCH

Also, since you are talking about sh, which may not be necessarily bash, you may change the first line to keep using [ instead of [[, which may not work on some shells:

if [ -z "$_BRANCH"  ] || [ "$_BRANCH" = "master" ]; then
admirabilis
  • 4,712
  • oh gosh I feel dump cause the line numbering was off by 3 lines ... yes.. yes you are right thank you – Quantum Jan 18 '15 at 02:41
  • you are saying if [ -z "$_BRANCH" ] || [ "$_BRANCH" = "master" ]; then is more portable? – Quantum Jan 18 '15 at 02:45
  • @jeremy.bass Yes, [[ is not specified by POSIX, although it is a powerful bashism. See http://mywiki.wooledge.org/BashFAQ/031 – admirabilis Jan 18 '15 at 02:48