How can I parse nmcli output for modem number, identifier, and name?

Viewed 256

This will be an easy one for most of you I think, but what is the most effective way to parse the modem number and modemmanager folder number from this:

root@5109910:~# mmcli -L
    /org/freedesktop/ModemManager1/Modem/1 [Sierra Wireless Inc.] Sierra Wireless EM7345 4G LTE

The ModemManager1, 1 and [Sierra Wireless Inc.] Sierra Wireless EM7345 4G LTE are dynamic and I want to be able to manipulate the modem in a shell script.

To be more specific, I'd like to generate the following:

MODEM_NUMBER=$(mmcli -L | grep ***PATERN TO PARSE 1 HERE***)

MODEM_DIR=$(mmcli -L | grep ***PATERN TO PARSE ModemManager1 HERE***

MODEM=$(mmcli -L | grep ***PATTERN TO PARSE [Sierra Wireles etc etc HERE***)
1 Answers

Using pure bash you can do this in single step i.e. single invocation of mmcli -L command:

IFS='/ ' read -r _ _ _ mm _ mn mt < <(mmcli -L)

# chek variable's content
declare -p mm mn mt

Output:

declare -- mm="ModemManager1"
declare -- mn="1"
declare -- mt="[Sierra Wireless Inc.] Sierra Wireless EM7345 4G LTE"

Details:

  • IFS='/ ': Sets / or space as input field separators
  • read -r _ _ _ mm _ mn mt: Read 4th, 6th and 7th onwards test in variable mm, mn and mt while ignoring rest
  • < <(mmcli -L): Command substitution to invoke mmcli -L and feed it's output to read
Related