Tôi đang thực hiện
import subprocess
def tail[filename]:
process = subprocess.Popen[['tail', '-F', filename], stdout=subprocess.PIPE]
while True:
line = process.stdout.readline[]
if not line:
process.terminate[]
return
yield line
4 để đuôi cho một tệp trực tiếp và import subprocess
def tail[filename]:
process = subprocess.Popen[['tail', '-F', filename], stdout=subprocess.PIPE]
while True:
line = process.stdout.readline[]
if not line:
process.terminate[]
return
yield line
5 cho một chuỗi làm thế nào tôi có thể thực hiện một cái gì đó khi grep thành công? Ví dụcmd= os.system[tail -f file.log | grep -i abc]
if [cmd]:
#Do something and continue tail
Có cách nào tôi có thể làm điều này không? Nó sẽ chỉ đến khối
import subprocess
def tail[filename]:
process = subprocess.Popen[['tail', '-F', filename], stdout=subprocess.PIPE]
while True:
line = process.stdout.readline[]
if not line:
process.terminate[]
return
yield line
6 khi câu lệnh OS.System hoàn thành.
Joel
4.6909 Huy hiệu vàng37 Huy hiệu bạc52 Huy hiệu Đồng9 gold badges37 silver badges52 bronze badges
Khi được hỏi ngày 9 tháng 5 năm 2015 lúc 4:20May 9, 2015 at 4:20
0
Bạn có thể sử dụng
import subprocess
def tail[filename]:
process = subprocess.Popen[['tail', '-F', filename], stdout=subprocess.PIPE]
while True:
line = process.stdout.readline[]
if not line:
process.terminate[]
return
yield line
7 và đọc các dòng từ stdout:import subprocess
def tail[filename]:
process = subprocess.Popen[['tail', '-F', filename], stdout=subprocess.PIPE]
while True:
line = process.stdout.readline[]
if not line:
process.terminate[]
return
yield line
Ví dụ:
for line in tail['test.log']:
if line.startswith['error']:
print['Error:', line]
Đã trả lời ngày 9 tháng 5 năm 2015 lúc 4:30May 9, 2015 at 4:30
BlenderBlenderBlender
281K52 Huy hiệu vàng429 Huy hiệu bạc487 Huy hiệu đồng52 gold badges429 silver badges487 bronze badges
Trong
import subprocess
def tail[filename]:
process = subprocess.Popen[['tail', '-F', filename], stdout=subprocess.PIPE]
while True:
line = process.stdout.readline[]
if not line:
process.terminate[]
return
yield line
8, thực tế là import subprocess
def tail[filename]:
process = subprocess.Popen[['tail', '-F', filename], stdout=subprocess.PIPE]
while True:
line = process.stdout.readline[]
if not line:
process.terminate[]
return
yield line
9 trên Ubuntu, lệnh for line in tail['test.log']:
if line.startswith['error']:
print['Error:', line]
0 đã được xây dựng chỉ có thể trả về các giá trị số - trạng thái thoát, có ý nghĩa trong bối cảnh của một hàm hoặc tập lệnh có nguồn gốc. Nguồn for line in tail['test.log']:
if line.startswith['error']:
print['Error:', line]
1:Cú pháp của lệnh trả về là
return [exitstatus]
Tất cả mọi thứ khác với tập lệnh shell của bạn trông chính xác. Tôi nghĩ rằng bạn cần sử dụng
for line in tail['test.log']:
if line.startswith['error']:
print['Error:', line]
2 thay vào đó trả lại và triệt tiêu các echo-es khác.Trong trường hợp bạn cần trả thêm dữ liệu cho tập lệnh chính, bạn có thể xuất mọi thứ dưới dạng một dòng và chia các trường riêng biệt cho một số ký tự sẽ đóng vai trò của một dấu phân cách chính trong tập lệnh chính mà bạn có thể chuyển đổi chuỗi thành một mảng. Ví dụ: trong đó
for line in tail['test.log']:
if line.startswith['error']:
print['Error:', line]
3 là dấu phân cách của chúng tôi và for line in tail['test.log']:
if line.startswith['error']:
print['Error:', line]
4 sẽ hỗ trợ ký tự dòng mới trong for line in tail['test.log']:
if line.startswith['error']:
print['Error:', line]
5]:echo -n "$COLOR","$exitstatus"
Thông tin khác được cung cấp bởi tập lệnh và không được yêu cầu bởi tập lệnh chính có thể được chuyển hướng đến một số tệp nhật ký:
$ cat whiptail.sh
#!/bin/sh
log_file='/tmp/my.log'
COLOR=$[whiptail --inputbox "What is your favorite Color?" 8 78 Blue --title "Example Dialog" 3>&1 1>&2 2>&3]
exitstatus=$?
if [ $exitstatus = 0 ]; then
echo "User selected Ok and entered $COLOR" > "$log_file"
echo -n "$COLOR","$exitstatus"
else
echo "User selected Cancel." >> "$log_file"
echo -n "CANCEL","$exitstatus"
fi
Thật không may, tôi không có nhiều kinh nghiệm với Python, nhưng đây là một tập lệnh .Py mẫu có thể xử lý đầu ra của tập lệnh .sh trên [tham khảo]:
$ cat main-script.py
#!/usr/bin/python
import subprocess
p = subprocess.Popen[['./whiptail.sh'], stdout=subprocess.PIPE]
p = p.communicate[][0]
p = p.split[","]
print "Color: " + p[0]
print "ExitCode: " + p[1]
Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về lệnh hệ thống Python. Trước đây chúng tôi đã tìm hiểu về số ngẫu nhiên Python.
Lệnh hệ thống Python
Trong khi tạo một chương trình trong Python, bạn có thể cần phải vượt qua một số lệnh shell cho chương trình của mình. Ví dụ: nếu bạn sử dụng IDE
for line in tail['test.log']:
if line.startswith['error']:
print['Error:', line]
6, bạn có thể nhận thấy rằng có tùy chọn để chia sẻ dự án của bạn trên GitHub. Và bạn có thể biết rằng việc chuyển tệp được thực hiện bởi Git, được vận hành bằng dòng lệnh. Vì vậy, Pycharm thực hiện một số lệnh shell trong nền để làm điều đó. Tuy nhiên, trong hướng dẫn này, chúng tôi sẽ tìm hiểu một số điều cơ bản về việc thực thi các lệnh shell từ mã Python của bạn.git, which is operated using command line. So, Pycharm executes some shell commands in background to do it. However, In this tutorial we will learn some basics about executing shell commands from your python code.Chức năng python os.system []
Chúng ta có thể thực thi lệnh hệ thống bằng cách sử dụng hàm os.system []. Theo tài liệu chính thức, người ta đã nói rằng
Điều này được thực hiện bằng cách gọi hệ thống hàm C tiêu chuẩn [] và có cùng giới hạn.
Tuy nhiên, nếu lệnh tạo ra bất kỳ đầu ra nào, nó được gửi đến luồng đầu ra tiêu chuẩn phiên dịch. Sử dụng lệnh này không được khuyến khích. Trong mã sau, chúng tôi sẽ cố gắng biết phiên bản GIT bằng lệnh hệ thống
for line in tail['test.log']:
if line.startswith['error']:
print['Error:', line]
7.import os
cmd = "git --version"
returned_value = os.system[cmd] # returns the exit code in unix
print['returned value:', returned_value]
Đầu ra sau đây được tìm thấy trong Ubuntu 16.04 khi Git được cài đặt.
import subprocess
def tail[filename]:
process = subprocess.Popen[['tail', '-F', filename], stdout=subprocess.PIPE]
while True:
line = process.stdout.readline[]
if not line:
process.terminate[]
return
yield line
0Lưu ý rằng chúng tôi không in đầu ra lệnh phiên bản Git thành bảng điều khiển, nó được in vì bảng điều khiển là luồng đầu ra tiêu chuẩn ở đây.
Chức năng của Python SubProcess.call []
Trong phần trước, chúng tôi đã thấy rằng chức năng
for line in tail['test.log']:
if line.startswith['error']:
print['Error:', line]
8 hoạt động tốt. Nhưng nó không nên cách thực thi các lệnh shell. Chúng tôi sẽ sử dụng mô -đun phụ Python để thực thi các lệnh hệ thống. Chúng ta có thể chạy các lệnh shell bằng cách sử dụng hàm for line in tail['test.log']:
if line.startswith['error']:
print['Error:', line]
9. Xem mã sau tương đương với mã trước đó.import subprocess
def tail[filename]:
process = subprocess.Popen[['tail', '-F', filename], stdout=subprocess.PIPE]
while True:
line = process.stdout.readline[]
if not line:
process.terminate[]
return
yield line
1Và đầu ra cũng sẽ giống nhau.
Chức năng của Python SubProcess.check_output []
Cho đến nay, chúng tôi đã thực hiện các lệnh hệ thống với sự trợ giúp của Python. Nhưng chúng tôi không thể điều khiển đầu ra được tạo ra bởi các lệnh đó. Sử dụng chức năng
return [exitstatus]
0, chúng ta có thể lưu trữ đầu ra trong một biến.import subprocess
def tail[filename]:
process = subprocess.Popen[['tail', '-F', filename], stdout=subprocess.PIPE]
while True:
line = process.stdout.readline[]
if not line:
process.terminate[]
return
yield line
2Nó sẽ tạo ra đầu ra như sau
import subprocess
def tail[filename]:
process = subprocess.Popen[['tail', '-F', filename], stdout=subprocess.PIPE]
while True:
line = process.stdout.readline[]
if not line:
process.terminate[]
return
yield line
3Vì vậy, trong các phần trên, chúng tôi đã thảo luận về các ý tưởng cơ bản về việc thực hiện lệnh hệ thống Python. Nhưng không có giới hạn trong học tập. Nếu bạn muốn, bạn có thể tìm hiểu thêm về lệnh hệ thống Python bằng mô -đun phụ từ tài liệu chính thức.