[20240426]sql_id 转换hash_value.txt
--//以前写的脚本,转换sql_id to hash_value.遇到问题:
$ cat s2p.sh
#! /bin/bash
# convert sql_id to hash_value
odebug=${ODEBUG:-0}
sql_id="$*"
v1=$(echo $sql_id | tr $(echo {0..9} {a..z} | tr -d 'eilo ') $( echo {0..9} {a..v}| tr -d ' '))
v2=$(( 32#$v1 % 2**32 ))
if [ $odebug -eq 1 ] ; then
echo "sql_id=$sql_id v1=$v1 v2=$v2"
fi
echo "sql_id = $sql_id hash_value = $v2"
echo "sql_id = $sql_id hash_value = $(( 32#$(echo $sql_id | tr $(echo {0..9} {a..z} | tr -d 'eilo ') $( echo {0..9} {a..v}| tr -d ' ')) % 2**32 ))"
--//计算转换后结果为负数,例子:
$ export ODEBUG=1
$ ./s2p.sh arw6k2rf5s29b
sql_id=arw6k2rf5s29b v1=ans6i2ne5o29b v2=-597948117
sql_id = arw6k2rf5s29b hash_value = -597948117
sql_id = arw6k2rf5s29b hash_value = -597948117
$ ./s2p.sh 0rw6k2rf5s29b
sql_id=0rw6k2rf5s29b v1=ns6i2ne5o29b v2=3697019179
sql_id = rw6k2rf5s29b hash_value = 3697019179
sql_id = rw6k2rf5s29b hash_value = 3697019179
$ ./s2p.sh 2rw6k2rf5s29b
sql_id=2rw6k2rf5s29b v1=2ns6i2ne5o29b v2=3697019179
sql_id = 2rw6k2rf5s29b hash_value = 3697019179
sql_id = 2rw6k2rf5s29b hash_value = 3697019179
--//估计带入值arw6k2rf5s29b有点大,bash shell计算后变成负数,hash_value是取full_hash_value值(后8位,16进制).
--//这样 8*4 = 32 bit, 32/5 = 6.4(sql_id为32进制,2^5=32), 取最后7个字符计算就ok了,
--//修改代码取消开头第1个字符不会影响计算结果.
--//修改后代码如下:
$ cat s2p.sh
#! /bin/bash
# convert sql_id to hash_value
odebug=${ODEBUG:-0}
sql_id="$*"
v1=$(echo $sql_id | tr $(echo {0..9} {a..z} | tr -d 'eilo ') $( echo {0..9} {a..v}| tr -d ' '))
v2=$(( 32#$v1 % 2**32 ))
v3=${v1:(-12):12}
#v3=${v1:(-7):7}
v4=$(( 32#$v3 % 2**32 ))
if [ $odebug -eq 1 ] ; then
echo "sql_id=$sql_id v1=$v1 v2=$v2 v3=$v3 v4=$v4"
fi
echo "sql_id = $sql_id hash_value = $v2"
echo "sql_id = $sql_id hash_value = $v4"
# echo "sql_id = $sql_id hash_value = $(( 32#$(echo $sql_id | tr $(echo {0..9} {a..z} | tr -d 'eilo ') $( echo {0..9} {a..v}| tr -d ' ')) % 2**32 ))"
--//测试看看:
$ ./s2p.sh arw6k2rf5s29b
sql_id=arw6k2rf5s29b v1=ans6i2ne5o29b v2=-597948117 v3=ns6i2ne5o29b v4=3697019179
sql_id = arw6k2rf5s29b hash_value = -597948117
sql_id = arw6k2rf5s29b hash_value = 3697019179
--//还可以采用如果结果为负数,使用 2^32-597948117 = 3697019179也可以.