アクセスカウンター
#! /usr/local/bin/ruby
# カウントファイル
dat = "./dat/count.dat"
# カウントファイルを開く
fh = open(dat, "r+")
# ファイルロック
fh.flock(File::LOCK_EX)
# カウント読み込み
count = fh.read
# 文字列を数字に変換
count = count.to_i
# カウントに1加える
count += 1
# 数字を文字列に変換
count = count.to_s
# ファイルポインタを先頭に移動
fh.rewind
# カウント書き込み
fh.write count
# ファイルロック解除
fh.flock(File::LOCK_UN)
# カウントファイルを閉じる
fh.close
# HTML出力
print "Content-type: text/html\n\n"
print <<END
<html>
<head>
<title>アクセスカウンター</title>
</head>
<body>
あなたは #{count} 人目の訪問者です<br>
</body>
</html>
END
exit
〔 実行する 〕