使用cURL bash脚本内发送JSON [英] Using cURL to send JSON within a BASH script

查看:204
本文介绍了使用cURL bash脚本内发送JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这里是我想要做的。我试图写在bash快速构建脚本,将检查出从GitHub的私人资料库的远程服务器上。要做到这一点放手成为可能,我想生成本地RSA密钥在远程服务器上设置并添加公钥作为重点部署为特定存储库。我知道如何使用GitHub的API来做到这一点,但我无法使用bash建设JSON的有效载荷。

Alright, here's what I'm trying to do. I'm attempting to write a quick build script in bash that will check out a private repository from GitHub on a remote server. To do this a "hands off" as possible, I want to generate a local RSA key set on the remote server and add the public key as a Deploy Key for that particular repository. I know how to do this using GitHub's API, but I'm having trouble building the JSON payload using Bash.

到目前为止,我这一具体过程包含如下:

So far, I have this particular process included below:

#!/bin/bash

ssh-keygen -t rsa -N '' -f ~/.ssh/keyname -q
public_key=`cat ~/.ssh/keyname.pub`

curl -u 'username:password' -d '{"title":"Test Deploy Key", "key":"'$public_key'"}' -i https://api.github.com/repos/username/repository/keys

这只是不能正常建立有效载荷。我不是专家,当谈到在Bash中字符串处理,所以我可能会严重一些帮助。谢谢!

It's just not properly building the payload. I'm not an expert when it comes to string manipulation in Bash, so I could seriously use some assistance. Thanks!

推荐答案

这不是一定的,但它可以帮助引述在您使用PUBLIC_KEY,即

It's not certain, but it may help to quote where you use public_key, i.e.

curl -u 'username:password' \
   -d '{"title":"Test Deploy Key", "key":"'"$public_key"'"}' \
   -i https://api.github.com/repos/username/repository/keys

否则,如果您使用shell的调试选项它会更容易调试设置-vx 靠近你的bash脚本的顶部。

Otherwise it will be much easier to debug if you use the shell's debugging options set -vx near the top of your bash script.

您会看到code(或块(每行,同时,等),因为它是在你的文件,然后你看到扩大到他们的价值观变量code的每一行。

You'll see each line of code (or block (for, while, etc) as it is in your file. Then you see each line of code with the variables expanded to their values.

如果你仍然坚持,编辑您的帖子显示变量的扩展值问题行脚本。你有什么乍看上去合理的。

If you're still stuck, edit your post to show the expanded values of variables for the problem line in your script. What you have looks reasonable at first glance.

这篇关于使用cURL bash脚本内发送JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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