我正在尝试学习 ruby 脚本。目前我正在创建一个脚本,它应该接受用户输入并执行如下操作:
Go to server1 and restart mysqld
下面是我的脚本:
#!/usr/bin/ruby
puts "on which environment you want to run the script?"
srenv = STDIN.gets.chomp()
puts "#{srenv}"
puts "Hi you have chosen to run the script on #{srenv}, now is it Cluster or not?"
srenvdl = STDIN.gets.chomp()
puts "#{srenvdl}"
if srenvdl == "y"
text = String.new
File.open("cluster.txt") { |f| text = f.read }
words = text.split(/[^a-zA-Z1-9]/)
puts "Now enter your services names:"
sernm = STDIN.gets.chomp()
sernmw = sernm.split(/[^a-zA-Z1-9]/)
for x in words
for each in sernmw
puts "Go to #{x} and restart #{each}!!"
end
end
else
puts "Sorry now we are only supporting Clusters."
exit
end
这是一个交互式脚本,它要求用户输入服务名称并从我想跳过的文件中读取集群服务器名称。此外,我在读取文件时无法跳过集群名称。
ruby script.rb cluster1:mysqld,nginx cluster2:memcache,sendmail
我有另一个文件 cluster.txt,其中存储了集群服务器名称,如下所示:
cluster1:server1,server11,server111
cluster2:server2,server22,server222
现在我希望脚本应该返回结果如下:
Go to server1 and restart mysqld
Go to server1 and restart nginx
Go to server11 and restart mysqld
Go to server11 and restart nginx
Go to server111 and restart mysqld
Go to server111 and restart nginx
Go to server2 and restart memcache
Go to server2 and restart sendmail
Go to server22 and restart memcache
Go to server22 and restart sendmail
Go to server222 and restart memcache
Go to server222 and restart sendmail