How can I have output these commands in shell script?
It comes over the socket... so you can read it, but we recommend tojson for scripts, e.g.:
output="$(echo "tojson(cache.stats())" | socat - /run/knot-resolver/control/1)"
There's the inconvenience of "> " occurrences meant for interactive use of the control socket, but it shouldn't be too hard to trim in the script.
--Vladimir