简体   繁体   English

Bash循环遍历元素列表

[英]Bash loop over a list of elements

I've got a script I use that looks like this 我有一个使用的脚本,看起来像这样

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

I'd like to update it so it loops through a list so I could have some improved output without having repeated explicit code. 我想对其进行更新,以便它遍历列表,这样我可以得到一些改进的输出,而无需重复重复的显式代码。 I'm very bad at shell scripting and wondering if it's possible to have a bash script that would run something like this if it was done in C 我对Shell脚本非常不好,想知道是否有可能使用C语言运行bash脚本来运行类似的东西

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");
}

Where you are: 您的位置:

 cd ~/.vim/bundle
 for f in supertab syntastic vim-alternate vim-easymotion vim-matchit vim-togglemouse
 do
    cd $f
    git pull
    cd ..
 done

You could use an array in Bash, like this : 您可以在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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM