1

I have a python script with the following output:

$ sudo ./demo.py --backend bluepy poll C4:7C:8D:xx:xx:xx
Getting data from Mi Flora
FW: 3.1.8
Name: Flower care
Temperature: 17.4
Moisture: 39
Light: 138
Conductivity: 513
Battery: 99

I would like to use these output lines as bash variables in a bash script:

FW (=3.1.8), NAME (="Flower care"), TEMPERATURE (=17.4), MOISTURE (=39), LIGHT (=138), CONDUCTIVITY (=513), BATTERY (=99)

Any ideas how to do that?

2
  • Just to show what's the desidered value for the current variable. Commented Apr 20, 2018 at 19:03
  • Modify demo.py to make the output easier to process Commented Apr 20, 2018 at 19:04

2 Answers 2

1

Read the output in a loop and use a case statement to assign the variables.

while read -r prefix val; do
    case "$prefix" in 
        FW:) fw=$val ;;
        Name:) name=$val ;;
        ...
    esac
done < <(sudo ./demo.py --backend bluepy poll C4:7C:8D:xx:xx:xx)

FYI, you shouldn't use uppercase variable names, those are conventionally reserved for environment variables.

Sign up to request clarification or add additional context in comments.

Comments

1

IIUC, you can set all variables at once like that:

$ eval $(sudo ./demo.py --backend bluepy poll C4:7C:8D:xx:xx:xx | tail +2 | awk '{$1=tolower($1);print}' | sed 's,: ,=,' | sed 's,=,=",' | sed 's,$,",')
$ echo $fw
3.1.8
$ echo $name
Flower care
$ echo $temperature
17.4
$ echo $moisture
39
$ echo $conductivity
513
$ echo $battery
99

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.