아직 존재하지 않는 파일에 대한 심볼릭 링크

아직 존재하지 않는 파일에 대한 심볼릭 링크

현재 , 등의 파일을 내 홈 디렉토리 내의 디렉토리로 이동하여 .vimrc홈 디렉토리를 정리하려고 합니다 .bash_profile..dotfiles

아이디어는 이후에 이러한 파일에 대한 심볼릭 링크를 사용하는 것입니다. ln -s ~/.dotfiles/.vimrc ~/.
이것은 잘 작동하지만 다음을 작성하여 프로세스를 자동화하고 싶습니다.나의 처음Bash 스크립트에 문제가 있습니다.

현재 스크립트는 다음과 같습니다.

#!//bin/bash
# Make script executable with: chmod u+x brew.sh

# Ask for the administrator password upfront.
sudo -v

# Keep-alive: update existing `sudo` time stamp until the script has finished.
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &

# Create '.other'-folder
echo "--> ~/.other"
if [ -d ~/.other ];
then
  echo "Directory ~/.other exists..."
else
  echo "Creating directory ~/.other..."
  mkdir ~/.other
fi
echo ""

# TRASH
echo "--> ~/.Trash"
if [ -d ~/.Trash ];
then
  echo "Directory ~/.Trash does exists. Moving it to ~/.other..."
  mv ~/.Trash ~/.other/
else
  echo "Directory ~/.Trash doesn't exists. Creating it in ~/.other..."
  mkdir ~/.other/.Trash
fi

echo "Linking ~/.other/.Trash to ~/.Trash..."
ln -s ~/.other/.Trash ~/.
echo ""

# BASH_HISTORY
echo "--> ~/.bash_history"
if [ -a ~/.bash_history ];
then
  echo "File ~/.bash_history does exists. Moving it to ~/.other..."
  mv ~/.bash_history ~/.other/
else
  echo "File ~/.bash_history doesn't exists. Creating it in ~/.other..."
  touch ~/.other/.bash_history
fi

echo "Linking ~/.other/.bash_history to ~/.bash_history..."
ln -s ~/.other/.bash_history ~/.
echo ""

# BASH_SESSIONS
echo "--> ~/.bash_sessions"
if [ -d ~/.bash_sessions ];
then
  echo "Directory ~/.bash_history does exists. Moving it to ~/.other..."
  mv ~/.bash_sessions ~/.other/
else
  echo "Directory ~/.bash_history doesn't exists. Creating it in ~/.other..."
  mkdir ~/.other/.bash_sessions
fi

echo "Linking ~/.other/.bash_sessions/ to ~/.bash_sessions/..."
ln -s ~/.other/.bash_sessions ~/.
echo ""

# .LOCAL
echo "--> ~/.local"
if [ -d ~/.local ];
then
  echo "Directory ~/.local does exists. Moving it to ~/.other..."
  mv ~/.local ~/.other/
else
  echo "Directory ~/.local doesn't exists. Creating it in ~/.other..."
  mkdir ~/.other/.local
fi

echo "Linking ~/.other/.local/ to ~/.local/..."
ln -s ~/.other/.local ~/.
echo ""

# .CONFIG
echo "--> ~/.config"
if [ -d ~/.config ];
then
  echo "Directory ~/.config does exists. Moving it to ~/.other..."
  mv ~/.config ~/.other/
else
  echo "Directory ~/.config doesn't exists. Creating it in ~/.other..."
  mkdir ~/.other/.config
fi

echo "Linking ~/.other/.config/ to ~/.config/..."
ln -s ~/.other/.config ~/.
echo ""

보시다시피 코드는 매우 반복적이지만 먼저 해야 할 일이 있습니다. 코드는 대략 다음과 같이 작동해야 합니다. 내 홈 디렉터리에 파일이 있는지 확인하세요 init.vim(예:). 존재하는 경우 다음 위치로 이동합니다 ~/.other(덜 중요한 파일) 또는 ~/.dotfiles(중요한 문서). 존재하지 않는 경우 ~/.dotfiles파일(또는 디렉터리)이 또는 에 생성됩니다 ~/.other. 이후 심볼릭 링크.

지금까지의 이론. 문제는 파일이 내 홈 디렉토리에 아직 존재하지 않는 경우 스크립트가 ~/.dotfiles/에 파일을 생성 ~/.other하고 내 홈 디렉토리의 이름을 해당 파일에 연결한다는 것입니다. 일부 파일에는 특정 권한이 필요하기 때문에 실제로는 작동하지 않습니다. 예를 들어, neovim이 스크립트를 사용하여 일부 파일을 생성한 경우 인식되지 않으며, 사용하기 전에 파일을 생성하는 것은 그리 효율적이지 않습니다.

이 문제를 해결할 수 있는 방법이 있습니까(예: 대상 파일을 생성하지 않고 링크를 생성하여 - 한 번 링크를 시도했는데 .bash_history링크 .other/.bash_history가 제대로 작동했지만 bash존재하지 않는 파일에 쓸 수 없었습니다)? 새 파일이 가장 좋은 경우만들어진내부에옳은위치만 지정하면 됩니다옳은이전에 배치되었나요?

추신: 파일이 이미 존재하는 경우 스크립트는 제대로 작동합니다(파일을 새 위치로 이동하고 링크하기만 하면 됩니다).

답변1

중복을 줄이기 위해 다시 작성됨

#!/bin/bash
# Make script executable with: chmod u+x brew.sh

# Ask for the administrator password upfront.
sudo -v

# Keep-alive: update existing `sudo` time stamp until the script has finished.
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &

# Create '.other'-folder
echo "--> ~/.other"
mkdir -p ~/.other 2>/dev/null
echo ""

create_dir() {
    local pathname=$1 destination=$2 permissions=$3
    local dirname=$(basename "$pathname")

    echo "--> $pathname"
    if [ -L "$pathname" ] && [ "$(dirname "$(readlink "$pathname")")" = "$destination" ]; then
        echo "$pathname is already a symbolic link to $destination/$filename"
        return
    elif [ -d "$pathname" ]; then
        echo "Directory $pathname exists. Moving it to $destination..."
        mv "$pathname" $destination/
    else
        echo "Directory $pathname doesn't exist Creating it in $destination..."
        mkdir -p "$destination/$dirname"
    fi
    chmod "$permissions" "$destination/$direname"

    echo "Linking $destination/$dirname to $pathname ..."
    (
        cd "$(dirname "$pathname")"
        ln -s "$destination/$dirname" 
    )
    echo
}

create_file() {
    local pathname=$1 destination=$2 permissions=$3
    local filename=$(basename "$pathname")
    echo "--> $pathname"
    if [ -L "$pathname" ] && [ "$(dirname "$(readlink "$pathname")")" = "$destination" ]; then
        echo "$pathname is already a symbolic link to $destination/$filename"
        return
    elif [ -a "$pathname" ]; then
        echo "File $pathname exists. Moving it to $destination..."
        mv "$pathname" $destination/
    else
        echo "File $pathname doesn't exists. Creating it in $destination..."
        touch "$destination/$filename"
    fi
    chmod "$permissions" "$destination/$filename"

    echo "Linking $destination/$filename to ~/.bash_history..."
    (
        cd "$(dirname "$pathname")"
        ln -s "$destination/$filename"
    )
    echo ""
}

create_dir   ~/.Trash          ~/.other  755   # TRASH
create_file  ~/.bash_history   ~/.other  600   # BASH_HISTORY
create_file  ~/.bash_sessions  ~/.other  644   # BASH_SESSIONS
create_dir   ~/.local          ~/.other  755   # .LOCAL
create_dir   ~/.config         ~/.other  755   # .CONFIG

create_file  ~/.bashrc         ~/.dotfiles 644 # etc ...

노트:

  • mkdir -p디렉토리가 존재하지 않으면 생성됩니다.
  • 먼저 디렉터리/파일이 이미 심볼릭 링크가 아닌지 확인하세요.
  • 특정 파일에 특정 권한이 필요한 경우 이를 지정할 수밖에 없습니다.

관련 정보