Skip to content

Instantly share code, notes, and snippets.

@alanwei43
Last active May 21, 2019 11:49
Show Gist options
  • Save alanwei43/327bae8af08697da4d0cb79d550cad69 to your computer and use it in GitHub Desktop.
Save alanwei43/327bae8af08697da4d0cb79d550cad69 to your computer and use it in GitHub Desktop.
Push to gist 读取文件并发布到gist上
# 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"
@alanwei43
Copy link
Author

alanwei43 commented May 21, 2019

Use:

wget https://dwz.cn/9a1GKeXO  # download script to local
chmod +x push-to-gist.sh # add execute policy
./push-to-gist.sh your_github_account your_file # create gist 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment