5
0

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 3 years have passed since last update.

ラップするコマンドの作り方

Posted at

誰向けかわからないけど,ラップするコマンドを作る方法

目標とする状態

  • 複数のコマンドが存在する.それら全てのラッパーコマンドを作りたい
    • 例えば,/usr/bin/* の全てに対して ~/local/bin 配下に似たような名前のコマンドを作りたい
      • コマンド名は別名として,例えば ls に対しては wrap_ls のような名前のコマンドにする
      • 例えは,ln -s /usr/bin/ls ~/local/bin/wrap_ls みたいな状態にする.
      • wrap_xx は,元のコマンドを呼び出す可能性があるので,別名にするようにする.(ls という名前ではなく,wrap_ を付ける)
  • ラップするコマンドは,全て同じコマンドを呼び出して引数に元のコマンドが渡せるようにする.
    • 例えば,wrap_ls の中身は echo ls になっている.
  • 引数を受け付けて上記コマンドに渡すようにする.
  • ログインシェルから起動しない可能性があるので,alias では実装しない.

中身

wrapper.sh
# !/bin/sh

prefix=wrap
command=`basename $0 | sed -e 's/^${prefix}_//'`

echo $command "${@}"
$ for f in /usr/bin/*; do 
  ln -s wrapper.sh ~/local/bin/wrap_`basename $f`
done

実行確認

$ exa -1 wrap*
wrap_ls -> wrapper.sh
wrapper.sh
$ ./wrap_ls aaa
ls aaa
5
0
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
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?