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 AFTER. Analogy in decimal world would be – Decimal number at 200 positions AFTER 11 is 211.
Usage
# lastdev.rb 1A1 4
1A4
# cat lastdev.rb
#!/usr/local/bin/ruby
# A script to display nth numbered hex number from given hex number
# Author - Ketan Patel - 01/06/2015
def usage
puts "Usage: lastdev.rb <hex-devid> <dev_count>"
puts "Example: lastdev.rb 1A1 4 => will print 1A4 (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? ? ( ld = sprintf("%04X", ARGV[0].hex.to_i + ARGV[1].to_i - 1 ); puts ld; puts sprintf("%04X", ARGV[0].hex) +":"+ lastdev ) : ( puts "Second argument #{ARGV[1]} must be integer"; usage ) )
No comments yet.