Sunday, January 3, 2016

Word dılɟ! in Bash

#!/bin/bash

### Copyrighted +A.M.Danischewski (c)2015+  
### You may reuse this software without limit provided this notice 
### remain in tact. 
###
### File: wordflip.bsh  
###  Description: This script turns text upside down! =) 
###  It can accept piped data and redirects,
###     $> wordflip.bsh <<< "some txt" 
###     $> echo -en "flip\nme\n" | wordflip.bsh
###  _-=  Enjoy!  _-=  

declare word=""
declare -A wordlist 
wordlist["a"]="\u0250" 
wordlist["b"]="q" 
wordlist["c"]="\u0254" 
wordlist["d"]="p" 
wordlist["e"]="\u01dd" 
wordlist["f"]="\u025f" 
wordlist["g"]="\u0183"
wordlist["h"]="\u0265"
wordlist["i"]="\u0131"
wordlist["j"]="\u027e"
wordlist["k"]="\u029e"
wordlist["l"]="l"
wordlist["m"]="\u026f"
wordlist["n"]="u"
wordlist["o"]="o"
wordlist["p"]="d"
wordlist["q"]="b"
wordlist["r"]="\u0279"
wordlist["s"]="s"
wordlist["t"]="\u0287"
wordlist["u"]="n"
wordlist["t"]="\u0287"
wordlist["v"]="\u028c"
wordlist["w"]="\u028d"
wordlist["x"]="x"
wordlist["y"]="\u028e"
wordlist["z"]="z"
wordlist["]"]="["
wordlist["["]="]"
wordlist[","]="'"
wordlist["'"]=","
wordlist["("]=")"
wordlist[")"]="("
wordlist["{"]="}"
wordlist["}"]="{"
wordlist["?"]="\u00bf"
wordlist["!"]="\u00a1"
wordlist["\""]=","
wordlist[">"]="<"
wordlist["<"]=">"
wordlist["+"]="+"
wordlist["_"]="\u203e"
wordlist[";"]="\u061b"

 ## Handle piped input and here-string redirects. 
if readlink /proc/$$/fd/0 | grep -Eq "^pipe:|deleted"; then
 word=""
 while IFS= read -r PIPED_INPUT || { DONE=1; }; do 
  PIPED_INPUT=$(sed 's#\\#\\\\#g' <<< "${PIPED_INPUT}")
  word="${word}${PIPED_INPUT}+"
  ((${DONE})) && break; 
 done
else 
 word="${1}" 
fi 

revword=$(echo "$word"|rev) 
for i in $(eval echo "{1..${#revword}}"); do 
 printf "${wordlist[${revword:$((${i}-1)):1}]}"
done | sed 's/^+*//' | tr '+' '\n' 
echo 
exit 0

No comments:

Post a Comment