Python Forum
builing a long variable-length command-pipeline
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
builing a long variable-length command-pipeline
#4
You read from standard out and write to standard in, right?

#!/usr/bin/env python3

import sys

if len(sys.argv) > 1:
    pipe_in = sys.stdin.read().strip()
    sys.stdout.write('{} {} \n'.format(pipe_in, ' '.join(sys.argv[1:])))
else:
    pipe_in = sys.stdin.read().strip()
    sys.stdout.write('{} \n'.format(pipe_in))
You have to be careful with the quotes. If you put double quotes for the strings you get EOL error because of the bash quoting. I've needed some time to get it during this example. I have to test it before to publish Confused  

victor@jerry:/tmp$ echo hello | ./piped.py world
hello world

victor@jerry:/tmp$ echo hello | ./piped.py
hello

victor@jerry:/tmp$ echo hello | ./piped.py | ./piped.py
hello

victor@jerry:/tmp$ echo hello | ./piped.py beautiful | ./piped.py world
hello beautiful world

victor@jerry:/tmp$ echo hello | ./piped.py beautiful | ./piped.py beautiful world
hello beautiful beautiful world
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Messages In This Thread
RE: builing a long variable-length command-pipeline - by wavic - Dec-12-2017, 09:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a part of powershell command as a variable? ilknurg 2 1,237 Jul-26-2022, 11:31 AM
Last Post: ilknurg
  Os command output in variable shows wrong value paulo79 2 1,645 Apr-09-2022, 03:48 PM
Last Post: ndc85430
  How to use a variable in linux command in python code? ilknurg 2 1,720 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  Command output to Variable ironclaw 1 1,876 Aug-26-2021, 06:55 PM
Last Post: bowlofred
  Correct syntax for a variable inside a quotes: MP4Box command JonnyDriller 2 2,909 Feb-02-2020, 01:22 AM
Last Post: JonnyDriller
  Long command with characters not working in Python on Oracle Linux 7 iaas_infra 10 6,530 Jul-19-2019, 04:53 PM
Last Post: ichabod801
  Insert a variable in a Python > Cellular AT command ElectronicsNut 1 2,242 Jul-07-2019, 02:26 PM
Last Post: joe_momma
  pipeline between 2 programs Skaperen 11 6,129 Jul-02-2018, 10:48 PM
Last Post: Skaperen
  search and store a value from linux command output in to variable python prazy29 1 3,396 Apr-06-2018, 01:56 PM
Last Post: wavic
  [subprocess]>Run a cmd command and get output into a variable CSA75 4 23,134 Mar-13-2017, 09:33 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020