我正在尝试编写一个脚本来定制我的点文件设置到每台机器使用,git update-index -skip-worktree
但它不断切断路径。我传递以命令开头的数组,其中数组中的每个其他值都是 git 跟踪的文件或目录路径。这是代码片段
fish=("fish" "$XDG_CONFIG_HOME/fish")
gtk=("gtk-launch" "$XDG_CONFIG_HOME/gtk-3.0")
i3=("i3-config-wizard" "$XDG_CONFIG_HOME/i3")
check_install() {
for var in "$@"
do
echo ${var[0]}
if ! [ -x "$(command -v ${var[0]})" ]; then
for path in "${var[@]:1}"
do
echo locally untracking $path
git update-index --skip-worktree "$path"
rm -r "$path"
done
fi
done
}
check_install $fish
check_install $gtk
check_install ${i3[@]}
这些条目的输出
locally untracking ish
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
rm: cannot remove 'ish': No such file or directory
gtk-launch
i3-config-wizard
locally untracking 3-config-wizard
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
rm: cannot remove '3-config-wizard': No such file or directory
/i3
locally untracking i3
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
rm: cannot remove 'i3': No such file or directory
urxvt
我对 bash 中的函数比较陌生,所以我想该函数并没有完全按照我的预期运行。另外,如果你们对如何解决这个问题有更好的想法,请随时提及。
答案1
问题的一部分是外部 for 循环将每个项目存储为单独的变量,然后内部 for 循环删除第一个字符。修正后的函数是这样的:
check_install() {
echo "${@: -1}"
if ! [ -x "$(command -v $1)" ]; then
for path in "${@: -1}"
do
echo locally untracking $path
git update-index --skip-worktree "$path"
rm -r "$path"
done
fi
}
数组变量也被错误地引用,因为它只传递了第一个元素。 @steeldriver提到的正确方法是"${kak[@]}"