如何创建在执行实际命令之前更改环境变量的别名命令

发布于 2024-10-03 11:26:42 字数 937 浏览 1 评论 0原文

首先,我很抱歉,这是一个“给我代码”的问题。不过,我保证会从您的回答中学习!这意味着如果您认为有必要,请随意使用苏格拉底方法


我目前正在使用 Grails 和它的工作方式是它提供一个命令行 shell 来执行各种任务。

这一切都很好,但是版本之间的切换有点麻烦,因为它全部由一个名为 GRAILS_HOME 的环境变量控制,该变量被设置为指向诸如 C 之类的目录:\grails\grails-1.2.3\

现在,我希望能够做到这一点,而不是做这样的事情:

grails create-app MyWebApp
grails list-plugins -repository=myPluginRepo
grails install-plugin myPlugin

然后注意到我使用了错误的版本并且我必须返回,我宁愿

g135 create-app MyWebApp
g135 list-plugins -repository=myPluginRepo
g135 install-plugin myPlugin

这样做会立即告诉我我我正在使用 Grails 1.3.5。

我在创建这样的 psh 脚本时遇到的问题是我完全不擅长创建脚本。我确实尝试这样做,但在 psh 本身发出大约五个神秘错误消息后,我决定寻求建议。

奖励点:如果我可以参数化别名调用来指定版本(类似g[1.3.5]其他参数),然后附加环境变量值的数字, 这对我来说是最方便的解决方案。

First, I'm sorry but this is kind of a "give me teh codez" question. I promise to learn from your answers, though! That means that feel free to use the Socratic method if you feel it's necessary.


I'm currently developing (both maintenance and building new ones) multiple sites using Grails and the way it works is that it provides a commandline shell for executing various tasks.

This is all fine and well, but switching between versions is a bit of a nuisance, since it's all controlled by a single environment variable called GRAILS_HOME which is set to point to a directory such as C:\grails\grails-1.2.3\.

Now, what I would like to be able to do that instead of doing something like this:

grails create-app MyWebApp
grails list-plugins -repository=myPluginRepo
grails install-plugin myPlugin

and then noticing that I'm using the wrong version and that I have to go back, I'd rather do

g135 create-app MyWebApp
g135 list-plugins -repository=myPluginRepo
g135 install-plugin myPlugin

which would immediately tell me that I'm using Grails 1.3.5.

The problem I have in creating a psh script like this is that I absolutely suck at creating scripts. I did try doing this but after about five mysterious error messages from psh itself I decided to just ask for advice.

Bonus points: If I can parameterize the alias call to specify the version (something like g[1.3.5] other params) and then append the number to the environment variable value,
that'd be the most handy solution for me.

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

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

发布评论

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

评论(1

流云如水 2024-10-10 11:26:42

这应该可以做到:

Function PSGrails($ver)
    {
    $env:GRAILS_HOME='c:\grails\grails-{0}\' -f $ver
    grails $args
    }

它只是一个简短的函数,用于设置变量,然后如上所述执行 grails。

通过在函数名称后直接指定版本号来运行它:

psgrails 1.3.5 create-app MyWebApp     
psgrails 1.3.5 list-plugins -repository=myPluginRepo     
psgrails 1.3.5 install-plugin myPlugin  

This should do it:

Function PSGrails($ver)
    {
    $env:GRAILS_HOME='c:\grails\grails-{0}\' -f $ver
    grails $args
    }

It's just a short function that sets the variable and then executes grails as above.

Run it by specifying the version number directly after the function name:

psgrails 1.3.5 create-app MyWebApp     
psgrails 1.3.5 list-plugins -repository=myPluginRepo     
psgrails 1.3.5 install-plugin myPlugin  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文