我写了bash git-install script。最后,我做了:
echo "Edit ~/.bash_profile to load ~/.git-completioin.bash on Terminal launch"
echo "source ~/.git-completion.bash" >> ~/.bash_profile
问题是,如果多次运行脚本,最后会多次将此行附加到〜/ .bash_profile。如何使用grep
或sed
(或您可能推荐的其他选项)使用bash脚本来仅添加文件中尚不存在的行。另外,如果该文件存在且~/.profile
不存在,我想将该行添加到~/.bash_profile
,否则只需将其添加到~/.bash_profile
。
答案 0 :(得分:11)
这样的事情应该这样做:
LINE_TO_ADD=". ~/.git-completion.bash"
check_if_line_exists()
{
# grep wont care if one or both files dont exist.
grep -qsFx "$LINE_TO_ADD" ~/.profile ~/.bash_profile
}
add_line_to_profile()
{
profile=~/.profile
[ -w "$profile" ] || profile=~/.bash_profile
printf "%s\n" "$LINE_TO_ADD" >> "$profile"
}
check_if_line_exists || add_line_to_profile
几点说明:
.
命令代替source
,因为source
是一种基础,但非bash shell可能会使用.profile
。命令source ...
在.profile
printf
而不是echo
,因为它更具可移植性,并且不会像bash echo
那样搞乱反斜杠转义的字符。grep -Fx
来搜索字符串。 -F
表示固定字符串,因此搜索字符串中的特殊字符不需要转义,-x
表示仅匹配整行。 -qs
是常见的grep语法,用于检查字符串的存在而不显示它。答案 1 :(得分:6)
if [[ ! -s "$HOME/.bash_profile" && -s "$HOME/.profile" ]] ; then
profile_file="$HOME/.profile"
else
profile_file="$HOME/.bash_profile"
fi
if ! grep -q 'git-completion.bash' "${profile_file}" ; then
echo "Editing ${profile_file} to load ~/.git-completioin.bash on Terminal launch"
echo "source \"$HOME/.git-completion.bash\"" >> "${profile_file}"
fi
答案 2 :(得分:2)
怎么样:
grep -q '^source ~/\.git-completion\.bash$' ~/.bash_profile || echo "source ~/.git-completion.bash" >> ~/.bash_profile
或以更明确(和可读)的形式:
if ! grep -q '^source ~/\.git-completion\.bash$' ~/.bash_profile; then
echo "Updating" ~/.bash_profile
echo "source ~/.git-completion.bash" >> ~/.bash_profile
fi
编辑:
你应该在你的单行之前添加一个额外的换行符,以防万一~/.bash_profile
没有结束:
if ! grep -q '^source ~/\.git-completion\.bash$' ~/.bash_profile; then
echo "Updating" ~/.bash_profile
echo >> ~/.bash_profile
echo "source ~/.git-completion.bash" >> ~/.bash_profile
fi
编辑2:
这样修改起来更容易一些,也更容易移植:
LINE='source ~/.git-completion.bash'
if ! grep -Fx "$LINE" ~/.bash_profile >/dev/null 2>/dev/null; then
echo "Updating" ~/.bash_profile
echo >> ~/.bash_profile
echo "$LINE" >> ~/.bash_profile
fi
-F
和-x
选项由POSIX指定,并在其他几个答案和评论中提出。
答案 3 :(得分:1)
# Decide which profile to add to
PROFILE=~/.bash_profile
if ! [ -e "$PROFILE" ] && [ -e ~/.profile ]; then
PROFILE=~/.profile
fi
# Add to profile if it doesn't appear to be there already. Err on the side of
# not adding it, in case user has made edits to their profile.
if ! grep -s 'git-completion\.bash' "$PROFILE"; then
echo "Editing $PROFILE to load ~/.git-completion.bash on Terminal launch"
echo "source ~/.git-completion.bash" >> "$PROFILE"
fi