feat: Grace.sh

This commit is contained in:
lohhiiccc 2026-05-27 15:38:10 +02:00
parent 9078f0ff5d
commit d700ab3a02

38
src/Grace.sh Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env bash
set -o posix
src=$'#!/usr/bin/env bash\n\nset -o posix\n\nsrc=$\'@\'\n\nescape_quine() {\n\tlocal char="$1" ref="$2"\n\tcase "$char" in\n\t\t$\'\\\\\')\tprintf -v "$ref" \'%s\' \'\\\\\' ;;\n\t\t$\'\\\'\')\tprintf -v "$ref" \'%s\' "\\\'" ;;\n\t\t$\'\\t\')\tprintf -v "$ref" \'%s\' \'\\t\' ;;\n\t\t$\'\\n\')\tprintf -v "$ref" \'%s\' \'\\n\' ;;\n\t\t*)\t\tprintf -v "$ref" \'%s\' "$char" ;;\n\tesac\n}\n\nrun_quine() {\n# One comments must be present\n\toutput=\'\'\n\n\tfor (( i=0; i<${#src}; i++ )); do\n\t\tc="${src:i:1}"\n\n\t\tif [[ "$c" == $\'\\x40\' ]]; then\n\t\t\tfor (( j=0; j<${#src}; j++ )); do\n\t\t\t\tout=""\n\t\t\t\tescape_quine "${src:j:1}" out\n\t\t\t\toutput+="$out"\n\t\t\tdone\n\t\telse\n\t\t\toutput+="$c"\n\t\tfi\n\tdone\n\tprintf \'%s\' "$output" > "Grace_Kid.sh"\n}\n\nrun_quine\n'
escape_quine() {
local char="$1" ref="$2"
case "$char" in
$'\\') printf -v "$ref" '%s' '\\' ;;
$'\'') printf -v "$ref" '%s' "\'" ;;
$'\t') printf -v "$ref" '%s' '\t' ;;
$'\n') printf -v "$ref" '%s' '\n' ;;
*) printf -v "$ref" '%s' "$char" ;;
esac
}
run_quine() {
# One comments must be present
output=''
for (( i=0; i<${#src}; i++ )); do
c="${src:i:1}"
if [[ "$c" == $'\x40' ]]; then
for (( j=0; j<${#src}; j++ )); do
out=""
escape_quine "${src:j:1}" out
output+="$out"
done
else
output+="$c"
fi
done
printf '%s' "$output" > "Grace_Kid.sh"
}
run_quine