-2

I'm writing down a bash script which connects to my GPS router using SNMP command. I'm using "snmpwalk", this is the command :

snmpwalk -v 2c 10.0.0.250 -c public enterprises.30140.7.2

It returns back :

SNMPv2-SMI::enterprises.30140.7.2.0 = STRING: "3203.558467N"

Now my main target is to get the number "3203.558467" without the "N".

I'm new to Linux/bash and after a couple of searches I found that there is such thing called "eval", it helped me out and separated the whole string into an array and putted "3203.558467N" at the 4th cell (The code is at the end).

It's still not what I wanted since "N" is there. I know that there are few more ways such as "sed" and "tr" but I don't know quite how to use them efficiently.
What is the best way to separate "3203.558467" in order to use this number for calculation?

#!/bin/bash

latitude=$(snmpwalk -v 2c 10.0.0.250 -c public enterprises.30140.7.2)
eval x=($latitude)
echo "latitude is : ${x[3]}"
Marked One
  • 59
  • 4
  • 13
  • note, you can use `snmpget` instead of `snmpwalk` when you are fetching a single oid. – meuh Aug 20 '15 at 14:23
  • What is the difference? is it wrong using snmpwalk? – Marked One Aug 20 '15 at 14:25
  • 1
    snmpwalk is meant to traverse a whole tree of oids from some starting point, whereas snmpget gets a single oid. I dont suppose there's much difference in your case. – meuh Aug 20 '15 at 16:44
  • possible duplicate of [How to remove the last character from a bash grep output](http://stackoverflow.com/questions/5074893/how-to-remove-the-last-character-from-a-bash-grep-output) – k1eran Aug 20 '15 at 16:45

1 Answers1

1

You can use bash parameter substition for that, with ${variable%?}

latitude=$(snmpwalk -v 2c 10.0.0.250 -c public enterprises.30140.7.2)
eval x=($latitude)
echo "latitude is : ${x[3]%?}"
jerik
  • 5,714
  • 8
  • 41
  • 80