Bash 循环遍历元素列表

Max*_*ahm 2 bash loops list

我有一个我使用的脚本,看起来像这样

cd ~/.vim/bundle/supertab
git pull
cd ~/.vim/bundle/syntastic
git pull
cd ~/.vim/bundle/vim-alternate
git pull
cd ~/.vim/bundle/vim-easymotion
git pull
cd ~/.vim/bundle/vim-matchit
git pull
cd ~/.vim/bundle/vim-togglemouse
git pull
Run Code Online (Sandbox Code Playgroud)

我想更新它,以便它循环遍历列表,这样我就可以得到一些改进的输出,而无需重复的显式代码。我非常不擅长 shell 脚本编写,想知道是否有可能有一个 bash 脚本,如果它是用 C 完成的,那么它可以运行这样的东西

vector<string> v{"supertab" , "syntastic", "vim-alternate", 
                 "vim-easymotion", "vim-matchit", "vim-togglemouse"};
for (string it : v) {
    system("cd ~/.vim/bundle/" + it);
    cout << it << ": ";
    system("git pull");
}
Run Code Online (Sandbox Code Playgroud)

Idr*_*ann 5

您可以在 Bash 中使用数组,如下所示:

rootDir="~/.vim/bundle/"
runDir=$(pwd)
declare -a lstDir
lstDir=("supertab" "syntastic" "vim-alternate" "vim-easymotion" "vim-matchit" "vim-togglemouse")

for file in "${lstDir[@]}"; do
    cd "$rootDir/$file" && git pull
done

cd "$runDir"
Run Code Online (Sandbox Code Playgroud)