Home > front end >  How do I assign multiple Variables from command output and getting only the values
How do I assign multiple Variables from command output and getting only the values

Time:01-31

On my VDSL Moden that runs Busybox I got only ash shell.

My goal is to put into multiple variables the output of the command /bin/dsl-tool iadtool

Output of the command is:

DSLAPI-Version=dsl-api-1.12.0
IADToolAPI-Version=0.0.1
LineState=Showtime
TransmissionSystem=G993.2 Region B Annex B
xDslMode=VDSL2
ActualDataRateUS=43992000
ActualDataRateDS=276368000
FirmwareVersion=8.13.1.12.1.7

At the end I need the values from ActualDataRateUS and ActualDataRateDS. My first Testscript looks like:

#!/bin/ash
#/bin/dsl-tool iadtool
input=$(/bin/dsl-tool iadtool | grep ActualDataRateUS)
echo $input

which sets the value input to ActualDataRateUS=43992000, but I only need the Value. I'm used to script in python. Is my goal even doable in bash?

CodePudding user response:

This should work:

#!/usr/bin/env sh

# Fail on error
set -o errexit

# ================
# CONFIGURATION
# ================
# Data
DATA=

# ================
# FUNCTIONS
# ================
# Read value matching key from data
# @param $1 Key
# @param $2 Data | Optional
read_value() {
  _key="$1"
  _data="${2:-$DATA}"

  printf '%s\n' "$_data" \
    | grep "$_key" \
    | sed -e 's#.*=\(\)#\1#'
}

# ================
# MAIN
# ================
{
  # Read data
  DATA="$(/bin/dsl-tool iadtool)"

  # Read values
  _ActualDataRateUS="$(read_value 'ActualDataRateUS')"
  _FirmwareVersion="$(read_value 'FirmwareVersion')"
  # ...

  # Print values
  # printf 'ActualDataRateUS is %d\n' "$_ActualDataRateUS"
  # printf 'FirmwareVersion is %s\n' "$_FirmwareVersion"
  # ...
}
  • Related