-1

I want to access printed value from python in bash script

for example of python code

# test.py
def main():
    print('True')

if __name__ == '__main__':
    s = input()
    if s == "True":
        main()

And, there are example(ex.sh) bash script

#!/bin/bash
echo {EX} | python test.py

Command:

bash ex.sh True

I hope to load variable from python in bash. It is fault but one of the ideas is like:

OUTPUT=$(echo {EX} | python test.py 2>&1)

I should appreciate if you advice me

  • A `return` in Python code is not visible from outside the program. The interface to the outside world is through stdin, stdout, stderr and the exit code. You probably want to `print()` to stdout and/or exit with `0` (success) or fail with a different exit code? – deceze Oct 06 '21 at 11:00
  • wirh `subprocess`? – cards Oct 06 '21 at 11:04
  • I haven't known it cannot be accessed from return to outside. So, if it is edited return True -> print('True'). How to access in bash? – Hanseo Park Oct 06 '21 at 11:10
  • 1
    By default, the Python *print* function writes to the standard output stream (*stdout*). Thus, if you *print* the desired output you can capture it using the Bash syntax you've shown. You code doesn't send anything to *stdout* –  Oct 06 '21 at 11:13

1 Answers1

0

Let's simplify this...

Create test.py like this:

print('Hello world')

Then, in your Bash shell:

OUTPUT=$(python test.py)
echo $OUTPUT
  • 1
    `echo "$OUTPUT"`, please. Without the quotes we get the bugs described in the question [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Oct 06 '21 at 11:24
  • 1
    (even better would be to choose a variable name in lower case; upper case is used for variables that reflect or modify shell and operating system component behavior, lower case is reserved for application use -- see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, keeping in mind that setting a shell variable with the same name as an environment variable will overwrite the latter). – Charles Duffy Oct 06 '21 at 11:25
  • Thanks a lot. I have add question. If output line is over 2 lines. how to choose specific line? – Hanseo Park Oct 06 '21 at 11:32
  • @HanseoPark, reading into an array with `readarray -t yourarray < – Charles Duffy Oct 06 '21 at 11:33
  • @BrutusForcus, btw, there are several preexisting answers identical to this one on the linked duplicate. Note the _Answer Well-Asked Questions_ section of [How to Answer](https://stackoverflow.com/help/how-to-answer), and the bullet point therein regarding questions that "have been asked and answered many times before". – Charles Duffy Oct 06 '21 at 11:37
  • @CharlesDuffy, It is not working like ${OUTPUT[1]} if print('TEST') and print(input()) in python, output line is continuous like "TEST {input()}" – Hanseo Park Oct 06 '21 at 11:39
  • @HanseoPark, ask a separate question with a [mre] for the problem. I'd need to see the details of what you did in enough detail to cause the same issue myself, and the comment above does not suffice. – Charles Duffy Oct 06 '21 at 12:13
  • @HanseoPark, ...or see https://ideone.com/AuLtTV for a working example. – Charles Duffy Oct 06 '21 at 12:17