bashgitlab.profile

Permanent Env variable not recognised


I have a script placed in /etc/profile.d/script.sh that on SSH connect checks if the user has the environment variables 'GIT_AUTHOR_NAME' and 'GIT_AUTHOR_EMAIL' set in their ~/.profile file.

If not, it asks to enter these details. Problem I am having is that even though the Env variables exist, the script keeps asking to enter details. This is what /etc/profile.d/script.sh looks like:

if grep -Fxq "GIT_AUTHOR_NAME" /$PWD/.profile
then
    echo Git details already known > /dev/null
else
    echo "To make sure your commits are shown under your name, you have to enter your Git details once."
    read -p "Enter your Git author name: " gituser
    echo export GIT_AUTHOR_NAME="$gituser" >> /$PWD/.profile
    read -p "Enter your Git e-mail address: " gitemail
    echo export GIT_AUTHOR_EMAIL="$gitemail" >> /$PWD/.profile
    echo "Your git name "$gituser" and e-mail "$gitemail" are now saved as environment variables in your .profile file."
fi

Does this have anything to do with subshelling of some kind? I have tried various differations of checking whether the env variables are set.


Solution

  • You're using -x with grep; unless I'm mistaken, that

    "Select only those matches that exactly match the whole line"

    (from man grep).

    I suspect that it is instead the beginning of the line, perhaps with

    if grep -q "^GIT_AUTHOR_NAME" /$PWD/.profile
    

    or, if there is any exporting going on, a more expansive regex:

    if grep -Eq "^[[:space:]]*(export)[[:space:]]*GIT_AUTHOR_NAME[[:space:]]*=" /$PWD/.profile