Scripts – firstdev.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 a hexadecimal id, and want to find out what is the device id at X positions before. Analogy in decimal world would be – Decimal number at 200 positions before 211 is 11.

Usage


# firstdev.rb 1A4 4
1A1


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

# A script to display nth hex number prior from given hex number
# Author - Ketan Patel - 01/06/2015

def usage
  puts "Usage:   firstdev.rb <hex-devid> <dev_count>"
  puts "Example: firstdev.rb 1A4 4 => will print 1A1 (1A1:1A4 = 4 devices)"
  exit
end

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

ARGV.length != 2 ? ( usage ) : ( ARGV[1].numeric? ? ( fd = sprintf("%04X", ARGV[0].hex.to_i - ARGV[1].to_i + 1 ); puts fd; puts fd +":"+ sprintf("%04X", ARGV[0].hex) ) : ( puts "Second argument #{ARGV[1]} must be integer"; usage ) )

, ,

sanaswati
No comments yet.

Leave a Reply

*