I recognize that my initial query might look too abstract, thus for some more context I would like to show you an example, taken from reset.sh in learn git by building an sql reference object course configuration for coderoad:
#!/bin/bash
if [[ ! -a sql_reference ]]
then
mkdir sql_reference
fi
find ./sql_reference -not -name '.' -not -name '..' -not -name 'sql_reference' -delete
cp -r ./.freeCodeCamp/sql_reference/. ./sql_reference
mv ./sql_reference/dotgit ./sql_reference/.git
This code is doing the following:
It checks for the presence of a directory named sql_reference
, if not it creates it. But then it tries to empty it and delete its contents, regardless if it was created just previously or if it existed before. Afterwards, it copies the content of an established backup and activates the git repository by renaming the dotgit
to .git
.
When I see this I reason to myself, the emptying of the directory should be only if it does exist, therefore it should be part of the check with if
. But then, I have to question, why to go all the trouble to check and delete if it looks like we do not care about the content, at all?
Why not to delete the directory and reconstructed with just a simple copy command?
rm -rf sql_reference # This will not exit with failure if no directory exists.
cp -r .freeCodeCamp/sql_reference . # It copies the backup to a directory with the same name.
mv sql_reference/dotgit sql_reference/.git # No need to pre-append the current directory
I would love to know what the intention was to see if I might be correct.