简体   繁体   中英

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.

The string is...

vtr«¥ºc€a

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.

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. The old server had LANG="en_GB" and the new server LANG="en_GB.UTF-8"

Changing the LANG back to en_GB resolved the reversing as UTF-8 ignored the character for some reason.

You can use rev utility:

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. 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.

$ 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
$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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