开发者

How to regain control of a command with a wrapper around it?

开发者 https://www.devze.com 2023-04-03 19:16 出处:网络
My company has a habit of putting wrappers around their startup program commands such as: \'MGC_HOME/bin/dmgr_ic\'

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. A开发者_开发问答ny 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.


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.

  • -


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消