~/.profile에 설정된 경로 유사 환경 변수에서 스푸핑을 제거합니다.

~/.profile에 설정된 경로 유사 환경 변수에서 스푸핑을 제거합니다.

[우분투 19.10 대 gdm]

내가 가진 것은 다음과 같습니다 $HOME/.profile.

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

XDG_DATA_DIRS=$HOME/PRATAP/:$XDG_DATA_DIRS

위 파일의 마지막 줄을 추가했습니다.XDG_DATA_DIRS=$HOME/PRATAP/:$XDG_DATA_DIRS

재부팅하면..

$ env | grep XDG_DATA_DIRS
XDG_DATA_DIRS=/home/admin/PRATAP/:/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
$

로그아웃하고 로그인하세요..

$ env | grep XDG_DATA_DIRS
XDG_DATA_DIRS=/home/admin/PRATAP/:/home/admin/PRATAP/:/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop

로그아웃했다가 다시 로그인하세요..

$ env | grep XDG_DATA_DIRS
XDG_DATA_DIRS=/home/admin/PRATAP/:/home/admin/PRATAP/:/home/admin/PRATAP/:/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop

내가 추가한 경로가 중복되었습니다 $HOME/PRATAP/.

이런 일이 다시 발생하지 않도록 하는 방법이 있나요?

답변1

XDG_DATA_DIRS 변수를 정리할 수 있습니다. 로그아웃/로그인할 때만 환경 변수에서 스푸핑이 발생한다는 사실은 귀하가 설명하는 내용, .profile 파일의 내용 및 아래의 일반적인 해결 방법과 일치합니다.

먼저 실행 가능한 스크립트를 정의합니다 pathclean(Stackexchange의 @Gilles의 2012년 6월 17일 게시물 #40749에서 수정되었으며 어떤 이유로든 해당 URL을 찾을 수 없습니다).

$ cat pathclean
#!/usr/bin/bash
# Cleans up PATH variable 
# Script accepts exactly 2 arguments
dedup_path() {
    if [ -n "$1" ]; then # TRUE if length of string is !=0
            OLPATH=${1}:; NUPATH=""
        while [ -n "$OLPATH" ]; do  
            x=${OLPATH%%:*}   # keep 1st entry in string $OLPATH
            case $NUPATH: in
                *:"$x":*) ;;                    # already there
                *) NUPATH=$NUPATH:$x ;; # not there yet
            esac
            OLPATH=${OLPATH#*:} # remove 1st entry in $OLPATH
        done
        NUPATH=${NUPATH#:}
        unset OLPATH x
    fi
    echo "$NUPATH"
}

echo $( sed -e 's/::/:/g' -e 's/:$//' <(dedup_path "${2}") )
exit 0

pathclean일반적으로 스크립트를 저장하는 곳에 두십시오 . 이는 $HOME/.local/share/또는 에 있을 수 있습니다 /opt/bin/. 아마도 귀하의 경우에는 $HOME/.local/share/ 가장 좋은 위치일 것입니다 . 그런 다음 실행 가능하게 만드는 데 사용하십시오 $ chmod +x $HOME/.local/share/pathclean.

$HOME/.profile 마지막으로 다음 줄을 추가하는 것을 잊지 마세요 .

  XDG_DATA_DIRS=$($HOME/.local/share/pathclean XDG_DATA_DIRS "$XDG_DATA_DIRS").

위의 줄을 배치하는 것이 중요합니다마지막에당신의 $HOME/.profile.

이 스크립트는 GUI 로그인 시 로그인 관리자가 한 번 가져오는 $HOME/.profile에서 자동으로 실행됩니다.

이렇게 하면 사기를 당하지 않게 될 것입니다. 문제 없이 PATH env-var에 dupe-cleanup을 적용할 수도 있습니다.

관련 정보