亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法將變量從 python 腳本傳遞到 bash 腳本?

有沒有辦法將變量從 python 腳本傳遞到 bash 腳本?

30秒到達戰場 2023-04-25 17:21:56
我有多個需要順序執行的 python 代碼。我是通過使用 abash 腳本來完成的。#! /bin/shclearecho "Test 1: Database Comparison"python3 databasecomparison.pypython3 error.pyecho "Test 2: Conflicting ARFCNs"python3 conflictingARFCNs.pyecho "Test 3: Conflicting Cell IDs"python3 conflictingCID.pyecho "Test 4: Lonesome Location ID"python3 conflictingLAC.pyecho "Test 5: Empty Neighbour List"python3 neighbour.pyecho "Test 6: Missing IMSI"python3 IMSI.pyecho "The tests are complete!"notify-send "COMPLETED!"現在,我的 error.py python 代碼是#!/usr/bin/env python3import subprocess as sfile = open("doubt.txt","r") Counter = 0  # Reading from file Content = file.read() CoList = Content.split("\n")   for i in CoList:     if i:         Counter += 1          #print("This is the number of lines in the file") #print(Counter) if Counter > 1:    print("There is some erroneous value. Please restart the scanner")    s.call(['notify-send','Alert!','There is some erroneous value. Please restart the scanner'])我想將變量 Counter 的值從 python 代碼傳遞到 bash 腳本,以便我可以執行:if Counter > 1then breakfi但是,我無法傳遞變量 Counter。我在 stackoverflow 上查找了現有的解決方案,但說實話,我一直無法理解其中的任何一個。請幫忙。
查看完整描述

4 回答

?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

在您的情況下,您想將一個小整數傳遞給調用程序?;旧?,你有三種可能性,都有缺點或優點。

  1. 使用退出代碼

如果整數始終為非負且小于 256,您可以通過 Python 將其傳回并使用保存最近執行程序的退出代碼的exit變量在調用方獲取它。$?

python3 your_program.py
count=$?

雖然這種方法很簡單,但我不推薦它,原因有二:

  • 退出代碼旨在傳達錯誤,而不是正常數據。

  • 如果有一天你想用set -e(terminate-on-error) 來運行你的腳本,你就會有麻煩了。

  1. 使用標準輸出

將你要返回的整數寫到stdout,通過命令替換來獲取,即

count=$(python3 your_program.py)

缺點:如果有一天您想要向您的程序添加額外的輸出(例如,用于診斷),您必須將它寫入 stderr,否則它會污染您的結果計數。

  1. 使用文件

讓您的 Python 程序接受文件名,并將計數寫入該文件:

python3 your_program.py count_file
count=$(<countfile)

缺點:您必須關心正在創建的 count_files,例如,如果不再需要,請刪除它們。


查看完整回答
反對 回復 2023-04-25
?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

stdout 的任何輸出都可以捕獲到 bash 變量中。常規print將打印到標準輸出

hello.py

print('hello')

狂歡

RESULT=`python3 hello.py`
echo $RESULT  # hello


查看完整回答
反對 回復 2023-04-25
?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

我認為你應該讓你的 bash 腳本接受命令行參數。然后,在您的 python 腳本中,執行 subprocess.Popen(['your_bash_script', your_variable])。



查看完整回答
反對 回復 2023-04-25
?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

你可以使用if $1(第一個參數的值)并修改你的最后一行error.py你應該能夠得到你想要的結果。


import subprocess as s



file = open("doubt.txt","r") 

Counter = 0

  

# Reading from file 

Content = file.read() 

file.close()

CoList = Content.split("\n") 

  

for i in CoList: 

    if i: 

        Counter += 1

          

#print("This is the number of lines in the file") 

#print(Counter) 


if Counter > 1:

    print("There is some erroneous value. Please restart the scanner")

    s.call(['notify-send', str(Counter), 'Alert!','There is some erroneous value. Please restart the scanner'])

if $1 > 1

then break

fi

你可以用大約 3 行來完成這一切,會簡單得多:


import subprocess as s


with open ("doubt.txt","r") as f:

    if len(f.readlines()) > 1:

        s.call(['notify-send', Counter, 'Alert!','There is some erroneous value. Please restart the scanner'])

附言。with如果您不打算將上下文管理器與該函數一起使用open,請確保close在您的file對象上使用該方法。


查看完整回答
反對 回復 2023-04-25
  • 4 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號