Scripts – print_range.rb

Print Friendly, PDF & Email

DISCLAIMER

This script is as is. I give no warranty it will work as you expected on your system. You are running it at your own risk. If something goes wrong because of this script, I bear no responsibility.

Language

server# ruby -v
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]

Purpose

You know the initial hexadecimal dev id. You want to assign a fixed number of devices to each node in a say 8 node cluster. print_range.rb will do it for you. It will display those many number of devices per line, one line per node.

Usage


# print_range.rb 1234 4 3
1234,1235,1236
1237,1238,1239
123A,123B,123C
123D,123E,123F


# cat print_range.rb
#!/usr/local/bin/ruby

# A script to display next NN hex numbers given at command line
# print_range.rb 21AA 4 8 (display 4 lines each line containing 8 comma separated devices)
# By Ketan Patel
# v01 - 26/05/2015

class String
  def numeric?
    return true if self =~ /\A\d+\Z/
  end
end

class ParseInput < String

  @firsthex = String.new
  @lines    = String.new
  @numdevs  = String.new

  def usage
    puts "Usage: print_range.rb 21AA 4 8 (display 4 lines each line containing 8 comma separated devices with first device id starting at 21AA)"
    exit
  end

  def checkInput (input)
    input.length != 3 ? ( usage ) : ( @firsthex = input[0])
    input[1].numeric? ? ( @lines = input[1].to_i ) : ( puts "ERROR - Number of lines \"#{input[1]}\" is not numeric")
    input[2].numeric? ? ( @numdevs = input[2].to_i ) : ( puts "ERROR - Number of devices per line \"#{input[2]}\" is not numeric")
  end

  def displayOutput
    firstdec    = @firsthex.hex
    lines_temp  = 1
    numdev_temp = 1
    @lines.times do
      devstr = ""
      isfirstid = "yes"
      @numdevs.times do
        dev = sprintf("%04X",firstdec)
        isfirstid == "yes" ? ( devstr = dev; isfirstid = "no" ) : ( devstr = devstr + "," + dev )
        firstdec+=1
      end
      puts devstr
    end
  end

end

ip = ParseInput.new
ip.checkInput(ARGV)
ip.displayOutput

,

sanaswati
No comments yet.

Leave a Reply

*