9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bashing: サブコマンドを持つ bash スクリプトをビルドするツール

Last updated at Posted at 2014-02-17

tumblr-gif を作るのに使ってみたビルドツール bashing の紹介。

$ foo bar, $ foo baz のようなサブコマンドを持つ bash スクリプトをビルドしたり、複数ファイルに分けて管理を簡潔にするツール。

こんな感じで、サブコマンドをもつシェルスクリプトを作れる。

$ bashing new foo
# foo プロジェクト作成

$ cd foo

$ cat > src/tasks/echo_args.sh
# <help>与えられた引数を出力するだけ</help>
echo "$@"
# Ctrl+D 押す

# ビルド
$ bashing deploy

# 実行
$ bin/foo
Usage: foo <task> [...]

    echo_args  :  与えられた引数を出力するだけ
    hello      :  (no help available)
    help       :  display this help message
    version    :  display version

# サブコマンドとその引数を与えて実行
$ bin/foo echo_args nyan nyan wan
nyan nyan wan

と割とお手軽にできる。

他にも、タスク間で共用する関数を置く場所や、毎回実行される処理もかける場所がある。

# lib には共通モジュール置く
$ cat > src/lib/greet.sh
function greet_ja() {
    echo 'こんにちは bashing'
}
# Ctrl + D
$ echo 'greet_ja' > src/tasks/helloja.sh

# 毎回実行される処理を書く
$ echo 'echo init' > src/init.sh
$ echo 'echo before-task' > src/before-task.sh
$ echo 'echo after-task' > src/after-task.sh
$ echo 'echo cleanup' > src/cleanup.sh

# ビルド
$ bashing deploy

# 実行
$ bin/foo
init
before-task
before-task
Usage: foo <task> [...]

    echo_args  :  与えられた引数を出力するだけ
    hello      :  (no help available)
    helloja    :  (no help available)
    help       :  display this help message
    version    :  display version

after-task
cleanup

# サブコマンド与えて実行
$ bin/foo helloja
init
before-task
こんにちは bashing
after-task
cleanup

before-task が二回実行されてるっぽいのはバグかな?

9
10
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
9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?