简体   繁体   English

您如何将参数传递给作为参数传递的脚本?

[英]How would you pass arguments to a script being passed as an argument?

Basically the title, I don't know how to pass an argument to a script that is being passed as an argument in a shell command. 基本上是标题,我不知道如何将参数传递给在shell命令中作为参数传递的脚本。 Take the command below for example: 以下面的命令为例:

$someexe --arg-for-someexe scriptA.py $ someexe --arg-for-someexe脚本

somexe is going to read from scriptA and perform a bunch of tasks and then spit out some data into a destination file hard-coded into scriptA. somexe将要从scriptA中读取并执行一堆任务,然后将一些数据吐出到硬编码到scriptA中的目标文件中。 How would I pass the destination file into scriptA as a command line argument? 如何将目标文件作为命令行参数传递给scriptA? In python I know I can get arguments using sys.argv[i] but someexe will also try to grab those arguments and could cause problems. 在python中,我知道我可以使用sys.argv [i]来获取参数,但是someexe还将尝试获取这些参数并可能导致问题。 How could I accomplish this? 我该怎么做?

You could do something like that: 您可以这样做:

Caller script (scripta.py): 呼叫者指令码(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)

Receiver script (scriptb.py): 接收方脚本(scriptb.py):

if __name__ == '__main__':
    import sys
    args = sys.argv[1:]
    print(args)

To call script a - say in a terminal: 调用脚本-在终端中说:

python scripta.py arg1 42 arg2 43

Will print in scriptb.py terminal: 将在scriptb.py终端中打印:

['[arg1,', '42,', 'arg2,', '43]']

os.system() spawns it in a separate shell/process. os.system()在单独的shell /进程中生成它。 May or not be what you want. 可能不是您想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linux:如何将命令行参数传递给正在传递给脚本的命令行参数? - Linux:How to pass command line argument to an command line argument that is being passed to a script? 如何将参数传递给作为关键字参数传递的函数? - How do you pass parameters to a function passed as a keyword argument? 如何将脚本参数传递给 pdb (Python)? - How do you pass script arguments to pdb (Python)? 如何在导入脚本时传递参数变量? - How do you pass an argument variable while to an imported script? TypeError:正好接受4个参数(给定3个):请求参数未被传递 - TypeError : takes exactly 4 arguments (3 given) : request argument is not being passed 如何将 arguments 传递给正在传递到 discord 机器人中的 bot.get_command() 的命令? - How to pass arguments into a command being passed into bot.get_command() in a discord bot? 如何将参数传递给本身作为 kwarg 传递的函数 - How to pass arguments to a function that itself is passed as a kwarg 当 class 的 object 具有 arguments 时,如何将它作为方法的参数传递给另一个 ZA2F2ED4F8EBC2CBB61ZDDC - When an object of a class has arguments, how do you pass it as an argument of a method in another class 如何知道第三个参数是否传递给脚本? - How to know if third argument is passed to script? 如何将 arguments 传递给 python 脚本,该脚本将被提供给标准输入上的解释器? - How can I pass arguments to a python script being fed to the interpreter on stdin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM