【发布时间】:2019-08-29 14:28:14
【问题描述】:
我正在尝试自动创建 github 存储库。以下是我的代码:
pushtogithub() {
git init
git config user.name "user name"
git config user.email "user email"
git add .
git commit -m "Initial commit"
content=$(curl -s -u githubusername:githubaccesstoken https://api.github.com/user/repos -d '{"name":"$1", "private":true}')
repo=$(jq -r '.ssh_url' <<< "${content}" )
echo $repo created
git remote add origin git@github.com:githubusername/$repo.git
git push -u origin master
}
我的 github 存储库总是使用 -1 名称创建。
我在这里做错了什么?
【问题讨论】:
-
在 curl 的请求数据中,
$1用单引号括起来,不会被展开。例如,您可以将该部分更改为-d "{\"name\":\"$1\", \"private\":true}" -
这解决了使用
-1名称创建repo。但我仍然没有在repo中得到正确的值。我将文字值echo jq -r...放入其中。 -
echo应该从jq子shell 中删除,我假设您已经将它放在那里调试您的命令,但在发布之前忘记将其删除。 -
如果您要使用 jq,请正确使用它——不仅要解析 JSON,还要创建 JSON。跨度>
-
@Gulshan,...如果在不使用语法感知编码器的情况下将内容插入 JSON 中“有效”,那么您就没有使用足够有趣的数据进行测试。 :)
标签: shell curl jq github-api