简体   繁体   中英

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

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 :

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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