Bash Script Leet Text Convertor: How to get array to recognize spaces -
the issue code below can not seem array recognize when text has spaces in or not. figured adding ' ' value in array handle this, wrong. haven't found luck searches how array recognize spaces in bash scripting.
#!/bin/bash if [ "$1" == "-e" ]; # if cli argument -e opt="encrypt"; # set option encrypt elif [ "$1" == "-d" ]; # if cli argument -d opt="decrypt"; # set option decrypt else # else show proper usage echo "usage - encrypt text: ./l33t.sh -e text"; echo "usage - decrypt text: ./l33t.sh -d text"; exit; fi #creating array leet text , plain text declare -a leet=('ɐ' 'ß' '©' 'Ð' '€' 'ƒ' '&' '#' 'i' '¿' 'x' '£' 'm' '?' 'ø' 'p' 'o' 'Я' '§' '†' 'µ' '^' 'w' '×' '¥' 'z' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0' ' '); declare -a eng=('a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0' ' '); echo -n "please enter string $opt: "; # asking user input read input; # grab input while read letter; # each character in input (check grep near done) in {0..37} # each item in array if [ "$opt" == "encrypt" ]; # if option set encrypt find=${eng[$i]}; # array through plain array elif [ "$opt" == "decrypt" ]; # else array through leet text array find=${leet[$i]}; fi if [ "$opt" == "encrypt" ]; # if option set encrypt if [ "$find" == "$letter" ]; # if our character in plain array encrypted+=${leet[$i]}; # add encrypted values leet transformation fi elif [ "$opt" == "decrypt" ]; # else same thing except oposite arrays if [ "$find" == "$letter" ]; encrypted+=${eng[$i]}; fi fi done done < <(grep -o . <<< $input) echo $encrypted; # echo result
change
while read letter
to
while ifs= read -r letter
otherwise, read command ignores leading , trailing whitespace. that's kind of crucial when you're trying read space.
Comments
Post a Comment