Last active
May 26, 2024 16:07
-
-
Save sagax/54300d2c835456ca5f847f8eac228879 to your computer and use it in GitHub Desktop.
paste bashscripts / sharefile scripts
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
#!/bin/bash | |
# take API token https://www.toptal.com/developers/hastebin/documentation | |
TOKENFILE="$HOME/.hastebin" | |
# example #1 | |
# echo "hello world!" | hastebin | |
# | |
# example #2 | |
# hastebin file_for_paste | |
# | |
# example #3 | |
# echo "hello world!" | hastebin file_for_paste | |
function get_token() { | |
local token | |
token="$(cat "$TOKENFILE")" | |
printf "%s" "$token" | |
} | |
function get_url() { | |
local uri | |
uri="$1" | |
printf "https://hastebin.com/share/%s" "$uri" | |
} | |
function post_data() { | |
local data | |
local result | |
local uri | |
local url | |
data="$1" | |
result="$(curl \ | |
--silent \ | |
--request POST \ | |
--url https://hastebin.com/documents \ | |
--header "Content-Type: text/plain" \ | |
--header "Authorization: Bearer $(get_token)" \ | |
--data "$data")" | |
if [[ $? -eq 0 ]] | |
then | |
uri="$(printf "%b" "$result" | jq -r .key)" | |
url="$(get_url "$uri")" | |
printf "%s" "$url" | xclip -sel cli | |
printf "%s\n" "$url" | |
else | |
printf "sorry, but fail\n" | |
fi | |
} | |
function read_and_post_pipe() { | |
local pipe_data | |
read -r -d "" pipe_data | |
post_data "$pipe_data" | |
} | |
function read_and_post_file() { | |
local file_path | |
local file_data | |
file_path="$1" | |
file_data="" | |
while IFS= read -r line | |
do | |
file_data+="$line"$'\n' | |
done < "$file_path" | |
post_data "$file_data" | |
} | |
function is_has_pipe() { | |
if [[ -p /dev/stdin ]] | |
then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function is_has_file() { | |
if [[ -f "$1" ]] | |
then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function is_has_args() { | |
if [[ ${#@} -ge 2 ]] | |
then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
if is_has_pipe "$@" | |
then | |
read_and_post_pipe | |
fi | |
if is_has_file "$@" | |
then | |
read_and_post_file "$1" | |
fi | |
if is_has_args "$@" | |
then | |
printf "" | |
fi |
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
#!/bin/bash | |
# example #1 | |
# echo "hello world!" | termbin | |
# | |
# example #2 | |
# termbin file_for_paste | |
# | |
# example #3 | |
# echo "hello world!" | termbin file_for_paste | |
function post_data() { | |
local data | |
local url | |
data="$1" | |
url="$(printf "%s\n" "$data" | netcat termbin.com 9999 | tr -d '\000')" | |
printf "%s" "$url" | xclip -sel cli | |
printf "%s\n" "$url" | |
} | |
function read_and_post_pipe() { | |
local pipe_data | |
read -r -d "" pipe_data | |
post_data "$pipe_data" | |
} | |
function read_and_post_file() { | |
local file_path | |
local file_data | |
file_path="$1" | |
file_data="" | |
while IFS= read -r line | |
do | |
file_data+="$line"$'\n' | |
done < "$file_path" | |
post_data "$file_data" | |
} | |
function is_has_pipe() { | |
if [[ -p /dev/stdin ]] | |
then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function is_has_file() { | |
if [[ -f "$1" ]] | |
then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
function is_has_args() { | |
if [[ ${#@} -ge 2 ]] | |
then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
if is_has_pipe "$@" | |
then | |
read_and_post_pipe | |
fi | |
if is_has_file "$@" | |
then | |
read_and_post_file "$1" | |
fi | |
if is_has_args "$@" | |
then | |
printf "" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment