简体   繁体   English

Linux:如何将命令行参数传递给正在传递给脚本的命令行参数?

[英]Linux:How to pass command line argument to an command line argument that is being passed to a script?

I have two scripts say script1.py and script2.sh . 我有两个脚本,分别是script1.pyscript2.sh

script1.py takes as command line argument say "-t somethinng" and script2.sh takes script1.py as its command line argument say : script2.sh -class script1.py . script1.py将命令行参数说为“ -t somethinng”,而script2.sh将script1.py作为命令行参数说: script2.sh -class script1.py

I can execute script1.py as standalone script by: "script1.py -t value" . 我可以通过以下方式将script1.py作为独立脚本执行: “ script1.py -t value”

Now what I want to do is to pass command lines to script1.py at the same time when I am passing script1.py to script2.sh as command line argument. 现在,我想在将script1.py作为命令行参数传递给script2.sh的同时,将命令行传递给script1.py。

I want to try something like this- 我想尝试这样的事情-

script2.sh --class script1:MyClass"-t value" --verbose script2.sh --class script1:MyClass“ -t值” --verbose

script2.sh is all set up to get Myclass from --class argument. script2.sh都已设置为从--class参数获取Myclass。 Now above command is what i want to do . 现在上面的命令是我想要做的。 // where MyClass is a class in script1.py //其中MyClass是script1.py中的类

Are script2.sh and script1.py your code or somebody else's? script2.shscript1.py您的代码还是其他人的代码?

If they are yours (or at least script2.sh ), you can implement such feature yourself (using argparse or equivalent); 如果它们是您的(或至少是script2.sh ),则您可以自己实现此功能(使用argparse或同等功能); if not, well there's no built in way to do that, as in: what you're asking has nothing to do with the generic Bash/shell functionality of passing arguments. 如果不是这样,那么就没有内置的方法可以做到这一点,例如:您要问的与传递参数的通用Bash / shell功能无关。

PS Or what @hek2mgl said might also just work. PS或@ hek2mgl所说的也可能起作用。

Brackets cannot being used here. 此处不能使用括号。 The simplest and most common way would be to pass the whole python sub command-line as a string: 最简单,最常见的方法是将整个python子命令行作为字符串传递:

script2.sh -class "script1.py -t value" --verbose

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM