简体   繁体   English

分发可加载的内置bash模块

[英]Distributing loadable builtin bash modules

I've written a built-in for bash which modifies the 'cd' command, a requirement for my software. 我为bash写了一个内置的修改'cd'命令,这是我的软件的要求。 Is there a way to actually distribute a loadable independently of bash itself? 有没有办法实际分发可加载的独立于bash本身? I'd ideally like to distribute just a drop in "additional feature" because I know people can be put off by patching and compiling their shell from source code. 我理想的是只想分发“附加功能”,因为我知道可以通过从源代码修补和编译shell来推迟人们。

I want to time how long a user is in a directory so I can determine where they want to be. 我想计算用户在目录中的时间,以便我可以确定他们想要的位置。 It's this functionality: http://github.com/joelthelion/autojump/tree/master rewritten as a bash builtin, for performance issues. 这是这个功能: http//github.com/joelthelion/autojump/tree/master重写为bash内置,用于性能问题。 This implementation uses $PROMPT_COMMAND to work but I wanted something integrated. 这个实现使用$PROMPT_COMMAND工作,但我想要集成一些东西。

It is unclear what you have modified but in any case, bash (like at least ksh93 which IIRC introduced the concept and zsh ) supports, using the enable -f file name syntax, loading built-in functions as external dynamically loaded modules. 目前还不清楚你修改了什么,但无论如何, bash (至少是IIRC引入概念和zsh ksh93 )支持,使用enable -f file name语法,将内置函数加载为外部动态加载模块。

These modules being plain files can certainly be distributed independently, as long as you make sure they are compatible with the target version/architecture. 这些纯文件模块当然可以独立分发,只要您确保它们与目标版本/体系结构兼容即可。 This was already true 5 years ago when you asked this question. 5年前,当你问这个问题时,这已经是真的了。

One issue in your case is there seems to be no documented way to overload a internal built-in like cd by a dynamically loaded one while keeping the ability to access the former. 在你的情况下的一个问题是似乎没有记录的方法来重载内部内置的像cd一样动态加载的同时保持访问前者的能力。

A simple workaround would be to implement your customized cd with a different name, say mycd, like this: 一个简单的解决方法是使用不同的名称实现您的自定义CD,比如mycd,如下所示:

int mycd_builtin(list)
WORD_LIST *list;
{
  int rv;
  rv=cd_builtin(list);
  if(rv == EXECUTION_SUCCESS) {
    char wd[PATH_MAX+1];
    getcwd(wd,sizeof(wd));
    // do your custom stuff knowing the new working directory
    ...
  }
  return (rv);
}

then to use an alias, or better, a shell function for your customized version to be used instead of the regular one: 然后使用别名或更好的shell函数来定制您的自定义版本而不是常规版本:

cd() {
  mycd "$@"
}

As long as your customization doesn't affect the behavior of the standard command and thus doesn't risk breaking scripts using it, there is nothing wrong in your approach. 只要您的自定义不会影响标准命令的行为,因此不会冒使用它破坏脚本的风险,您的方法就没有错。

Changing the built-in cd is a support nightmare for any admin and unwelcome to foreign users. 更改内置CD是任何管理员的支持噩梦,不受外国用户欢迎。 What is wrong with naming it 'smart-cd' and letting the USER decide if they want the functionality by including it in their .bashrc or .profile? 命名为'smart-cd'并让USER通过将其包含在.bashrc或.profile中来决定是否需要该功能有什么问题? Then they can setup things however they want. 然后他们可以设置他们想要的东西。

Also, using how long you've been in a directory is a pretty poor indication of preference. 此外,使用您在目录中的时间长度是一个非常糟糕的偏好指示。 How would you distinguish between idling (a forgotten shell hanging in /tmp overnight), long-running scripts (nightly cron jobs), and actual activity. 你如何区分空转(一夜之间挂在/ tmp中的被遗忘的shell),长时间运行的脚本(夜间cron作业)和实际活动。

There are a multitude of other methods for creating shortcuts to favorite directories: aliases, softlinks, $VARIABLES, scripts. 有许多其他方法可用于创建喜爱目录的快捷方式:别名,软链接,$ VARIABLES,脚本。 It is arrogant of you to assume that your usage patterns will be welcomed by other users of your system. 假设您的使用模式将受到系统其他用户的欢迎,这是傲慢的。

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

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