1

I'm trying to write a script to tailor my dotfile setup to each machine using git update-index -skip-worktree but it keeps chopping up the paths. I'm passing arrays starting with commands where every other value in the array is a file or directory path being tracked by git. here is a snippet of the code

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[@]}

the output for these entries

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

I'm relatively new to functions in bash so I imagine the function isn't doing exactly what I expect. also if you guys have a better idea about how to go about this, feel free to mention it.

2
  • In bash, $array is equivalent to ${array[0]} - to pass all the elements of array fish as positional parameters to your function, you probably want check_install "${fish[@]}" and so on Commented Feb 11, 2020 at 17:08
  • That was one of the issues... that did get me started on a solution. I had to change a multiple things in order for the code to work, both in the arrays containing the configs and the function parsing the config arrays. I might just remove the arrays entirely as the idea was orignially to have a multidimensional array or to pass multiple arrays to the function, but unfortunately this wasn't possible due to the lack of support for multidimensional arrays and the arrays being pass being read as a single array. Commented Feb 11, 2020 at 19:36

1 Answer 1

0

part of the issue was the outer for loop was storing each item as a separate variable, which the inner for loop then was removing the first character. the corrected function is this:

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

}

the array variables were also being referenced incorrectly, as it was only passing the first element. the correct way as mentioned by @steeldriver is "${kak[@]}"

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.