shell调用python脚本,并且向python脚本传递参数

1. 直接传递数值

shell中

python test.py 1 2

python脚本(test.py)中

import sys

print(sys.argv[1])
print(sys.argv[2])

#输出
1
2

2. 传递变量名

shell中

#bash中定义变量,注意等号间不能有空格
a = 2
b = "test"
python test.py $a $b
#csh中定义变量要用set
set a=2
set b="test"
python test.py $a $b

python脚本(test.py)中

import sys

print(sys.argv[1])
print(sys.argv[2])

#输出
2
test

猜你喜欢

转载自blog.csdn.net/u012991043/article/details/81220072