代码之家  ›  专栏  ›  技术社区  ›  net068

有没有办法在zsh shell(Mac/Unix)上从.cmd文件执行命令

  •  0
  • net068  · 技术社区  · 1 年前

    我在我试图执行的文件的目录中,我知道我正在使用正确的文本来执行位于正确位置的文件。该文件名为“modify search.cmd”,我正在执行:

    ./modify-search.cmd
    

    但是,因为zsh只识别“echo”,所以我的“.cmd”文件中的其他命令无法识别,因此我的zsh shell状态为“找不到命令”。这是我的代码 modify-search.cmd 文件,减去用户特定的域/URL和管理密钥

    @echo off
    
    rem Set values for your Search service
    set url=YOUR_SEARCH_URL
    set admin_key=YOUR_ADMIN_KEY
    
    echo -----
    echo Updating the skillset...
    call curl -X PUT %url%/skillsets/margies-skillset?api-version=2020-06-30 -H "Content-Type: application/json" -H "api-key: %admin_key%" -d @skillset.json
    
    rem wait
    timeout /t 2 /nobreak
    
    echo -----
    echo Updating the index...
    call curl -X PUT %url%/indexes/margies-index?api-version=2020-06-30 -H "Content-Type: application/json" -H "api-key: %admin_key%" -d @index.json
    
    rem wait
    timeout /t 2 /nobreak
    
    echo -----
    echo Updating the indexer...
    call curl -X PUT %url%/indexers/margies-indexer?api-version=2020-06-30 -H "Content-Type: application/json" -H "api-key: %admin_key%" -d @indexer.json
    
    echo -----
    echo Resetting the indexer
    call curl -X POST %url%/indexers/margies-indexer/reset?api-version=2020-06-30 -H "Content-Type: application/json" -H "Content-Length: 0" -H "api-key: %admin_key%" 
    
    rem wait
    timeout /t 5 /nobreak
    
    echo -----
    echo Rerunning the indexer
    call curl -X POST %url%/indexers/margies-indexer/run?api-version=2020-06-30 -H "Content-Type: application/json" -H "Content-Length: 0" -H "api-key: %admin_key%" 
    
    
    

    以下是我执行命令时在终端中得到的输出:

    ./modify-search.cmd: line 1: @echo: command not found
    ./modify-search.cmd: line 3: rem: command not found
    -----
    Updating the skillset...
    ./modify-search.cmd: line 9: call: command not found
    ./modify-search.cmd: line 11: rem: command not found
    ./modify-search.cmd: line 12: timeout: command not found
    -----
    Updating the index...
    ./modify-search.cmd: line 16: call: command not found
    ./modify-search.cmd: line 18: rem: command not found
    ./modify-search.cmd: line 19: timeout: command not found
    -----
    Updating the indexer...
    ./modify-search.cmd: line 23: call: command not found
    -----
    Resetting the indexer
    ./modify-search.cmd: line 27: call: command not found
    ./modify-search.cmd: line 29: rem: command not found
    ./modify-search.cmd: line 30: timeout: command not found
    -----
    Rerunning the indexer
    ./modify-search.cmd: line 34: call: command not found
    

    这对我来说很有意义,因为 echo 是zsh中的内置命令,因此不会执行@echo,而是执行通知文本消息。如何执行其他“.cmd”命令?

    我正在为Microsoft Azure做培训实验室,所以我使用Azure CLI,并通过Visual Studio代码完成所有这些工作。此外,我在Visual Studio代码中安装了一个Powershell扩展,但也无法在那里工作。激活Powershell(通过zsh)后,我尝试执行:

    command ./modify-search.cmd
    

    该命令给我的输出与我在zsh上的可执行命令相同——“echo”语句有效,其他所有命令都返回“找不到命令”。

    我完成实验室练习的低效解决方法是直接在“.cmd”代码中插入我的URL和管理密钥,无论我的文件在哪里:

    %url%%admin_key%

    并直接通过我的zsh shell(插入我的Azure CLI)执行调用,这在我上次遇到这个问题时起到了作用。

    这是我第二次遇到这个问题,所以我想我会寻求帮助,这样我就可以有一个更有效的解决方案。请帮忙,有人吗?

    编辑:更新,我的变通办法这次不起作用了!上次我执行Azure CLI命令,并在指定的部分之间替换身份验证数据以连接到我的帐户。这一次,我执行POST和PUT语句,但它们的工作方式不同。我得到以下输出:

    zsh: no matches found: %url%/skillsets/margies-skillset?api-version=2020-06-30
    

    第2版:好吧,我尝试使用VS代码调用REST API,您中的一些人可能已经从 modify-search.cmd 。我正在做一些与之相关的研究,这可能会让我找到解决方案。到目前为止,我已经:

    1. 为我的IDE安装了REST客户端扩展,VS代码

    2. 制作了一个不带扩展名的单独的“.rest”文件(这样文本文件就可以在此后将代码更改为“.erest”文件)。

    我试图发送请求,但收到了 getaddrinfo ENOTFOUND echo 错误进步

    0 回复  |  直到 1 年前
        1
  •  2
  •   mklement0    1 年前

    如评论中所述 .cmd .bat )文件就是所谓的 批处理文件 使用传统语言的 窗户 shell(又称命令行解释器), cmd.exe .

    因此,您无法在类Unix平台(如macOS)上运行批处理文件。

    然而,将批处理文件翻译成与POSIX兼容的shell脚本并不困难 zsh shell理解:

    • 去除 @echo off

    • 代替 rem 具有 #

    • 去除 set 来自变量定义,例如 set url=YOUR_SEARCH_URL

    • 带前缀的引用变量 $ 而不是把它们封闭起来 %...% ; 例如,使用 $url 而不是 %url%

    • 去除 call 在调用诸如 curl

    • 代替 timeout /t 5 /nobreak 具有 sleep 5 (以及类似地用于其他秒值)。

    中基本shell语言特征的并置 命令提示符 ,POSIX兼容的shell和PowerShell,请参阅 this answer .


    正如评论中所指出的,考虑到。NET(Core)版本的PowerShell, PowerShell (Core) 7 ,是跨平台的,也在类Unix平台上运行。

    也就是说,如果您在PowerShell中编写代码,并将其放入 .ps1 文件,它将在所有支持的平台上运行(尽管在特定情况下仍必须尊重平台差异,例如不使用 .exe Unix和特定于平台的环境变量上的实用程序调用中的扩展)

    推荐文章