1

I'm trying to get a list of some deleted files in my git repo using a bash script. This however, needs to be run in a docker container and in a one liner.

I also want to only create the list of the files if there are actually any deleted files.

What I'm currently doing is:

[[ "$(git ls-files --deleted)" =~ "filename" ]] && git ls-files --deleted >! deleted_files.txt

Locally, this works fine. When I try in docker:

docker exec my-container bash -c "[[ $(git ls-files --deleted) =~ "filename" ]] && git ls-files --deleted >! deleted_files.txt"

I get this error, however:

bash: -c: line 0: conditional binary operator expected
bash: -c: line 0: syntax error near `filename'
bash: -c: line 0: `[[  =~ filename ]] && git ls-files --deleted >! deleted_files.txt'

My bash knowledge is extremely lacking, so there is a great chance I'm doing something wrong, but not exactly sure what.

Thanks in advance!

pteixeira
  • 1,617
  • 3
  • 24
  • 40

1 Answers1

1

You have to run your command in single quotes. When Bash sees $variable in double quotes it expands it before running the command so what you get is =~ "filename". It should be:

docker exec my-container bash -c '[[ $(git ls-files --deleted) =~ "filename" ]] && git ls-files --deleted >! deleted_files.txt'

Also make sure you cd to the correct directory before running git ls-files.

If you want to use in Makefile you have to escape each $$ or you will get the same error as if you use double quotes. It could be:

docker exec gallant_stonebraker bash -c 'cd /all ; [[ $$(git ls-files --deleted) =~ "filename" ]] && git ls-files --deleted >! deleted_files.txt'
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38