[20230428]bash实现xor计算.txt
[20230428]bash实现xor计算.txt
--//昨天使用自己以前写的脚本做xor计算,仔细看了自己以前写的源代码,这样写主要问题调用bc次数太多,计算效率很低.
--//http://blog.itpub.net/267265/viewspace-2134945/ => [20170308]bc做xor(异或)计算.txt
$ cat xor.sh
#! /bin/bash
# just play , calc xor!!
s='0'
for i in $(cat $1| tr 'a-f' 'A-F')
do
#echo "obase=16;ibase=16; xor($s,$i)"
echo $i
[ $i != '0000' ] && s=$(echo "obase=16;ibase=16; xor($s,$i)" | bc -l ~/bc/logic.bc)
done
echo -e "\nxor result: $s \n"
--//理论讲bash 应该支持xor,以前的学习不仔细,仔细看一些文档.发现bash本身就支持xor的计算,例子如下:
$ echo $(( 0x5 ^ 0x9 ))
12
--//自己尝试修改看看.
$ cat xor2.sh
#! /bin/bash
# just play , calc xor!!
s='0'
for i in $(cat $1| tr 'a-f' 'A-F')
do
#echo "obase=16;ibase=16; xor($s,$i)"
echo $i
#[ $i != '0000' ] && s=$(echo "obase=16;ibase=16; xor($s,$i)" | bc -l ~/bc/logic.bc)
[ $i != '0000' ] && s=$((0x$s ^ 0x$i )) && s=$(printf '%04x\n' $s)
done
echo -e "\nxor result: $s \n"
--//测试看看:
1.环境:
SCOTT@book> @ ver1
PORT_STRING VERSION BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx 11.2.0.4.0 Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
2.测试:
SCOTT@book> select rowid from dept where rownum=1;
ROWID
------------------
AAAVRCAAEAAAACHAAA
SCOTT@book> @ rowid AAAVRCAAEAAAACHAAA
OBJECT FILE BLOCK ROW ROWID_DBA DBA TEXT
---------- ---------- ---------- ---------- -------------------- -------------------- ----------------------------------------
87106 4 135 0 0x1000087 4,135 alter system dump datafile 4 block 135 ;
SCOTT@book> @ bbvi 4 135
BVI_COMMAND
----------------------------------------------------------------------------------------------------
bvi -b 1105920 -s 8192 /mnt/ramdisk/book/users01.dbf
xxd -c16 -g 2 -s 1105920 -l 8192 /mnt/ramdisk/book/users01.dbf
dd if=/mnt/ramdisk/book/users01.dbf bs=8192 skip=135 count=1 of=4_135.dd conv=notrunc 2>/dev/null
od -j 1105920 -N 8192 -t x1 -v /mnt/ramdisk/book/users01.dbf
hexdump -s 1105920 -n 8192 -C -v /mnt/ramdisk/book/users01.dbf
alter system dump datafile '/mnt/ramdisk/book/users01.dbf' block 135;
alter session set events 'immediate trace name set_tsn_p1 level 5';
alter session set events 'immediate trace name buffer level 16777351';
9 rows selected.
$ time xxd -c16 -g 2 -s 1105920 -l 8192 /mnt/ramdisk/book/users01.dbf | cut -c10-48| xor.sh | grep result
xor result: 0
real 0m18.801s
user 0m9.147s
sys 0m8.431s
--//使用旧的xor.sh脚本计算需要18秒.
$ time xxd -c16 -g 2 -s 1105920 -l 8192 /mnt/ramdisk/book/users01.dbf | cut -c10-48| xor2.sh | grep result
xor result: 0000
real 0m3.853s
user 0m0.539s
sys 0m1.258s
--//而新的xor2.sh脚本计算需要4秒完成,快了不少.
--//昨天使用自己以前写的脚本做xor计算,仔细看了自己以前写的源代码,这样写主要问题调用bc次数太多,计算效率很低.
--//http://blog.itpub.net/267265/viewspace-2134945/ => [20170308]bc做xor(异或)计算.txt
$ cat xor.sh
#! /bin/bash
# just play , calc xor!!
s='0'
for i in $(cat $1| tr 'a-f' 'A-F')
do
#echo "obase=16;ibase=16; xor($s,$i)"
echo $i
[ $i != '0000' ] && s=$(echo "obase=16;ibase=16; xor($s,$i)" | bc -l ~/bc/logic.bc)
done
echo -e "\nxor result: $s \n"
--//理论讲bash 应该支持xor,以前的学习不仔细,仔细看一些文档.发现bash本身就支持xor的计算,例子如下:
$ echo $(( 0x5 ^ 0x9 ))
12
--//自己尝试修改看看.
$ cat xor2.sh
#! /bin/bash
# just play , calc xor!!
s='0'
for i in $(cat $1| tr 'a-f' 'A-F')
do
#echo "obase=16;ibase=16; xor($s,$i)"
echo $i
#[ $i != '0000' ] && s=$(echo "obase=16;ibase=16; xor($s,$i)" | bc -l ~/bc/logic.bc)
[ $i != '0000' ] && s=$((0x$s ^ 0x$i )) && s=$(printf '%04x\n' $s)
done
echo -e "\nxor result: $s \n"
--//测试看看:
1.环境:
SCOTT@book> @ ver1
PORT_STRING VERSION BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx 11.2.0.4.0 Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
2.测试:
SCOTT@book> select rowid from dept where rownum=1;
ROWID
------------------
AAAVRCAAEAAAACHAAA
SCOTT@book> @ rowid AAAVRCAAEAAAACHAAA
OBJECT FILE BLOCK ROW ROWID_DBA DBA TEXT
---------- ---------- ---------- ---------- -------------------- -------------------- ----------------------------------------
87106 4 135 0 0x1000087 4,135 alter system dump datafile 4 block 135 ;
SCOTT@book> @ bbvi 4 135
BVI_COMMAND
----------------------------------------------------------------------------------------------------
bvi -b 1105920 -s 8192 /mnt/ramdisk/book/users01.dbf
xxd -c16 -g 2 -s 1105920 -l 8192 /mnt/ramdisk/book/users01.dbf
dd if=/mnt/ramdisk/book/users01.dbf bs=8192 skip=135 count=1 of=4_135.dd conv=notrunc 2>/dev/null
od -j 1105920 -N 8192 -t x1 -v /mnt/ramdisk/book/users01.dbf
hexdump -s 1105920 -n 8192 -C -v /mnt/ramdisk/book/users01.dbf
alter system dump datafile '/mnt/ramdisk/book/users01.dbf' block 135;
alter session set events 'immediate trace name set_tsn_p1 level 5';
alter session set events 'immediate trace name buffer level 16777351';
9 rows selected.
$ time xxd -c16 -g 2 -s 1105920 -l 8192 /mnt/ramdisk/book/users01.dbf | cut -c10-48| xor.sh | grep result
xor result: 0
real 0m18.801s
user 0m9.147s
sys 0m8.431s
--//使用旧的xor.sh脚本计算需要18秒.
$ time xxd -c16 -g 2 -s 1105920 -l 8192 /mnt/ramdisk/book/users01.dbf | cut -c10-48| xor2.sh | grep result
xor result: 0000
real 0m3.853s
user 0m0.539s
sys 0m1.258s
--//而新的xor2.sh脚本计算需要4秒完成,快了不少.