bash pipe to python file

00_8_3·2022년 12월 27일
0

pipe를 이용한 문자열을 python으로 넘기기

  • 1
#!/usr/bin/env python

import sys

def hello(variable):
    print (variable)

data = sys.stdin.read()
hello(data)
  • 2
#!/bin/bash

echo "test" | python3 t.py

sh 파일을 실행하면 "test" 문자열이 py 파일에 전달됨.

sys.stdin.read()

문자열이 개행문자가 포함 되어있어

print를 찍어보면

내가 넣은 문자열
\n
-----

이렇게 \n 까지 갖게되어 오류가 발생했다. rstrip 함수를 사용하여 한줄 문자열로 바꾸어 오류 해결하자!

해결

data=sys.stdin.read().rstrip("\n")

참고

https://stackoverflow.com/questions/11109859/pipe-output-from-shell-command-to-a-python-script#answer-11109926

https://codechacha.com/ko/python-remove-newline-in-string/

0개의 댓글