무한히 실행되는 프로그램에 입력을 제공하는 방법은 무엇입니까? [폐쇄]

무한히 실행되는 프로그램에 입력을 제공하는 방법은 무엇입니까? [폐쇄]

무한히 실행되고 입력을 받아 출력을 던지는 Python 프로그램이 있습니다. 실행 중인 Python 스크립트에 입력을 제공하는 bash 프로그램을 작성하고 싶습니다. 그렇다면 프로그램이 실행되는 동안 어떻게 프로그램에 입력을 제공합니까?

답변1

쉬운 방법은 명명된 파이프를 사용하는 것입니다.

# Choosing a unique, secure name for the pipe left as an exercise
PIPE=/tmp/feed-data-to-program

# Create the named pipe
mkfifo "$PIPE"
# Start the Python program in the background
myprogram <"$PIPE" &
# Now grab an open handle to write the pipe
exec 3>"$PIPE"
# And we don't need to refer to the pipe by name anymore
rm -f "$PIPE"

# Later, the shell script does other work,...
# ...possibly in a loop?
while :; do
    ...
    ...
    # Now I've got something to send to the background program
    echo foo >&3
    ...
    ...
done

파일 시스템에 임시 항목을 추가하지 않는 것이 가장 좋습니다. 일부 쉘이 zsh이를 수행하는 방법을 제공한다는 것을 알고 있지만 이식 가능한 방법은 모르겠습니다.

관련 정보