Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从bash向python发送参数_Python_Bash_Parameters_Split - Fatal编程技术网

从bash向python发送参数

从bash向python发送参数,python,bash,parameters,split,Python,Bash,Parameters,Split,我有一个bash脚本,它使用参数调用python脚本。 在bash脚本中,我正在读取一个文件,该文件包含一行由“”分隔的参数,然后使用我读取的行调用python脚本。 我的问题是python获取由空格分隔的参数 该行如下所示:“参数a”“参数B”“参数C” 代码示例: Bash脚本: LINE=`cat $tmp_file` id=`python /full_path/script.py $LINE` Python脚本: print sys.argv[1] print sys.argv[2]

我有一个bash脚本,它使用参数调用python脚本。 在bash脚本中,我正在读取一个文件,该文件包含一行由“”分隔的参数,然后使用我读取的行调用python脚本。 我的问题是python获取由空格分隔的参数

该行如下所示:“参数a”“参数B”“参数C” 代码示例:

Bash脚本:

LINE=`cat $tmp_file`
id=`python /full_path/script.py $LINE`
Python脚本:

print sys.argv[1]
print sys.argv[2]
print sys.argv[3]
收到的输出:

"param_a"
"Param 
B"
param_a
Param B
Param C
想要的输出:

"param_a"
"Param 
B"
param_a
Param B
Param C
如何按需要的方式将参数发送到Python脚本? 谢谢!

怎么样

id=`python /full_path/script.py $tmp_file`


问题在于bash如何传递参数。Python与此无关

因此,在将其发送到Python之前,您必须解决所有这些问题,我决定使用awk和xargs(但这里的xargs是实际的MVP)


这可以使用
bash
数组完成:

tmp_file='gash.txt'

# Set IFS to " which splits on double quotes and removes them
# Using read is preferable to using the external program cat
# read -a reads into the array called "line"
# UPPERCASE variable names are discouraged because of collisions with bash variables
IFS=\" read -ra line < "$tmp_file"

# That leaves blank and space elements in "line", 
# we create a new array called "params" without those elements
declare -a params
for((i=0; i < ${#line[@]}; i++))
do
    p="${line[i]}"
    if [[ -n "$p" && "$p" != " " ]]
    then
        params+=("$p")
    fi
done

# `backticks` are frowned upon because of poor readability
# I've called the python script "gash.py"
id=$(python ./gash.py "${params[@]}")
echo "$id"
给出:

1 param_a
2 Param B
3 Param C

不行,因为参数在
$tmp\u文件中没有正确分隔,shell会分割参数,python与此无关。您能显示
$tmp\u文件的确切内容吗
$tmp\u文件
为什么不传递
$tmp\u文件
(路径)到python并从那里打开和解析文件?嘿,谢谢你的提示-但是python脚本也需要手动运行,这意味着他需要获得3个参数…@Bramat-修改它来检查
sys.argv
,或者测试它是否大于1,或者甚至使用
raw\u input()提示用户输入
(因为您似乎正在使用旧的python 2)。
import sys

print "1",sys.argv[1]
print "2",sys.argv[2]
print "3",sys.argv[3]
1 param_a
2 Param B
3 Param C