>  기사  >  백엔드 개발  >  가져오기를 사용하여 한 줄 명령줄에서 여러 줄 문을 실행하는 방법은 무엇입니까?

가져오기를 사용하여 한 줄 명령줄에서 여러 줄 문을 실행하는 방법은 무엇입니까?

Susan Sarandon
Susan Sarandon원래의
2024-10-18 12:01:07296검색

How to Execute Multiline Statements in a One-Line Command-Line with Imports?

Executing Multiline Statements in the One-Line Command-Line with Prerequisites

When executing Python one-line loops using the -c option, including module imports can lead to syntax errors. However, there are several approaches to overcome this limitation and execute multiline statements efficiently in a one-liner.

One solution is to use the echo command followed by piping the statements to Python:

echo -e "import sys\nfor r in range(10): print 'rob'" | python

Another method involves using Python's exec() function to execute the statements dynamically:

python -c "exec(\"import sys\nfor r in range(10): print 'rob'\")"

Alternatively, you can split the statements into multiple lines and pipe them separately to Python:

(echo "import sys" ; echo "for r in range(10): print 'rob'") | python

By utilizing these techniques, you can effectively execute complex multiline statements, including module imports, in a single-line command-line, fulfilling the requirement of incorporating such statements in a Makefile.

위 내용은 가져오기를 사용하여 한 줄 명령줄에서 여러 줄 문을 실행하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.