簡體   English   中英

如何從Shell腳本獲取loop變量以在python腳本中進行檢索

[英]How to get the variable of loop from Shell script to retrieve inside python script

抱歉,我是python的新手,我只有Shell腳本/ sh的基本知識。 我有兩個問題。 1)如何從bash獲取循環變量以在python腳本中進行檢索? 2)如何保持從多個文件到同一詞典的最大鍵值?

上面的那些問題是相關的。 說明:主腳本在Shell腳本中,在那里他們將循環一些Shell命令,然后對許多K運行Python腳本。

例如:

#!/bin/sh

for a in K*
do
 echo $a
   # Loop to call the file.
   for i in 1 2 3 4 5
   do
      # Cut the row and column
      grep -v '^#' $a/result*.txt | tr -s ' ' | cut -d ' ' -f 6 | cat > pos.txt
   done
chmod a+wx pie1.py
./pie1.py $a/pos.txt $a/neg.txt

chmod a+wx max.py
./max.py $a/output1.py

因此,在max.py中,我制作了一個字典,它將保存鍵和最大值。

#!/usr/bin/env python

#import pickle
from output1 import *
import subprocess

dicoMax = {}
# The Flist read from output1 that save all the Fscore for one File
dicoMax['K*']=max(Flist)
print(dicoMax)

我想從Shell腳本中檢索K *或變量作為max.py(python腳本)中字典的鍵,最后將K *的所有鍵和最大值保存在一個字典中。

TQ。

有兩種方法-您可以將其作為命令行參數傳遞(這是最好的方法),也可以將其插入環境變量中(這不太清楚)

閱讀本教程的python教程將是一個不錯的開始。

對於問題1和2:

#!/bin/sh

chmod a+wx pie1.py
for a in K*
do
 echo $a
   # Loop to call the file.
   for i in 1 2 3 4 5
   do
      # Cut the row and column
      grep -v '^#' $a/result*.txt | tr -s ' ' | cut -d ' ' -f 6 | cat > pos.txt
   done

./pie1.py $a/pos.txt $a/neg.txt
### passing the arguments or value through "$a"
./max.py "$a" $a/output1.py
done

所以在python中

#!/usr/bin/env python

from output1 import *
from maxList import *
import sys  

dicoMax = {}
var = sys.argv[1] ###important
print var  
### Flist is the list of the Fscore that save in the output1 
### var is the key passing by Shell script
dicoMax[var]=max(Flist)
print(dicoMax)
### mList is the dictionary that save in the maxList file (mList must   
### be declared first) 
### it will update new key:value pair for new loop of Shell script
dicoMax.update(mList)
### Save the updated mList in maxList
a=open('maxList.py', 'w')
a.write("mList=%s\n" % dicoMax)
a.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM