Hướng dẫn write shell script python - viết shell script python

sys.exit[myString] không có nghĩa là "trả về chuỗi này". Nếu bạn chuyển một chuỗi sang

python yourscript 2> return_file
0,
python yourscript 2> return_file
0 sẽ coi chuỗi đó là một thông báo lỗi và nó sẽ viết chuỗi đó thành

output=$[[your command here] 2> &1]
0. Khái niệm gần nhất với giá trị trả về cho toàn bộ chương trình là trạng thái thoát của nó, phải là một số nguyên.

Nội dung chính ShowShow

  • Làm thế nào để bạn chuyển đầu ra của tập lệnh Python cho một biến shell?
  • Làm cách nào để chạy một chương trình python trong script shell?
  • Chúng ta có thể viết mã python trong script shell không?
  • Làm thế nào để bạn có được đầu ra của tập lệnh shell trong một tệp?

Nếu bạn muốn nắm bắt đầu ra được viết cho Stderr, bạn có thể làm một cái gì đó như

python yourscript 2> return_file

Bạn có thể làm một cái gì đó tương tự trong tập lệnh bash của bạn

output=$[[your command here] 2> &1]

Tuy nhiên, điều này không được đảm bảo chỉ nắm bắt giá trị được chuyển cho

python yourscript 2> return_file
0. Bất cứ điều gì khác được viết cho Stderr cũng sẽ được ghi lại, có thể bao gồm đầu ra ghi nhật ký hoặc dấu vết ngăn xếp.

example:

test.py

print "something"
exit['ohoh'] 

t.sh

va=$[python test.py 2>&1]                                                                                                                    
mkdir $va
output=$[[your command here] 2> &1]
2

chỉnh sửa

Không chắc chắn tại sao nhưng trong trường hợp đó, tôi sẽ viết một kịch bản chính và hai kịch bản khác ... trộn Python và bash là vô nghĩa trừ khi bạn thực sự cần.

import script1
import script2

if __name__ == '__main__':
    filename = script1.run[sys.args]
    script2.run[filename]

Phương pháp không hoạt động

Dưới đây là mã thực tế của tôi.

Code:::

export L=128

cd /home/betafish/Desktop/Cpp_codes/gnuradio/acoustic
echo "$["pwd"]"
echo "$L"
exec python ofdm_acoustic.py "$L"

Và tệp python khác là

Code:::

#!/usr/bin/env python
##################################################
# Gnuradio Python Flow Graph
# Title: Ofdm Acoustic
# Generated: Wed Dec  1 18:17:52 2010
##################################################

from gnuradio import audio
from gnuradio import blks2
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.wxgui import constsink_gl
from gnuradio.wxgui import fftsink2
from grc_gnuradio import blks2 as grc_blks2
from grc_gnuradio import wxgui as grc_wxgui
from optparse import OptionParser
import wx

