I'm trying to pass the data in the pipe through the python script and write this data into a separate variable for further processing
The main purpose of the script is to be a layer between pipes, to record parsing errors and what leads to them.
Application:
echo '{"user": "Basic dGVzdDp0ZXN0"}' | script.py | rotatelogs {....}
I made a script:
cmd = ["/usr/bin/jq -c \'.user |= if test(\"^[Bb]asic \") then .[6:] | @base64d | gsub (\":.*$\"; \"\") else . end \'"]
with open('/dev/stdin') as f:
try:
subprocess.run(cmd, check = True, shell=True)
except subprocess.CalledProcessError:
with open('/path/to/parseerror.log', 'w') as pfile:
pfile.write(f.read())
Command in subprocess.run executes successfully and produces its output, but f.read() become empty
If I move the reading of the variable f.read() to execution subprocess.run
, then I will get the value of the variable, but the command subprocess.run
in will not be executed (get null pipe input).
with open('/dev/stdin') as f:
line=(f.read())
try:
subprocess.run(cmd, check = True, shell=True)
except subprocess.CalledProcessError:
....
How can I combine the execution of the command with the parameters from the pipe and record the incoming pipe itself? The main goal is to pass the command execution through the script and write the received pipe parameters to a separate file