0

Pretty simple script, but I am stuck.

It connects to a battery balancer, spits out the info into a json formatted file. I then have a pipe the output into jq to obtain the info I need. It works in the bash shell, but not in the script:

Here is the script:

echo "Checking battery voltages"
jkbms -p 3C:A5:19:7B:28:09 -o json > /home/bms/batt.log
echo cat /home/bms/batt.log | jq -r '.highest_cell_voltage'
echo "done"

The cat line shows this in the script output:

Checking battery voltages
parse error: Invalid numeric literal at line 1, column 4
done

From the shell it works as expected:

cat /home/bms/batt.log | jq -r '.highest_cell_voltage'
4.152044773101807

I have tried enclosing the whole cat command in quotes etc, but I am at a loss.

This, however, works:

echo "Checking battery voltages"
jkbms -p 3C:A5:19:7B:28:09 -o json > /home/bms/batt.log
batt=$(cat /home/bms/batt.log)
echo $batt | jq -r '.highest_cell_voltage'
#echo /usr/bin/cat /home/bms/batt.log
echo "done"
0

1 Answer 1

2
jkbms -p 3C:A5:19:7B:28:09 -o json > /home/bms/batt.log
echo cat /home/bms/batt.log | jq -r '.highest_cell_voltage'

The echo here is wrong. By the way, you can simplify the above to:

jkbms -p 3C:A5:19:7B:28:09 -o json|tee /home/bms/batt.log|jq -r '.highest_cell_voltage'

If I need to print the output of the comand on the screen, how do i do it without using echo?

If you want the saved output in /home/bms/batt.log, you can cat /home/bms/batt.log anytime.
If you want to print the output of the comand on the screen only at the time of execution, you can tee /dev/tty instead of tee /home/bms/batt.log.
If at the time of execution you want the output on screen as well as in the log file, you can tee /home/bms/batt.log /dev/tty at once.

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

2 Comments

Thanks for the simplication, but I need to understand how to do what I need above, as will need to be logging various things into variables to compare values. If I need to print the output of the comand on the screen, how do i do it without using echo?
I amended the answer.

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.