Last active
May 21, 2019 11:49
-
-
Save alanwei43/327bae8af08697da4d0cb79d550cad69 to your computer and use it in GitHub Desktop.
Push to gist 读取文件并发布到gist上
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 0. Your file name | |
FNAME="$2" | |
# 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": "description", | |
"public": false, | |
"files": { | |
"${FNAME}": { | |
"content": "${CONTENT}" | |
} | |
} | |
} | |
EOF | |
# 3. Use curl to send a POST request | |
curl -X POST -u "$1" -d "${DESC}" "https://api.github.com/gists" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use: