简体   繁体   English

bash中的反向字符串丢失

[英]Reverse string in bash missing

All, 所有,

I have a decrypt process which has a part that reverses an encoded (only as another ascii character) string. 我有一个解密过程,该过程的一部分会反转编码的字符串(仅作为另一个ascii字符)。

The string is... 字符串是...

vtr«¥ºc€a VTR«¥ºC一个€

And the reverse command I'm using is... 我正在使用的反向命令是...

enc_pass="vtr«­¥ºc€a"
reverse=""

len=${#enc_pass}
for (( i=$len-1; i>=0; i-- ))
do
      reverse="$reverse${enc_pass:$i:1}"
done

echo $reverse

But when I check the length, it comes out as 9 characters long rather than 10, and I've found out its the € character which is being ignored. 但是当我检查长度时,它显示为9个字符而不是10个字符,并且我发现它的€字符被忽略了。

Is there any way to cater for this? 有什么办法可以解决这个问题? I'm pretty lost on it, not sure where to go next. 我对此很迷茫,不确定下一步要去哪里。


It turns out the main issue was with my LANG on the new server. 原来的主要问题是我的LANG在新服务器上。 The old server had LANG="en_GB" and the new server LANG="en_GB.UTF-8" 旧服务器具有LANG =“ en_GB”,新服务器LANG =“ en_GB.UTF-8”

Changing the LANG back to en_GB resolved the reversing as UTF-8 ignored the character for some reason. 将LANG改回en_GB可以解决该问题,因为UTF-8出于某种原因忽略了该字符。

You can use rev utility: 您可以使用rev实用程序:

enc_pass="vtr«­¥ºc€a"
rev <<< "$enc_pass"
a€cº¥­«rtv
LC_ALL='en_US.UTF-8'
(your code)
echo "${#reverse}"
10

LC_ALL='C'
(your code)
echo "${#reverse}"
16

I think you must just have a localization/encoding problem in your environment. 我认为您必须在您的环境中遇到本地化/编码问题。 I suspect that would even effect rev , although it's a nonstandard utility so I can't say for sure. 我怀疑它甚至会影响rev ,尽管它是非标准实用程序,所以我不确定。 This works fine for me: 这对我来说很好:

rev() {
    local -x LC_ALL=en_US.UTF-8
    if [[ ! "$1" ]]; then
        echo
        return
    fi
    printf '%s' "${1: -1:1}"
    rev "${1:0: -1}"
}
$ rev 'vtr«­¥ºc€a'
a€cº¥­«rtv

rev command is of no great help, at least for binary files because rev terminates at 0x00. rev命令没有太大帮助,至少对于二进制文件而言,因为rev终止于0x00。

$ export LC_ALL='en_US.UTF-8'
$ echo -e "\xe4" | rev
rev: stdin: Invalid or incomplete multibyte or wide character
$ 
$ export LC_ALL='de_DE.ISO-8859-1'
$ echo -e "\xe4" | rev | od -tx1
0000000 e4 0a
0000002
$ 
$ export LC_ALL='en_US.UTF-8'
$ echo -e "te\x00st" | rev | od -tx1
0000000 65 74 0a
0000003
$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM