簡體   English   中英

Python中的bash變量

[英]bash variables in Python

我有一個python腳本conve_cal.py,它帶有7個參數,如下所示:

enter code here
import numpy as np
import sys

def read_file(filename1,filename2):
    Input1 = np.loadtxt(filename1)
    Input2 = np.loadtxt(filename2)
    #print type(Input1)
    #print Input1
    return Input1,Input2

def NU_calc(T_avg,Trght,Ttop,rmv,argv1,argv2,arg3):
    ....

if __name__ == "__main__":
    T_avg = sys.argv[1]
    Trght = sys.argv[2]
    Ttop = sys.argv[3]
    rmv = sys.argv[4]
    arg3 = sys.argv[5]
    file1 = sys.argv[6]
    file2 = sys.argv[7]
    print (sys.argv)
    In1,In2 = read_file(file1,file2)
    #print type(arg3)
    NU_calc(T_avg,Trght,Ttop,rmv,In1,In2,arg3)

如果我在終端中單獨運行代碼

python2 conve_calc.py 285 282 300 0 noclip Input1.dat Input2.dat

該代碼根據需要提供輸出。 但是,如果我在bash腳本中對bash變量使用相同的語法,如下所示;

#!/bin/bash
...
...
t_avg=`grep "Average" test.avg |cut -f6 -d" "`
t_right=`grep "Tright" ./0/Input_conditions|cut -f 3 |cut -f 1 -d';'`
t_top=`grep "Ttop" ./0/Input_conditions|cut -f 3 |cut -f 1 -d';'`

echo "$t_right $t_top $Hr $Ht" >> $start_path/post_process_data/'HT_stat.dat'

rm test.avg tmp.dat test_fl.txt

# Call 
# Arg-1; Average Temp
## Arg-2; Temp of right wall
## Arg-3; Temp of top wall 
## Arg-4; # of faces to remove (0:None ,1...upto max of no. of faces)
## Arg-5; Right GradT file
## Arg-6; Top GradT file

rv0="0" # Change this to Remove the faces
rv1="1"
c1="noclip"
c2="middle"
i1="Input1.dat"
i2="Input2.dat"

python2 $start_path/conve_calc.py $t_avg $t_right $t_top $rv1 $c1 $i1 $i2 >> $start_path/post_process_data/\
'Common_out.dat'

但是使用此bash腳本輸入時,我遇到了以下錯誤,但無法找到這些輸入為什么出錯的原因。

Traceback (most recent call last):
  File "/home/meisu/OpenFOAM/meisu-2.4.0/Cyence_data/foam_adagio/conve_calc.py", line 69, in <module>
    file2 = sys.argv[7]
IndexError: list index out of range

我研究了各種堆棧解決方案,但沒有一個起作用。

您的問題是變量之一為空。 Shell展開變量,然后解析命令,因此認為該命令只有6個參數。 您可以添加兩種類型的防御性編碼。 首先,對於可能失敗的派生變量,請在使用它們之前對其進行測試。 其次,將您的參數括在引號中,以便正確傳遞空參數或帶有空格之類的參數的參數,這些參數會破壞命令解析。

#!/bin/bash
...
...
t_avg=`grep "Average" test.avg |cut -f6 -d" "`
if [ -z "$t_avg" ]; then
    echo t_avg failed >&2
    exit 2
fi

t_right=`grep "Tright" ./0/Input_conditions|cut -f 3 |cut -f 1 -d';'`
if [ -z "$t_right" ]; then
    echo t_right failed >&2
    exit 2
fi

t_top=`grep "Ttop" ./0/Input_conditions|cut -f 3 |cut -f 1 -d';'`
if [ -z "$t_top" ]; then
    echo t_top failed >&2
    exit 2
fi

echo "$t_right $t_top $Hr $Ht" >> $start_path/post_process_data/'HT_stat.dat'

rm test.avg tmp.dat test_fl.txt

# Call 
# Arg-1; Average Temp
## Arg-2; Temp of right wall
## Arg-3; Temp of top wall 
## Arg-4; # of faces to remove (0:None ,1...upto max of no. of faces)
## Arg-5; Right GradT file
## Arg-6; Top GradT file

rv0="0" # Change this to Remove the faces
rv1="1"
c1="noclip"
c2="middle"
i1="Input1.dat"
i2="Input2.dat"

python2 $start_path/conve_calc.py "$t_avg" "$t_right" "$t_top" "$rv1" "$c1" "$i1" "$i2" >> "$start_path/post_process_data/Common_out.dat"

您可以看到一個玩具玩具腳本確切地發送到您的python腳本,該玩具腳本僅打印其參數並退出

!#/usr/bin/env python
import sys
for i, arg in enumerate(sys.argv):
    print i, arg
print '---- end ----'

暫無
暫無

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

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