如何通过包装器重新获得对命令的控制?

发布于 2024-12-03 15:17:10 字数 310 浏览 1 评论 0原文

我的公司习惯于在启动程序命令周围放置包装器,例如:

“MGC_HOME/bin/dmgr_ic”,

它将启动 MGC 环境,允许用户简单地键入“dmgr_ic”,它将检查一些环境变量并加载程序你。这些包装器很有用,但我想在原始命令中添加一些选项,例如

MGC_HOME/bin/dmgr_ic -geometry 800x600+0+0

来调整放置不当的窗口的大小。有什么想法吗?我不能以任何方式接触原始包装,也不想制作我自己的副本或版本。如果简单地使用包装器并以某种方式捕获有问题的命令并对其进行修改,那就太好了。

谢谢。

My company has a habit of putting wrappers around their startup program commands such as:

'MGC_HOME/bin/dmgr_ic'

which would start the MGC environment, allowing one to simply type 'dmgr_ic' and it will check some environment variables and load the program for you. These wrappers are useful, but I would like to add some options to the original command, such as

MGC_HOME/bin/dmgr_ic -geometry 800x600+0+0

to resize a poorly placed window. Any ideas? I cannot touch the original wrapper in any way and don't want to make my own copy or version of it. It would be nice to simply use the wrapper and somehow capture the command in question and modify that.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘逸的'云 2024-12-10 15:17:10

涉及修改包装器脚本的选项 - 这些想法足够好,他们可能会愿意更改它们:

  • 添加由新的 -xargs xyz 包装器参数提供给包装器的参数(最佳)

  • 添加通过特殊环境变量提供给包装器的参数

  • 添加通过 ~/.dmgr_ic_rc 文件提供给包装器的参数

涉及的选项不修改包装器脚本:

  • 创建一个 Perl 脚本(称为 dmgr_ic),将当前包装器复制到 /tmp 中,对其进行修改以添加-xargs 选项,并调用该临时副本。这不是一个好主意,但可行。

  • -

Options involving modification of wrapper script(s) - the ideas are good enough that they might buy into changing them:

  • Add parameters supplied to the wrapper by a new -xargs xyz wrapper parameter (best)

  • Add parameters supplied to the wrapper via a special environmental variable

  • Add parameters supplied to the wrapper via a ~/.dmgr_ic_rc file

Options involving NOT modifying wrapper scripts:

  • Create a Perl script (called dmgr_ic) that copies the current wrapper into /tmp, modifies it to add the -xargs option, and calls that temporary copy. NOT a good idea but works.

  • -

酒与心事 2024-12-10 15:17:10

这些包装脚本是否非常复杂,或者只是一个衬垫?

可以运行set -xv,然后使用bash -n运行包装器脚本。这不会执行该命令,但会打印出应该执行的内容,我假设可能是最后一行。

然后,您可以获取实际执行的命令,并将您自己的内容附加到其中。这将涉及包装包装器脚本。

他们需要更改包装脚本并使其更加灵活。我不介意默认值,但允许它们被命令行参数覆盖。例如,默认值可能是:

somescript -f foo -b bar -u uff

但是,如果我这样做,

somescript -f fufu

脚本将执行:

somescript -f fufu -b bar -u uff

这可以通过创建 Perl 包装器并使用 Getopt::Long.一个快速而肮脏的实现可能看起来像这样:

use GetOpt::Long;

# Default Values for Paremters
my $foo = bar
my $bar = foo
my $fubar;           #No default, but valid parameter

GetOptions (
    "foo:s" => \$foo,
    "bar:s  => \$bar,
    "fubar:s" => \$fubar,
) or die qq($USAGE\n);

if (defined $fubar) {
    $fubar = "-fubar $fubar";
}
system qq(mycommand -foo $foo -bar $bar $fubar);

不是 100% 有效的代码,但你应该明白这个想法。

这个想法是您需要小心像 -fubar 这样的可选参数。在上面,我将 $fubar 与参数和值(如果有参数 $fubar)结合起来。另一种选择是使用哈希来存储参数(可以使用 GetOptions),然后通过哈希构建命令行参数。

无论如何,让包装器更加灵活并提供所需的默认值和覆盖这些默认值的能力并不难。

Are these wrapper scripts very complex, or just one liners?

It might be possible to run set -xv, then run the wrapper script with bash -n. This won't execute the command, but will print out what is suppose to be executed which I'm assuming is probably the last line.

Then, you could take the command that's actually executed, and append your own stuff to it. That would involve wrapping the wrapper script.

They need to change the wrapper script and make them more flexible. I don't mind the defaults, but allow them to be overwritten by command line parameters. For example, the default might be:

somescript -f foo -b bar -u uff

However, if I did this

somescript -f fufu

The script would execute:

somescript -f fufu -b bar -u uff

This could easily be done by creating a Perl wrapper and using Getopt::Long. A quick and dirty implementation might look like this:

use GetOpt::Long;

# Default Values for Paremters
my $foo = bar
my $bar = foo
my $fubar;           #No default, but valid parameter

GetOptions (
    "foo:s" => \$foo,
    "bar:s  => \$bar,
    "fubar:s" => \$fubar,
) or die qq($USAGE\n);

if (defined $fubar) {
    $fubar = "-fubar $fubar";
}
system qq(mycommand -foo $foo -bar $bar $fubar);

Not 100% that's valid code, but you should get the idea.

The idea is that you need to be careful of optional parameters like -fubar. In the above, I combine $fubar with the parameter and the value if there is a parameter $fubar. Another option is to use a hash for storing the parameters (possible with GetOptions), and then building the command line parameter via the hash.

Anyway, it's not that hard to make wrappers more flexible and provide both the desired defaults and the ability to override those defaults.

揽月 2024-12-10 15:17:10

尝试

mv MGC_HOME/bin/dmgr_ic MGC_HOME/bin/dmgr_ic.orig

文本文件

#!/bin/sh
MGC_HOME/bin/dmgr_ic.orig -geometry 800x600+0+0

然后创建 MGC_HOME/bin/dmgr_ic 作为包含then 的

chmod 755 MGC_HOME/bin/dmgr_ic

Try

mv MGC_HOME/bin/dmgr_ic MGC_HOME/bin/dmgr_ic.orig

Then create MGC_HOME/bin/dmgr_ic as a text file containing

#!/bin/sh
MGC_HOME/bin/dmgr_ic.orig -geometry 800x600+0+0

Then

chmod 755 MGC_HOME/bin/dmgr_ic
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文