CCCPaste

testing shell redirect of commands with Pytho...

#!/usr/bin/env python3

"""
[deadeye@nexus ~]$ python3 out.py \
<(echo "Hello World") \
<(echo "Enter the Matrix") \
<(echo "Oh, it's an countdown starting at 63") \
<(echo "Another argument")
Printing content from 1. argument: /dev/fd/63
Hello World

Printing content from 2. argument: /dev/fd/62
Enter the Matrix

Printing content from 3. argument: /dev/fd/61
Oh, it's an countdown starting at 63

Printing content from 4. argument: /dev/fd/60
Another argument
"""

import sys

arguments = sys.argv[1:]
message = rf"""
python3 {sys.argv[0]} \
<(echo "Hello World") \
<(echo "Enter the Matrix") \
<(echo "Oh, it's an countdown starting at 63") \
<(echo "Another argument")
""".lstrip()

if not arguments:
    raise SystemExit(message)

for number, file in enumerate(sys.argv[1:], start=1):
    print(f"Printing content from {number}. argument: {file}")

    if not file.startswith("/dev/fd/"):
        print(f"Argument {number} is not a PIPE: {file}")
        continue

    with open(file) as fd:
        try:
            while chunk := fd.read(1024):
                print(chunk, end="")

        except UnicodeDecodeError:
            print(
                f"Could not decode file '{file}' from argument {number}. UTF8 encoding is expected. Don't cat binary files"
            )
            break

    print()