我修改了我的帖子,並設法在此處複製/粘貼代碼。我知道這很醜,但真的希望它能幫助找到錯誤。 錯誤消息是:「./v_c:2:in‘:未定義的局部變量或方法’主:對象(NameError)」./count:78:語法錯誤,意外的關鍵字結束,期待輸入結束
#!/usr/bin/env ruby
#
# This program is used for collecting web server visit information.
#
# Author: A. Genius
#
require 'optparse'
def print_usage
puts "USAGE: v_c -d DNS_NAME"
exit
end
# add option switch and handler
options = {}
option_parser = OptionParser.new do |opts|
# DNS_NAME argument
options[:dns_name] = nil
opts.on('-d', '--dns-name DNS_NAME', 'Specify a DNS NAME') { |dns_name| options[:dns_name] = dns_name }
# HELP argument
options[:help] = nil
opts.on('-h', '--help', 'Display usage') { |help| options[:help] = help }
end
option_parser.parse!
# verify arguments
if options[:dns_name] then
dns_name = options[:dns_name]
else
puts "Please set a balancer's DNS."
print_usage
exit
end
if options[:help] then
print_usage
exit
end
# Keep STDOUT
# orig_stdout = $stdout
# redirect stdout to /dev/null
# $stdout = File.new('/dev/null', 'w')
server1_visit_count = 0
server2_visit_count = 0
server3_visit_count = 0
server4_visit_count = 0
# starting to visit load balancing server
puts "Starting to visit load balancing server"
2000.times do
# visit load balancer
# o = "curl #{dns_name}"
o = "curl -s #{dns_name}"
if o =~ /server\s*1/i
server1_visit_count += 1
elsif o =~ /server\s*2/i
server2_visit_count += 1
elsif o =~ /server\s*3/i
server3_visit_count += 1
elsif o =~ /server\s*4/i
server4_visit_count += 1
end
print "."
end
puts
puts '-------------------------'
puts ' Summary'
puts '-------------------------'
puts "Server1 visit counts : " + server1_visit_count.to_s
puts "Server2 visit counts : " + server2_visit_count.to_s
puts "Server3 visit counts : " + server3_visit_count.to_s
puts "Server4 visit counts : " + server4_visit_count.to_s
puts "total is: " + (server1_visit_count + server2_visit_count +
server3_visit_count + server4_visit_count).to_s
'elsif'是單個關鍵字,裏面沒有空格。另外,請發佈代碼,而不是截圖。 – mudasobwa
你在這行'puts'得到的錯誤總訪問次數:「+(server1_visit_count + server2_visit_count + server3_visit_count + server4_visit_count).to_s'? –
您可以將代碼複製並粘貼到問題中嗎?如果我能夠將代碼粘貼到我最喜歡的編輯器中並運行它,我會更容易幫助您。 – spickermann