Python Forum

Full Version: cheating at running a pipeline
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i wanted to run a pipeline of programs. this time i cheated and let the bash command do it. but there are problems passing command names and arguments reliably. there might be meta characters like space or dollar sign. i can quote them but then i could have quotes in quotes in the string passed to bash. single quotes don't support any kind of escaping in bash, so if a string has a single quote i have to use double quotes and deal with all the hassle those bring (escape everything). the way i did it was putting everything in numbered environment variables and built a bash command string referencing them in a pipeline structure. double quotes are used, but resolving from variables does not need escaping in that string. some day, i will finish my python pipeline function. i am not providing input or capturing output. if i was, it would be more complicated.

    os.environ['V1']="command1"
    os.environ['V2']="argument1"
    os.environ['V3']="command2"
    os.environ['V4']="argument2"
    subprocess.call(['bash','-c','"$V1" "$V2"|"$V3" "$V4"])
of course, there are many ways to set this up. longer environment variable names were used in the real script i wrote, keeping the name prefix in a variable so it is coded in just one place.