-
-
Save s-leroux/7cb7424d33ba3753e907cc2553bcd1ba to your computer and use it in GitHub Desktop.
| # 0. Your file name | |
| FNAME=some.file | |
| # 1. Somehow sanitize the file content | |
| # Remove \r (from Windows end-of-lines), | |
| # Replace tabs by \t | |
| # Replace " by \" | |
| # Replace EOL by \n | |
| CONTENT=$(sed -e 's/\r//' -e's/\t/\\t/g' -e 's/"/\\"/g' "${FNAME}" | awk '{ printf($0 "\\n") }') | |
| # 2. Build the JSON request | |
| read -r -d '' DESC <<EOF | |
| { | |
| "description": "some description", | |
| "public": true, | |
| "files": { | |
| "${FNAME}": { | |
| "content": "${CONTENT}" | |
| } | |
| } | |
| } | |
| EOF | |
| # 3. Use curl to send a POST request | |
| # ANONYMOUS GIST : | |
| # curl -X POST -d "${DESC}" "https://api.github.com/gists" | |
| # REGISTERED USER | |
| curl -u "${GITHUB_USERNAME}" -X POST -d "${DESC}" "https://api.github.com/gists" |
I don't have enough stackoverflow reputation to add a comment to the main conversation thread, but the reason why this breaks if the file contains a percent symbol (%) is because awk then attempts to treat it as a "Format-Control Letter" - please see https://www.gnu.org/software/gawk/manual/html_node/Control-Letters.html HTH, Jaime
And it turns out that the solution is just to "double-escape" the percent symbols in the input, so line 9:
CONTENT=$(sed -e 's/\r//' -e's/\t/\t/g' -e 's/"/\"/g' "${FNAME}" | awk '{ printf($0 "\n") }')
becomes:
CONTENT=$(sed -e 's/\r//' -e's/\t/\t/g' -e 's/"/\"/g' "${FNAME}" | awk '{gsub("%","%%",$0);print $0}' | awk '{ printf($0 "\n") }')
:-)
This code deserves a repo: https://github.com/ceremcem/create-gist (I'll hand it over to @s-leroux on request)
posted on http://stackoverflow.com/a/33354920/1069083