Scripts – listdevs.rb

Print Friendly

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 a list of comma separated hexadecimal device ids (range as well as solo), and want to print them one per line. listdevs.rb will do it for you. I use this frequently while preparing a flat file containing pairs of rdf devices.

Usage


# list_devs.rb 1A4,1234:1236,1ab
01A4
01AB
1234
1235
1236


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

# A script to list the device ids on a screen when the input is range of devices separated by ","
# Author - Ketan Patel
# To run: list_devs.rb 1234:1245,1a0:1b0

class ParseInput
  def initialize
    @hexids             = Array.new
        @alldevs_dec_sorted = Array.new
  end

  def usage
    puts "Usage:   list_devs.rb <hex-devid> <dev_count>"
    puts "Example: list_devs.rb 1A4,1234:1239,1ab"
    exit
  end

  def checkInput (input)
    input.empty? ? ( self.usage ) : ( temphexids = input.first.split(",") )
        @hexids = temphexids.reject{ |e| e.empty? }
  end # checkInput ends

  def goThroughInput
    alldevs_dec = Hash.new
    @hexids.each do |hexid|
      if hexid.include? ':'
        range    = hexid.split(":")
        range[0].empty? ? ( puts "Missing start value in range #{hexid}"; exit ) : ( firstdec = "#{range[0]}".hex)
        range.length == 1 ? ( puts "Missing end value in range #{hexid}"; exit ) : ( lastdec = "#{range[1]}".hex )
        if firstdec < lastdec
          (firstdec..lastdec).each do |decid|
            alldevs_dec[decid] = 1
          end
        else
          puts "ERROR: Invalid range #{hexid} - #{range[0]} is bigger than #{range[1]}"
          exit
        end
      else
        decid = hexid.hex
        alldevs_dec[decid] = 1
      end
        end
        @alldevs_dec_sorted = alldevs_dec.keys.sort # this array contains all numbers given to script in sorted decimal format
  end

  def displayOutput
    @alldevs_dec_sorted.each do | dev |
          puts sprintf("%04X", dev)
        end
  end
end

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

,

sanaswati
No comments yet.

Leave a Reply

*