0

Background:

I'm working on some OBDII software and I'm attempting to automate the connection process via bluetooth. I have a working script but I'd like to further automate it so it'll work on all *nix machines and not just mine (right now the bluetooth device's MAC is stored manually in the script).

My Problem: The output of this command is...

$ hcitool scan
Scanning ...

    00:18:56:68:AE:08     OBDII

I need a simple way of piping this into grep (or whatever works) and checking the output for the string "OBDII". If it sees it, then it takes that same line, copies the resulting MAC into a variable while stripping all whitespace and the OBDII identifier at the end, leaving only the MAC to be utilized further down into the script.

What's the simplest way to get this done?

Any help is appreciated!!

1

1 Answer 1

1

There's no reason to only conditionally store the output if the operation is successful -- easier to always store it, and check whether it's empty if you want to know if a match was found.

result=$(hcitool scan | awk '/OBDII/ { print $1 }')
if [[ $result ]]; then
  echo "Found a value: $result" >&2
else
  echo "No result found" >&2
fi
Sign up to request clarification or add additional context in comments.

9 Comments

I need more people in my life like you, Charles haha Thank you for the speedy response and explanation! I'm gonna do some research on "awk" (which is a useful tool but NOT my strongpoint) and try this bit of code out. Thank you again!
nod. awk is actually a full-fledged scripting language -- anything you can do with sed, grep, cut, etc., you can also do in awk -- so pipelines involving multiple of those tools are often best replaced with just one awk command.
BTW, since this uses [[ $result ]], you'll want to be sure your shebang is #!/bin/bash, not #!/bin/sh; otherwise, change that test to [ -n "$result" ].
Beautiful! Thanks again for all the help and insight. The script doesn't work right out of the box for some reason but I'm marking this one as answered because it got me going in the right direction and I can definitely work with that. Thank you again for your help!
If you want to gist the output from running with set -x (aka bash -x scriptname), I may have the bandwidth to help you debug.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.