如何传递参数给.SH脚本在Windows下? [英] How to pass parameters to an .sh script under Windows?

查看:656
本文介绍了如何传递参数给.SH脚本在Windows下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows下执行.SH脚本。我安装的Git,这让我执行.SH文件。不过,我似乎无法各地传递任何参数,而不prefixing用嘘的执行:

I'm trying to execute .sh scripts under Windows. I installed Git, which allowed me to execute .sh files. However, I can't seem to pass any parameters around without prefixing the execution with "sh":

我的sh文件:

echo "Test" $1

如果我执行它:

> sh test.sh 123

我得到

测试123

不过,如果我只是执行它

However, if I just execute it as

> test.sh 123

我得到

测试

我知道这是可能的参数和无$ P $与sh的pfixing他们执行.SH脚本,因为通过配置的组合,我能够做我的旧电脑弄好了。任何猜测如何做到这一点?

I know it is possible to execute .sh scripts with parameters and without prefixing them with "sh", since through some combination of configurations I was able to do it on my old computer somehow. Any guesses as to how to accomplish this?

推荐答案

由于在评论中所解释的@HarryJohnston,第一步要解决这个问题搞清楚了 assoc命令 FTYPE 关联。 assoc命令将最有可能返回 .SH = sh_auto_file ,而在我的情况下, FTYPE 返回 sh_auto_file =C:\\ Program Files文件\\的Git \\混帐bash.exe--no-CD%L%* 。这个问题的一个部分是类似发生了什么事<一href=\"https://stackoverflow.com/questions/2640971/windows-is-not-passing-command-line-arguments-to-python-programs-executed-from-t\">in这个问题 - 视窗没有传递参数给sh文件。为了解决这个问题,在 sh_auto_file 协会需要与/有%1%* 来结束,因为它的一部分显然。然而,问题的另一部分是,(至少在Windows 10在我的情况),有可能是在有一个错误记录在注册表的多个地方。最后,我不得不手动更改注册表2-3项C:\\ Program Files文件(x86)的\\ Git的\\ BIN \\ sh.exe%1%* ,以便它最终工作。为了找到这些地方,搜索 .SH sh_auto_file 并相应地进行更换。

As explained by @HarryJohnston in the comments, the first step to solving this problem was figuring out the assoc and ftype associations. assoc will most likely return .sh=sh_auto_file, while in my case, ftype returned sh_auto_file="C:\Program Files\Git\git-bash.exe" --no-cd "%L" %*. One part of the issue was similar to what was happening in this issue - Windows was not passing parameters to the .sh file. To fix that, the sh_auto_file association needs to end with / have "%1" %* as a part of it apparently. However, another part of the problem was that (at least on Windows 10 in my case), there might be multiple places in the registry that have a wrong record. In the end I had to manually change 2-3 entries in registry to "C:\Program Files (x86)\Git\bin\sh.exe" "%1" %* in order for it to finally work. To find those places, search for .sh and sh_auto_file and replace accordingly.

奖励:为了使.SH文件执行,而无需编写.SH末,在环境变量添加到.SH PATHEXT

Bonus: To make .sh files execute without having to write ".sh" at the end, add .SH to pathext in environmental variables.

这篇关于如何传递参数给.SH脚本在Windows下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