![](/img/trans.png)
[英]Linux:How to pass command line argument to an command line argument that is being passed to a script?
[英]How would you pass arguments to a script being passed as an argument?
基本上是标题,我不知道如何将参数传递给在shell命令中作为参数传递的脚本。 以下面的命令为例:
$ someexe --arg-for-someexe脚本
somexe将要从scriptA中读取并执行一堆任务,然后将一些数据吐出到硬编码到scriptA中的目标文件中。 如何将目标文件作为命令行参数传递给scriptA? 在python中,我知道我可以使用sys.argv [i]来获取参数,但是someexe还将尝试获取这些参数并可能导致问题。 我该怎么做?
您可以这样做:
呼叫者指令码(scripta.py):
import os
def main(args):
os.system('python scriptb.py' + ' ' + str(args))
if __name__ == '__main__':
import sys
args = sys.argv[1:]
main(args)
接收方脚本(scriptb.py):
if __name__ == '__main__':
import sys
args = sys.argv[1:]
print(args)
调用脚本-在终端中说:
python scripta.py arg1 42 arg2 43
将在scriptb.py终端中打印:
['[arg1,', '42,', 'arg2,', '43]']
os.system()在单独的shell /进程中生成它。 可能不是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.