Help us understand the problem. What is going on with this article?

bashでスペース区切りの文字列をまとめて扱う

自分用のメモっていいタイトルつけられないですね…。なんかこう、vimのパッケージ管理用にgit submoduleをたくさん実行したかったのだけど、

Vim 8 時代のがんばらないプラグイン管理のすすめ - Humanity

git submodule add https://github.com/cespare/vim-toml pack/cespare/start/vim-toml
git submodule add https://github.com/hashivim/vim-terraform pack/hashivim/start/vim-terraform
git submodule add https://github.com/itchyny/landscape.vim pack/itchyny/start/landscape.vim
git submodule add https://github.com/itchyny/lightline.vim pack/itchyny/start/lightline.vim
#...

数行書いて飽きたので、きれいに書く方法を考えていた。

https://github.com/ の後ろは A/B なのに、clone先のパスを pack/A/start/B にしないといけない。
色々やり方はあるんだろうけど、軽くこの辺を読んで考えた。

readとヒアドキュメント案

ヒアドキュメントで複数行書いておいて、組み込みコマンドのreadを使ってパースしていくやり方でどうだろうか。(試行中なので、とりあえずコマンドは実行せずにechoで出力するだけ)

while read author name
do
  echo "git submodule add https://github.com/${author}/${name} pack/${author}/start/${name}"
done << EOS
cespare vim-toml
hashivim vim-terraform
itchyny landscape.vim
itchyny lightline.vim
EOS

とりあえずこれで実行できそう。

IFS変数で微調整

好みの問題だけど、vimのプラグインは author/name 的に、スラッシュ区切りで書きたい。
readの区切りを変えたければ、ローカル変数でIFSをいじればいい。

while IFS=/ read author name
do
  echo "git submodule add https://github.com/${author}/${name} pack/${author}/start/${name}"
done << EOS
cespare/vim-toml
hashivim/vim-terraform
itchyny/landscape.vim
itchyny/lightline.vim
EOS

良さそうな感じになった。

最終形

あとは、echoを外して実際に実行するようにすれば完成ですね。めでたしめでたし

while IFS=/ read author name
do
  git submodule add https://github.com/${author}/${name} pack/${author}/start/${name}
done << EOS
cespare/vim-toml
hashivim/vim-terraform
itchyny/landscape.vim
itchyny/lightline.vim
EOS
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away