2013-08-07 34 views
2

下面的代碼的問題是,我似乎無法讓數組識別何時文本中有空格。我想加入數組中的''值可以解決這個問題,但我錯了。我還沒有找到關於如何讓數組識別bash腳本中的空格的運氣。Bash Script Leet Text Convertor:如何獲取數組來識別空格

#!/bin/bash 
if [ "$1" == "-e" ]; then # if the cli argument is -e 
    OPT="encrypt"; # set the option to encrypt 
elif [ "$1" == "-d" ]; then # if the cli argument is -d 
    OPT="decrypt"; # set the option to decrypt 
else # else show the proper usage 
    echo "Usage - Encrypt text: ./l33t.sh -e text"; 
    echo "Usage - Decrypt text: ./l33t.sh -d text"; 
    exit; 
fi 
#creating an array for leet text and 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 a string to $OPT: "; # asking for user input 
read INPUT; # grab the input 
while read letter; # for each character in the input (check the grep near done) 
do 
    for i in {0..37} # for each item in the array 
    do 
      if [ "$OPT" == "encrypt" ]; then # if the option is set to encrypt 
        FIND=${ENG[$i]}; # the array to look through is the plain array 
      elif [ "$OPT" == "decrypt" ]; then # else the array to look through is the leet text array 
        FIND=${LEET[$i]}; 
      fi 

      if [ "$OPT" == "encrypt" ]; then # if the option is set to encrypt 
        if [ "$FIND" == "$letter" ]; then # if our character is in the plain array 
          ENCRYPTED+=${LEET[$i]}; # Add to Encrypted that values leet transformation 
        fi 
      elif [ "$OPT" == "decrypt" ]; then # else do the same thing except with oposite arrays 
        if [ "$FIND" == "$letter" ]; then 
          ENCRYPTED+=${ENG[$i]}; 
        fi 
      fi 
    done 
done < <(grep -o . <<< $INPUT) 
echo $ENCRYPTED; # echo the result 

回答

0

我不知道你做這部分的比較,但我想引用你的變量ENCRYPTED也將是有益的:

echo "$ENCRYPTED" 

我還沒有看到代碼的任何部分,其中一個可能的實例與空間的比較可能是一個問題。

地址:您只需要37元,因此該循環也應該只有從0到36:

for i in {0..36} # for each item in the array 

也許在這個時候,讓你添加一個空字符。

0

考慮使用tr(man tr瞭解更多細節,自然)。

3

變化

while read letter 

while IFS= read -r letter 

否則,讀命令忽略前後空白。當您嘗試閱讀空間時,這是至關重要的。

+0

謝謝,這就是我需要的!小東西,小東西。 – Singularity