#!/usr/bin/env python3 import re import sys import subprocess print( re.sub( r"^(```+)\$ (.*)\n((?:.+\n)*)\1$", lambda m: subprocess.check_output( m.group(2), input=m.group(3), shell=True, encoding="utf-8" ), sys.stdin.read(), flags=re.MULTILINE, ) )