0

I have a block of text that I want to cat into bash and then send to python so I can work with it more.

So my command line is cat input_file | sh bash_file.sh

My sh file is

#!/bin/sh
input_data=$(cat)
"$input_data" | python3 ./python_file.py

and my python file is

contents = sys.stdin.read()
print(contents)

But nothing is actually getting stored to contents. How can I print out what I have catted into my shell script file from python?

2 Answers 2

2

You've got it mostly right, but you're not printing the contents of $input_data to stdin. Instead you're trying to run a command named $input_data.

Use printf to print the contents of the variable by changing the last line of your shell script like this:

printf "%s" "$input_data" | python3 ./python_file.py
5
  • 1
    Shouldn't that be printf '%s' "$input_data"? (I guess the original script could just as well simply use cat | ... without the variable anyway, though.)
    – DonHolgo
    Nov 25, 2020 at 9:49
  • Good point, @DonHolgo. Without using %s the shell will interpret sequences like \n in the text instead of printing them verbatim. I've updated my answer. (As for cat | ..., I assume the OP is actually doing some additional processing to populate $input_data.
    – satwell
    Nov 25, 2020 at 14:54
  • cat | ... does not work because it does not quit and sys.stdin.read() waits for EOF. But you could use python3 ... <<< "$input_data".
    – pLumo
    Nov 25, 2020 at 14:59
  • @pLumo If cat doesn't quit, do we even get past the assignment line?
    – DonHolgo
    Nov 25, 2020 at 15:34
  • Sure, with Ctrl+d
    – pLumo
    Nov 25, 2020 at 16:06
1

The standard input of your shell script is passed to the python command you have into the script, similarly to the way the standard output of your python script is going to the stdout of the shell script. So, there is no need to use a variable, this is enough:

#!/bin/sh
python python_file.py

and you can call it either with your cat file | sh script.sh or better

sh script.sh < file

If you need more readability, you could use cat - | python script.py, where - means stdin, or add a comment saying that this execution waits for the standard input.

1
  • 1
    Good answer. A simple and clean solution. Nov 26, 2020 at 14:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .