1

I need to do validation on git clone [bitbucket Git URL]using shell script. Running the above command gives fatal: destination path 'sweta-test-parameter' already exists and is not an empty directory.

I need to write an if-else condition where it checks if the clone command gives a fatal error don't proceed or else do git add remote origin What regex should I use to do that validation?

Can someone please help?

Appreciate all your help! Thanks!

Sweta Sharma
  • 2,404
  • 4
  • 21
  • 36
  • 2
    It will be easier to exit the script if `git clone` fails than to validate the string: https://stackoverflow.com/questions/821396/aborting-a-shell-script-if-any-command-returns-a-non-zero-value – Lars Christian Jensen May 06 '21 at 09:07
  • Thanks Lars for your reply and for sharing the information. How can I inform the user that the project already exists rather than closing the command prompt as soon as the error occurs. I mean the user should why the ` git clone could not happen ` – Sweta Sharma May 06 '21 at 09:13
  • 1
    Print the output from the failing command, and maybe also print the commandline itself. You will most likely not be able to interpret every possible error message from `git clone` in your script, so I wouldn't even try. You can also print something like "This could fail because the project already exists", if that is a common mistake for the user in this case. – Lars Christian Jensen May 06 '21 at 11:51
  • Thanks so much Lars for your reply and for sharing the information. – Sweta Sharma May 07 '21 at 06:20

1 Answers1

0

A quick hack would be to redirect the STDERR generated from the command :git clone [bitbucket Git URL] to a temp file and then print the error if the return code of the command is not equal to 0... :

git clone [bitbucket Git URL] 2>/some_path/error.txt
if [[ "$?" -ne '0' ]]
  then
    printf "The error encountered on git clone is : $(cat /some_path/error.txt)\n"
else
  git add remote origin
fi
User123
  • 1,498
  • 2
  • 12
  • 26