[英]Passing shell script variable to Python script as argument
Am writing shell script which takes argument, 正在编写带有参数的shell脚本,
while getopts ":i:o:m" opt; do
case $opt in
i) a="$OPTARG"
;;
o) b="$OPTARG"
;;
m) c="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&3
;;
esac
done
then am adding 然后添加
python filename.py a b c
instead value of variable, variable name is sent to the Python program. 取而代之的是变量的值,变量名被发送到Python程序。
Please help to solve this problem. 请帮助解决此问题。
When you want to get the value of variable in shell use $variable_name. 当您想在shell中获取变量的值时,请使用$ variable_name。 See the below example
请参阅以下示例
root@s:/tmp# cat t.sh
#!/bin/sh
a="test"
b="test2"
c="test3"
python test.py $a $b $c
root@s:/tmp# cat test.py
#!/usr/bin/python
import sys
print sys.argv[1:]
root@s:/tmp# ./t.sh
['test', 'test2', 'test3']
You can access environment variables by name, but you need to export them. 您可以按名称访问环境变量,但是需要导出它们。
on bash 猛扑
myvariable="something"
export myvariable
on python 在python上
import os
print os.environ["myvariable"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.