2010-12-18 3 views
2

나는 bash git-install script이라고 썼습니다. 끝으로, 내가 할 :Bash 스크립트 : 줄이 아직 없으면 ~/.bash_profile 한 줄만 표시

echo "Edit ~/.bash_profile to load ~/.git-completioin.bash on Terminal launch" 
echo "source ~/.git-completion.bash" >> ~/.bash_profile 

문제는 당신이 한 번 이상 스크립트 이상을 실행하는 경우, 당신은 ~/.bash_profile에이 라인을 여러 번 추가 끝입니다. grep 또는 sed (또는 다른 옵션을 사용하는 것)과 함께 bash 스크립팅을 사용하여 파일에 아직 존재하지 않는 행을 추가하는 방법은 무엇입니까? 또한 해당 파일이 존재하고 ~/.bash_profile이 없으면 ~/.profile에 줄을 추가하고 그렇지 않으면 ~/.bash_profile에 줄을 추가하고 싶습니다.

+0

이와 같은 경쟁 조건에주의하십시오. 한 번에 두 개의 스크립트 인스턴스가 실행되는 경우 파일에 둘 다 추가 될 수 있습니다. 무리를 지어 섹션을 지키고 싶지는 않을 것입니다. 이 특정 유스 케이스에서는별로 중요하지 않지만 다른 목적으로 같은 기술을 적용 할 때는 유의해야 할 중요한 고려 사항이 될 수 있습니다. –

답변

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 
+0

Masterful. 고맙습니다. :) – ma11hew28

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에 의해 지정되는 여러 다른 답변과 의견에 제안했다. 이 같은

+0

나는 이스케이프해야하는 표준 정규 표현식 grep 문을 사용하는 대신'grep -F'를 사용합니다. –

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 
10

뭔가를 수행해야합니다

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 

노트의 몇 :

  • 내가 대신 sourcesource. 명령을 사용한 것은 bashism입니다 , .profile은 비 bash 쉘에서 사용할 수 있습니다. 명령 source ... 내가 더 휴대용이기 때문에 printf 대신 echo 사용 습관 배쉬의 echo처럼 백 슬래시 이스케이프 문자를 망치 한 .profile
  • 에서 오류가 발생합니다.
  • 명백하지 않은 오류에 대해 좀 더 강력 해 지도록하십시오. 이 경우 .profile이 이고 쓰기가 가능하도록 쓰기 전에을 쓸 수 있는지 확인하십시오.
  • 나는 grep -Fx을 사용하여 문자열을 검색합니다. -F은 고정 된 문자열을 의미하므로 검색 문자열의 특수 문자는 이스케이프해야하며 -x은 전체 행과 만 일치한다는 의미입니다. -qs은 문자열의 존재를 확인하고 표시하지 않는 일반적인 grep 구문입니다.
  • 이것은 개념 증명입니다. 나는 실제로 이것을 실행하지 않았다. 나쁘지 만, 일요일 아침에 나가서 놀고 싶다.
관련 문제