Scripts – firstdev.bash

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# bash -version
GNU bash, version 3.2.52(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2007 Free Software Foundation, Inc.

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.bash 1A4 4
1A1


server# cat firstdev.bash
#!/bin/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:]" `
firstval=$2

# 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"
    exit
  fi
else
  echo "ERROR - Device count not given"
  echo "firstdev.bash <hex-devid> <dev_count>"
  exit
fi

# 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

,

sanaswati
No comments yet.

Leave a Reply

*