【问题标题】:Power-shell /Windows equivalent of bash script [duplicate]Power-shell /Windows相当于bash脚本[重复]
【发布时间】:2020-02-11 09:57:10
【问题描述】:

我正在尝试通过 Power-Shell CLI 在Windows 10 中执行以下简单命令。我该怎么做?

ID=test001 ./initializeID.sh

initializeID.sh 在哪里:

#!/bin/bash
echo "Initializing for ID: $ID"
DPPROG="{\"title\":\"Test \", \"Id\": \"$ID\"}"
curl -H "Content-type: application/json" -d "$DPPROG" http://localhost:8080/StorageApi/id

目前如果我运行它,它会显示这个错误:

ID=test001 : The term 'ID=test001' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ ID=test001 ./initializeID.sh
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (ID=test001:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

【问题讨论】:

  • 请记住,您不能只在 Powershell 中运行 .sh 脚​​本。 shebang 在 Windows 中也不是一个东西。
  • 详情请看链接帖子和this answer
  • 好点,@BendertheGreatest。 Tina J,您使用的语法仅适用于类似 POSIX 的 shell,例如 Bash。在 PowerShell 中,ID=test001 被解释为 命令名称,它解释了错误消息。如果您还需要翻译脚本本身:请查看 Invoke-RestMethodInvoke-WebRequest 以了解类似 curl 的功能。
  • 即使在 linux 中我也遇到了运行问题。 ./initializeID.sh 需要权限,没有bash 就无法运行。但是我知道如何通过ID=test001 那么。

标签: bash powershell cmd windows-10 sh


【解决方案1】:

没有办法直接做到这一点。您可以设置变量,然后像这样运行命令:

$env:VARIABLE = 'somevalue'; some-command-or-script

但这会将环境变量保留在调用some-command-or-script 的当前powershell 会话中。您需要在some-command-or-script 退出后运行Remove-Item ENV:VARIABLE,或者在上述行之前存储原始值,并在命令完成后恢复它。

【讨论】:

  • 即使在 linux 中我也遇到了运行问题。 ./initializeID.sh 需要权限,没有bash 就无法运行。但是我知道如何通过ID=test001。有什么解决方法吗?
猜你喜欢
  • 2014-01-19
  • 2011-07-19
  • 2019-10-31
  • 1970-01-01
  • 2012-11-25
  • 2016-11-29
  • 2010-11-05
  • 2021-08-22
  • 1970-01-01
相关资源
最近更新 更多