DeaD_EyE

testing shell redirect of commands with Python

Dec 19th, 2025
1,920
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 1 0
  1. #!/usr/bin/env python3
  2.  
  3. """
  4. [deadeye@nexus ~]$ python3 out.py \
  5. <(echo "Hello World") \
  6. <(echo "Enter the Matrix") \
  7. <(echo "Oh, it's an countdown starting at 63") \
  8. <(echo "Another argument")
  9. Printing content from 1. argument: /dev/fd/63
  10. Hello World
  11.  
  12. Printing content from 2. argument: /dev/fd/62
  13. Enter the Matrix
  14.  
  15. Printing content from 3. argument: /dev/fd/61
  16. Oh, it's an countdown starting at 63
  17.  
  18. Printing content from 4. argument: /dev/fd/60
  19. Another argument
  20. """
  21.  
  22. import sys
  23.  
  24. arguments = sys.argv[1:]
  25. message = rf"""
  26. python3 {sys.argv[0]} \
  27. <(echo "Hello World") \
  28. <(echo "Enter the Matrix") \
  29. <(echo "Oh, it's an countdown starting at 63") \
  30. <(echo "Another argument")
  31. """.lstrip()
  32.  
  33. if not arguments:
  34.     raise SystemExit(message)
  35.  
  36. for number, file in enumerate(sys.argv[1:], start=1):
  37.     print(f"Printing content from {number}. argument: {file}")
  38.  
  39.     if not file.startswith("/dev/fd/"):
  40.         print(f"Argument {number} is not a PIPE: {file}")
  41.         continue
  42.  
  43.     with open(file) as fd:
  44.         try:
  45.             while chunk := fd.read(1024):
  46.                 print(chunk, end="")
  47.  
  48.         except UnicodeDecodeError:
  49.             print(
  50.                 f"Could not decode file '{file}' from argument {number}. UTF8 encoding is expected. Don't cat binary files"
  51.             )
  52.             break
  53.  
  54.     print()
Advertisement