LoginSignup
1
1

More than 3 years have passed since last update.

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

Posted at

自分用のメモっていいタイトルつけられないですね…。なんかこう、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
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1