Scripts – lastdev.bash

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# 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 AFTER. Analogy in decimal world would be – Decimal number at 200 positions AFTER 11 is 211.

Usage


server# lastdev.bash 1A1 4
1A4


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

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

# Check whether the lastval is numeric
if [[ ! -z $lastval ]] ; then
  lastval_check=`echo $lastval| tr -d "[0-9]"`
  if [[ ! -z $lastval_check ]]; then
    echo "ERROR - Numeric value $lastval is not an integer"
    exit
  fi
else
  echo "ERROR - Device count not given"
  echo "lastdev.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 first number into dec
firstdec=`h2d $firsthex`
let lastdec="$firstdec + $lastval - 1"
lasthex=`d2h $lastdec`
echo $lasthex

,

sanaswati
No comments yet.

Leave a Reply

*