class ofdm_acoustic[grc_wxgui.top_block_gui]:

	def __init__[self]:
		grc_wxgui.top_block_gui.__init__[self, title="Ofdm Acoustic"]
		_icon_path = "/home/betafish/.local/share/icons/hicolor/32x32/apps/gnuradio-grc.png"
		self.SetIcon[wx.Icon[_icon_path, wx.BITMAP_TYPE_ANY]]

		##################################################
		# Variables
		##################################################
		self.samp_rate = samp_rate = 32000

		##################################################
		# Blocks
		##################################################
		self.audio_sink_0 = audio.sink[48000, "", True]
		self.blks2_ofdm_demod_0 = grc_blks2.packet_demod_f[blks2.ofdm_demod[
				options=grc_blks2.options[
					modulation="qpsk",
					fft_length=512,
					occupied_tones=200,
					cp_length=[COLOR="rgb[255, 140, 0]"]L[/COLOR],
					snr=10,
					log=None,
					verbose=None,
				],
				callback=lambda ok, payload: self.blks2_ofdm_demod_0.recv_pkt[ok, payload],
			],
		]
		self.blks2_ofdm_mod_0 = grc_blks2.packet_mod_f[blks2.ofdm_mod[
				options=grc_blks2.options[
					modulation="qpsk",
					fft_length=512,
					occupied_tones=200,
					cp_length=L,
					pad_for_usrp=False,
					log=None,
					verbose=None,
				],
			],
			payload_length=0,
		]
		self.gr_complex_to_float_0 = gr.complex_to_float[1]
		self.gr_throttle_0 = gr.throttle[gr.sizeof_float*1, samp_rate]
		self.gr_wavfile_sink_0 = gr.wavfile_sink["/home/betafish/Desktop/Cpp_codes/gnuradio/acoustic/data_rx", 1, samp_rate, 8]
		self.gr_wavfile_source_0 = gr.wavfile_source["/home/betafish/Desktop/Cpp_codes/gnuradio/acoustic/data_tx.wav", True]
		self.wxgui_constellationsink2_0 = constsink_gl.const_sink_c[
			self.GetWin[],
			title="Constellation Plot",
			sample_rate=samp_rate,
			frame_rate=5,
			const_size=2048,
			M=4,
			theta=0,
			alpha=0.005,
			fmax=0.06,
			mu=0.5,
			gain_mu=0.005,
			symbol_rate=samp_rate/4.,
			omega_limit=0.005,
		]
		self.Add[self.wxgui_constellationsink2_0.win]
		self.wxgui_fftsink2_0 = fftsink2.fft_sink_c[
			self.GetWin[],
			baseband_freq=9000,
			y_per_div=10,
			y_divs=10,
			ref_level=50,
			sample_rate=samp_rate,
			fft_size=1024,
			fft_rate=30,
			average=False,
			avg_alpha=None,
			title="FFT Plot",
			peak_hold=False,
		]
		self.Add[self.wxgui_fftsink2_0.win]

		##################################################
		# Connections
		##################################################
		self.connect[[self.gr_wavfile_source_0, 0], [self.gr_throttle_0, 0]]
		self.connect[[self.gr_throttle_0, 0], [self.blks2_ofdm_mod_0, 0]]
		self.connect[[self.gr_complex_to_float_0, 0], [self.audio_sink_0, 0]]
		self.connect[[self.gr_complex_to_float_0, 1], [self.audio_sink_0, 1]]
		self.connect[[self.blks2_ofdm_mod_0, 0], [self.wxgui_fftsink2_0, 0]]
		self.connect[[self.blks2_ofdm_mod_0, 0], [self.wxgui_constellationsink2_0, 0]]
		self.connect[[self.blks2_ofdm_mod_0, 0], [self.gr_complex_to_float_0, 0]]
		self.connect[[self.blks2_ofdm_mod_0, 0], [self.blks2_ofdm_demod_0, 0]]
		self.connect[[self.blks2_ofdm_demod_0, 0], [self.gr_wavfile_sink_0, 0]]

	def set_samp_rate[self, samp_rate]:
		self.samp_rate = samp_rate
		self.wxgui_constellationsink2_0.set_sample_rate[self.samp_rate]
		self.wxgui_fftsink2_0.set_sample_rate[self.samp_rate]

if __name__ == '__main__':
	parser = OptionParser[option_class=eng_option, usage="%prog: [options]"]
	[options, args] = parser.parse_args[]
	tb = ofdm_acoustic[]
	tb.Run[True]

Tôi đã sử dụng L trong các không gian được đánh dấu màu đỏ

Làm thế nào để bạn chuyển đầu ra của tập lệnh Python cho một biến shell?

Làm cách nào để chạy một chương trình python trong script shell?.

var=`produce_output.py`.

Chúng ta có thể viết mã python trong script shell không?

Làm thế nào để bạn có được đầu ra của tập lệnh shell trong một tệp?

Nếu bạn muốn nắm bắt đầu ra được viết cho Stderr, bạn có thể làm một cái gì đó như

Bạn có thể làm một cái gì đó tương tự trong tập lệnh bash của bạn

Làm cách nào để chạy một chương trình python trong script shell?

Chúng ta có thể viết mã python trong script shell không?Open the terminal by searching for it in the dashboard or pressing Ctrl + Alt + T . Navigate the terminal to the directory where the script is located using the cd command. Type python SCRIPTNAME.py in the terminal to execute the script.

Chúng ta có thể viết mã python trong script shell không?

Làm thế nào để bạn có được đầu ra của tập lệnh shell trong một tệp?you can embed python code inside the bash shell script using here doc. First lets write a working python code. To demonstrate, let me write a small piece of Python code that just simply gets the file properties of a given file name.

Làm thế nào để bạn có được đầu ra của tập lệnh shell trong một tệp?

Nếu bạn muốn nắm bắt đầu ra được viết cho Stderr, bạn có thể làm một cái gì đó như.

Bạn có thể làm một cái gì đó tương tự trong tập lệnh bash của bạn

Tuy nhiên, điều này không được đảm bảo chỉ nắm bắt giá trị được chuyển cho

python yourscript 2> return_file
0. Bất cứ điều gì khác được viết cho Stderr cũng sẽ được ghi lại, có thể bao gồm đầu ra ghi nhật ký hoặc dấu vết ngăn xếp.

chỉnh sửa

Không chắc chắn tại sao nhưng trong trường hợp đó, tôi sẽ viết một kịch bản chính và hai kịch bản khác ... trộn Python và bash là vô nghĩa trừ khi bạn thực sự cần.

import script1
import script2

if __name__ == '__main__':
    filename = script1.run[sys.args]
    script2.run[filename]

Phương pháp không hoạt động

Bài Viết Liên Quan

Chủ Đề