うまくいかない。

要望

こんな感じのスクリプトが作りたい。

#作ったスクリプトの名前が「rpipe.rb」
$ which refe
#=>/var/lib/gems/1.8/bin/refe
$ which refe | rpipe.rb cat
#cat /var/lib/gems/1.8/bin/refe   に置換されて実行される。

問題点

  • ターゲットを親ディレクトリにしたいときがある。
    • lsとか
  • cdを使いたいのだけれど動かない。

暫定的な解決策

  • 先に親ディレクトリを試す。(その後、対象のファイル)
  • bashの履歴に置換されたコマンドを格納する。

コード

#!/usr/bin/ruby

def get_element
  return  $_ while $stdin.gets
end

process=ARGV.join(" ")
target=get_element.chomp

write_history = proc do
  File.open(ENV["HOME"]+"/.bash_history","a") do |wf|
    wf.puts "#{process} #{target}"
  end
end.call

exit if system "#{process} #{File.dirname target}"
exit if system "#{process} #{target}"

p "error"; exit -1