1

I have a file called pids.txt that contains lines, such as the following:

123
456
789

Then, I wrote a Python script that does something with each line. The Python script gets run like:

my_script.py 123 456 789 -a "something here" -b "something else"

I am stuck at the part where I want to run a Unix command, piping the outputs of pids.txt to my_script.py. I think you have to do something with cat?

(This doesn't work)

cat pids.txt | ./myscript.py -a "something here" -b "something else"

Any idea?

1 Answer 1

5

You can use the $(command) construct (equivalent of backticks: `command`):

./myscript.py $(cat pids.txt) -a "something here" -b "something else"

The construct $(command) runs the command and substitutes the standard output of the command where the construct is placed.

Bash also provides you with $(< file) construct:

./myscript.py $(< pids.txt) -a "something here" -b "something else"

This reads the pids.txt file and substitutes its contents for the $(< ...) construct. (Thanks for this one go to glenn jackman)

Sign up to request clarification or add additional context in comments.

2 Comments

for bash, you can avoid calling out to cat with $(< pids.txt) that is run inside the shell.
@Jeff, note the $(< pids.txt) portion is NOT quoted, so each line will be passed to your python program as a separate argument. If you were to write `"$(<pids.txt)" you would get a single, newline-separated, argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.