0

In a bash script, I need to prompt the user for an email address, but only if git user.email is not set or there is no ssh key. I have very little experience with bash, so I've been coming to stackoverflow again and again for this project.

Based on this stackoverflow answer, I came up with this:

if [[ ! -f "$HOME/.ssh/id_rsa" ]]; then
    echo "No File Found"
else
    echo "File Found"
fi

This has the expected behavior: If id_rsa exists, it says File Found; If I remove id_rsa, it says No File Found.

However, I cannot get this to work as part of a longer expression. Here's what I have so far:

md_email=$4

... more code ...

if [[ "$md_email" = "" && "`git config --get user.email`" = "" || ! -f "$HOME/.ssh/id_rsa" ]]; then
    echo "No File Found"
else
    echo "File Found"
fi

This will always print No File Found. I've tried various combinations of brackets, double brackets, and parentheses, in the hope of isolating the second and third expressions from the first as well as isolating the third from the rest in a way that might make it work. All to no avail. What am I missing?


At request of @codeforester:

ls -l $HOME/.ssh/id_rsa
-rw------- 1 menasheh menasheh 3243 Feb 13 22:52 /home/menasheh/.ssh/id_rsa`
codeforester
  • 39,467
  • 16
  • 112
  • 140
Menasheh
  • 3,560
  • 3
  • 33
  • 48

1 Answers1

0

You could do this:

md_email=$4
git_email=$(git config --get user.email)
if [[ ! $md_email ]] && [[ ! $git_email || ! -f "$HOME/.ssh/id_rsa" ]]; then
  echo "user.email not set or id_rsa is not present"
fi
  • ! $md_email will be true when $md_email is empty; the same thing applies to ! $git_email as well

Your if condition to check the presence of id_rsa looks good to me. Please include the output of ls -l $HOME/.ssh/id_rsa in your question.

You may want to see this post: How to use double or single brackets, parentheses, curly braces

Community
  • 1
  • 1
codeforester
  • 39,467
  • 16
  • 112
  • 140