Scripts – firstdev.bash

Print Friendly, PDF & Email


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.


server# bash -version
GNU bash, version 3.2.52(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2007 Free Software Foundation, Inc.


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.


# firstdev.bash 1A4 4

server# cat firstdev.bash
# A script to display nth hex number prior from given hex number
# If you have to assign 8 devices, ending number is A1
# ./lastdev.bash A8 8 (will show A1) - It includes given number in calculation

# Read the given arguments and assign to appropriate variables
lasthex=`echo $1 | tr "[:lower:]" "[:upper:]" `

# Check whether the firstval is numeric
if [[ ! -z $firstval ]] ; then
  firstval_check=`echo $firstval| tr -d "[0-9]"`
  if [[ ! -z $firstval_check ]]; then
    echo "ERROR - Numeric value $firstval is not an integer"
  echo "ERROR - Device count not given"
  echo "firstdev.bash <hex-devid> <dev_count>"

# Set up h2d function which will convert hex to dec and vice versa
function h2d  {
  echo "ibase=16; $@"|bc

function d2h {
  echo "obase=16; $@"|bc

# Convert last number into dec
lastdec=`h2d $lasthex`
let firstdec="$lastdec - $firstval + 1"
firsthex=`d2h $firstdec`
echo $firsthex


No comments yet.

Leave a Reply