Created
November 26, 2024 21:54
-
-
Save goeko/b9b5dc6c59098d712f9f3fbd1a8b438d to your computer and use it in GitHub Desktop.
base64copy from file or -t=text and output text or -o=html
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 | |
base64copy() { | |
local input="" | |
local output="text" | |
local mimetype="text/plain" | |
local base64data | |
local is_text_input=0 | |
while [[ "$#" -gt 0 ]]; do | |
case $1 in | |
-o|--output) | |
case "$2" in | |
text|html) | |
output="$2" | |
shift | |
;; | |
*) | |
echo "Fehler: Ungültiger Ausgabe-Typ. Verwenden Sie 'text' oder 'html'." | |
return 1 | |
;; | |
esac | |
;; | |
-t|--text) | |
input="$2" | |
is_text_input=1 | |
mimetype="text/plain" | |
shift | |
;; | |
-h|--help) | |
echo "Verwendung: base64copy [Optionen] <Datei> oder --text 'string'" | |
echo "Optionen:" | |
echo " -o, --output=text|html Ausgabeformat: 'text' (Standard) oder 'html' für HTML img Tag" | |
echo " -t, --text=\"string\" Direkte Text-Eingabe zum Kodieren" | |
echo " -h, --help Hilfe anzeigen und beenden" | |
echo "" | |
return 0 | |
;; | |
*) | |
if [[ $is_text_input -eq 0 ]]; then | |
input="$1" | |
fi | |
;; | |
esac | |
shift | |
done | |
if [[ $is_text_input -eq 0 && ! -f "$input" ]]; then | |
echo "Datei existiert nicht. Verwenden Sie --help für Informationen zur Nutzung." | |
return 1 | |
fi | |
if [[ $is_text_input -eq 0 ]]; then | |
mimetype=$(file --mime-type -b "$input") | |
base64data=$(base64 < "$input") | |
else | |
base64data=$(echo -n "$input" | base64) | |
fi | |
if [[ "$output" == "html" ]]; then | |
echo "<img src='data:$mimetype;base64,$base64data'/>" | pbcopy | |
echo "Base64-Daten mit MIME-Typ in die Zwischenablage als HTML img Tag kopiert!" | |
else | |
echo "data:$mimetype;base64,$base64data" | pbcopy | |
echo "Base64-Daten mit MIME-Typ in die Zwischenablage als einfacher Text kopiert!" | |
fi | |
} | |
# Nur erforderlich, wenn das Skript direkt ausgeführt wird | |
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then | |
base64copy "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment