Bash: 如何在 for 循环中从多个文件的数组中删除目录的第一部分?
Bash: How would I remove the first part of a directory from a array for multiple files in a for loop?
我正在尝试编写一个脚本来为我管理点文件,这就是我所拥有的:
dots=(
~/.config/alacritty
~/.config/dunst
~/.config/flameshot
~/.config/i3
~/.config/i3status
~/.config/kitty
~/.config/nvim
~/.config/picom
~/.config/rofi
~/.config/gtk-3.0
~/.config/obs-studio
~/.config/sway
~/.config/waybar
~/.config/swappy
~/.config/mako
~/.config/swaylock
~/.config/Code/User/settings.json
~/.config/Code/User/keybindings.json
~/.config/fontconfig
~/.bash_logout
~/.bash_profile
~/.bashrc
~/.profile
~/.stignore
~/.gtkrc-2.0
)
for name in "${dots[@]}"; do
if [ ! -e "$name" ]; then
echo "$name does not exist."
ln -sv "$HOME/Dotfiles/dots/${name#~/.config/}" "$name"
else
echo "$name exists."
fi
done
这里的问题是它从 ~/.config/sway
等目录中删除了 ~/.config/
部分,但没有删除 ~/.profile
等目录中的 ~/
部分。
我的问题是,我如何在删除 ~/.config/
部分和 ~/
部分的文件中删除它们?
编辑:将符号链接行中的 echo
替换为 ln -sv
。我用 echo
替换了它,这样我就可以测试它是否有效。
My question is, how would I make it where it removed the ~/.config/ part and the ~/ part for files that have them?
就个人而言,我会分两步完成,将中间结果存储在一个单独的变量中:
local_part=${name#~/}
ln -sv "$HOME/Dotfiles/dots/${local_part#.config/}" "$name"
我正在尝试编写一个脚本来为我管理点文件,这就是我所拥有的:
dots=(
~/.config/alacritty
~/.config/dunst
~/.config/flameshot
~/.config/i3
~/.config/i3status
~/.config/kitty
~/.config/nvim
~/.config/picom
~/.config/rofi
~/.config/gtk-3.0
~/.config/obs-studio
~/.config/sway
~/.config/waybar
~/.config/swappy
~/.config/mako
~/.config/swaylock
~/.config/Code/User/settings.json
~/.config/Code/User/keybindings.json
~/.config/fontconfig
~/.bash_logout
~/.bash_profile
~/.bashrc
~/.profile
~/.stignore
~/.gtkrc-2.0
)
for name in "${dots[@]}"; do
if [ ! -e "$name" ]; then
echo "$name does not exist."
ln -sv "$HOME/Dotfiles/dots/${name#~/.config/}" "$name"
else
echo "$name exists."
fi
done
这里的问题是它从 ~/.config/sway
等目录中删除了 ~/.config/
部分,但没有删除 ~/.profile
等目录中的 ~/
部分。
我的问题是,我如何在删除 ~/.config/
部分和 ~/
部分的文件中删除它们?
编辑:将符号链接行中的 echo
替换为 ln -sv
。我用 echo
替换了它,这样我就可以测试它是否有效。
My question is, how would I make it where it removed the ~/.config/ part and the ~/ part for files that have them?
就个人而言,我会分两步完成,将中间结果存储在一个单独的变量中:
local_part=${name#~/}
ln -sv "$HOME/Dotfiles/dots/${local_part#.config/}" "$name"