无法识别永久环境变量



我在/etc/profile.d/script.sh 中放置了一个脚本,在 SSH 连接上检查用户是否在其 ~/.profile 文件中设置了环境变量"GIT_AUTHOR_NAME"和"GIT_AUTHOR_EMAIL"。

如果没有,它会要求输入这些详细信息。我遇到的问题是,即使存在 Env 变量,脚本也会不断要求输入详细信息。这是/etc/profile.d/script.sh 的样子:

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

这与某种子壳有什么关系吗?我已经尝试了各种不同的方法来检查是否设置了 env 变量。

你正在将-x与 grep 一起使用;除非我弄错了,否则

"仅选择与整行完全匹配的匹配项">

(来自man grep(。

我怀疑这是行的开头,也许是

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

或者,如果有任何export正在进行,则使用更广泛的正则表达式:

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

最新更新