さくです。
IRCでもちょっとお尋ねしましたが、open do ... end の中でexecを行ったとき、
openされているファイルはどうなるのでしょうか?
$ cat openit.c
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = open("test", O_CREAT);
printf("%d\n", fd);
close(fd);
unlink("test");
return 0;
}
$ cat execit.rb
#!/usr/bin/ruby
open('/dev/null') do |file|
exec('./openit');
end
$ ruby execit.rb
4
$
となりました。他にファイルを開いていなければ、閉じているなら3になるはずな
ので、閉じないままになると判断したのですが。
実際にやってるのは、自分の ~/.shosts を読んで、条件にマッチするエントリが
あったら、sshをexecする以下のようなスクリプトです。
# sshにホスト名でリンク張るだけだと名前が違うときに困るので。
#!/usr/bin/ruby
SHOSTS = File.join(ENV['HOME'], '.shosts')
$0 = File.basename($0)
open(SHOSTS) do |shosts|
while shosts.gets
h, u = $_.chomp.split(/ /)
exec "ssh -l #{u} #{h} #{ARGV.join(' ')}" if h =~ $0
end
end
# エラー処理省略
--
OZAWA -Crouton- Sakuro
E-Mail: mailto:crouton / weatherlight.org
Web: http://www.weatherlight.org/~crouton/
PGP: C98F 74E0 AEEB 4578 1DFC F650 3BE0 9627 11FC C9F5
VERBA VOLANT, SCRIPTA MANENT