zsh 函数由于报价 mar 而不起作用

zsh function doesn't work due to quotation mar

提问人:Roberto D. Maggi 提问时间:11/15/2023 更新时间:11/15/2023 访问量:20

问:

今天我在我的 .zshrc 上编辑了这个在互联网上找到的功能。 关键是,如果在 shell 中输入每一行,它就可以工作并在 gitlab 中创建 repo。 将其作为函数调用,它返回:

gitdir:5: 搜索结果:https://gitlab.mine.my/api/v4/projects?private_token=HI-U-ALL

功能是这样的:

   function gitdir() {
      repo=$1           
      token=HI-U-ALL            
      GITLAB=gitlab.mine.my            
      test -z $repo && echo "Repo name required." 1>&2 && exit 1            
      curl -H "Content-Type:application/json" https://$GITLAB/api/v4/projects?private_token=$token \
      -d "{ \name\: \$repo\ }"    }

问题出在curl调用的“”上,但我无法解决问题,有很多解决方法 但绝对不是作为原生功能优雅。 谁能提出一个可能的解决方案? 提前致谢。 抢

我已经尝试更改引号,但 curl 确实粘在引号上。 我想简单地自动化该过程

壳牌 gitlab zsh

评论

0赞 Armali 11/15/2023
我不相信如果你 zsh 中输入单行 curl -H “Content-Type:application/json” https://$GITLAB/api/v4/projects?private_token=$token \ -d “{ \name\: \$repo\ }” 它就可以工作了

答:

0赞 user1934428 11/15/2023 #1

问号是 shell 的 glob 字符,zsh 尝试在 https 参数中生成文件名。最简单的解决方案是将参数括在双引号之间。或者,您可以转义问号:

... "https://$GITLAB/api/v4/projects?private_token=$token" ...

... https://$GITLAB/api/v4/projects\?private_token=$token ...