Basic bash if statement with hg fails

0

I'm running Cygwin. I came across the following snippet of Bash code from the FFmpeg Wiki's guide to compiling FFmpeg:

cd /ffmpeg_sources && if cd x265 2> /dev/null; then hg pull && hg update && cd ..; \
else hg clone https://bitbucket.org/multicoreware/x265; fi

As I understand it, it's a pretty basic if statement that returns non-zero if a CD into directory x265 fails (i.e. if it doesn't exist), and then clones from the Mercurial repository, which would create the x265 folder ready to be pulled and updated the next time.

The error from the failed cd x265 is repressed as expected, but then I also get the following output:

abort: no repository found in '/ffmpeg_sources' (.hg not found)!

This output seems to be a result of the then hg pull && hg update && cd ..; portion of the statement, and is unexpected because at that point it should simply fall back to cloning the directory.

The only thing that occurs to me is that the code on the FFmpeg Wiki is outdated, and that Mercurial has since then implemented a fatal abort on unsuccessful location of the .hg directory. Am I right, or am I missing something obvious here?

Hashim

Posted 2019-03-13T00:03:37.507

Reputation: 6 967

Answers

1

Just debug your if (which is wrong, obviously) with existing and non-existing target directory

Something like (from a memory, sorry, I don't have bash-shell now)

if cd x265 2> /dev/null; then echo "The directory exist"; else echo "The directory doesn't exist"; fi

because now:

  • cd failed (re-read part of error-message "... in'/ffmpeg_sources'")
  • but pull instead of clone part is executed

It can be "always zero exit code of cd"

Probably reason of such strange behavior described here with solution

Lazy Badger

Posted 2019-03-13T00:03:37.507

Reputation: 3 557

Wow, great catch! My cd is indeed aliased to a function that also does ls immediately after, which is obviously why it was returning true, I've just had it that way for so long it slipped my mind. And good point about the debugging tip, I'll need to bear that in mind for the future. Thanks for your help. – Hashim – 2019-03-13T02:38:01.093