1

I am trying to learn ruby scripting. Currently I am creating a script which should accept user input and do some action like below:

Go to server1 and restart mysqld

Below is my Script:

#!/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

It is an interactive script and it's asking user to enter the service name and reads the cluster server names from the file which I want to skip. Also I am not able to skip the cluster name while reading from the file.

ruby script.rb cluster1:mysqld,nginx cluster2:memcache,sendmail

I have anoter file cluster.txt where I have stored the cluster server names as below:

cluster1:server1,server11,server111
cluster2:server2,server22,server222

Now I want the script should return me the result as like below:

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
2
  • 1
    So, what's the problem with that? Commented Oct 24, 2013 at 9:33
  • Actually I don't want interactive, second thing is it is not able to skip the clustername while reading from the cluster.txt. Commented Oct 24, 2013 at 9:46

1 Answer 1

2

You may encounter a situation in which you need to pass an argument with a space in it to a Ruby program. It seems impossible at first, since the shell separates arguments on spaces. However, there is a provision for this: any arguments in double quotes will not be separated. The double quotes are removed by the shell before passing it to the Ruby program. The following example passes a single argument to the test.rb Ruby script, test1 test2.

$ ./test.rb "test1 test2"

Using Command-line Arguments

In your Ruby programs, you can access any command-line arguments passed by the shell with the ARGV special variable. ARGV is an Array variable which holds, as strings, each argument passed by the shell. The following program iterates over the ARGV array and prints out its contents.

#!/usr/bin/env ruby

ARGV.each do|a|
  puts "Argument: #{a}"
end

The following is an excerpt of a bash session launching this script (saved as the file test.rb) with a variety of arguments.

$ ./test.rb test1 test2 "three four"
Argument: test1
Argument: test2
Argument: three four
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.