Hi guys...
Sometime ago I said I was going to attempt an Android project.
I got my phone for XMAS and after installing a terminal program I realised how limited the command line is.
I do NOT intend to __root__ the phone at this point but I can read and write to certain folders.
The biggest problem is the vast majority of the commands are missing - OUCH!
awk, printf, sed, hexdump, xxd, od, and many many others we take for granted are not available...
I DO have a subset of builtins of which echo -n -e -E
is but one.
The terminal program has a large subset of the terminal escape codes so this is a bonus.
This set me on a quest to start my own commands using what I have got to play with.
This DEMO hex dumper for example is a starter.
This is as bizarre as shell scripting can possibly get.
It is SSLLOOWW but gives me the ability to read in small amounts of binary data for ideas in my head.
I have no idea yet whether this works on my phone but I will let you all know in due course.
The phones's default shell [sh] is bash...
** My question is can this be speeded up as it takes around a minute or so to read a random 256 byte file on this MBP. **
This DEMO works like this:-
1) 256 single byte files are generated from 0x00 to 0xFF named as 00.HEX to FF.HEX.
2) $1 is the full path and filename required for viewing.
3) The file $1 is read byte by byte and compared against the single byte files cat(ted) using a brute force method.
4) With the exception of two values, NULL and NEWLINE, these binary values are are printed onto STDOUT only for this DEMO.
5) Read the code for NULL and NEWLINE.
6) The HEX values are extracted from the filenames, NOT, the bytes themselves... <wink>
This was the only way I could think of to get binary into my machine with a small subset of the shell command available...
There is nothing on the net about this... ;o/
LBNL the first display is a random file generated to show it works in binary mode and the second is a hexdump of itself.
Note that hexdump is there for comparison only...
#!/bin/bash
# hex.sh </full/path/to/filename>
ifs_str="$IFS"
IFS=""
for high_nibble in 0 1 2 3 4 5 6 7 8 9 A B C D E F
do
for low_nibble in 0 1 2 3 4 5 6 7 8 9 A B C D E F
do
echo -e -n "\x$high_nibble$low_nibble" > /tmp/$high_nibble$low_nibble'.HEX'
done
done
listing=$(ls /tmp/*.HEX)
count=0
char=""
hexval="/tmp/00.HEX"
subscript=0
chr=""
hexdump -C $1
while read -s -d '' -r -n 1 char
do
subscript=0
count=0
while [ $count -le 255 ]
do
hexval="${listing:$subscript:${#hexval}}"
chr=$(cat $hexval)
if [ "$char" == "$chr" ]
then
if [ "$chr" == "" ]
then
echo -n "00 "
break
fi
if [ "$chr" != "" ]
then
echo -n "${hexval:$((${#hexval}-6)):2} "
break
fi
fi
subscript=$((subscript+${#hexval}+1))
count=$((count+1))
if [ $count -eq 256 ]
then
echo -n "0A "
fi
done
done < $1
IFS="$ifs_str"
exit
Results, edited to __remove__ the ultra long string created:-
Last login: Sun Jan 11 09:39:47 on ttys000
AMIGA:barrywalker~> cd Desktop
AMIGA:barrywalker~/Desktop> cd Code
AMIGA:barrywalker~/Desktop/Code> cd Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./hex.sh binary
00000000 b2 5f 96 da d0 5d 4e ba bc 6d 31 4f 4b 81 2c eb |._...]N..m1OK.,.|
00000010 b9 ce 53 8e 6a e4 ab fc 98 7a 8a 81 d1 ad 41 ab |..S.j....z....A.|
00000020 79 83 c0 c9 54 d7 c2 2f 0b ae 54 9e 38 fe 5c 2e |y...T../..T.8.\.|
00000030 ce 96 1d 51 f8 f2 16 9d ef dc 23 b9 3f b6 56 8c |...Q......#.?.V.|
00000040 92 1d 54 38 9b 8c b1 2b 5b 1e 2c 42 b3 89 da 47 |..T8...+[.,B...G|
00000050 7e 95 2d 21 8a d7 77 b9 f9 db 05 76 de 04 ca 14 |~.-!..w....v....|
00000060 20 66 23 7e 93 fc fa fd 5f ad c9 1c 39 8d 47 65 | f#~...._...9.Ge|
00000070 ac ee d8 c2 c9 fe b6 16 13 04 a7 8b 43 77 70 a1 |............Cwp.|
00000080 06 0f 88 9d 0a 0a 1a 14 37 51 57 be fd 74 ab 0b |........7QW..t..|
00000090 2f 50 64 9b 28 b7 98 df 8a 74 aa 21 07 83 a5 4d |/Pd.(....t.!...M|
000000a0 d0 42 f2 b1 91 a5 83 64 2c 1e a5 81 80 e2 a7 8a |.B.....d,.......|
000000b0 84 7d 14 78 9e 38 96 d5 fe b1 7e d6 ad d9 21 f9 |.}.x.8....~...!.|
000000c0 d2 cf 32 92 5d 30 7b ef 93 99 23 81 ab c3 db 87 |..2.]0{...#.....|
000000d0 c9 b8 62 4a 1f 0d 34 fe 9f dc 7d 82 98 bd 09 3d |..bJ..4...}....=|
000000e0 91 04 60 42 d1 53 09 8f 50 3a 79 80 33 fa 16 23 |..`B.S..P:y.3..#|
000000f0 76 48 76 3a 68 b6 7c 4c 7d 89 30 47 32 8e 56 41 |vHv:h.|L}.0G2.VA|
00000100 9a 6d 19 cc d6 c5 8b db 6f 29 60 19 2e f9 4c 17 |.m......o)`...L.|
00000110 f8 31 8c a0 27 34 63 74 f7 ed 8e cf e5 53 16 e1 |.1..'4ct.....S..|
00000120 18 c2 47 2c 4b 7a ed 0b c8 f1 58 f1 72 36 bf c4 |..G,Kz....X.r6..|
00000130 a4 f8 d4 9b f1 64 6b 57 dc 36 1b 22 4c 26 2a 87 |.....dkW.6."L&*.|
00000140 e4 fd fa 5f a2 6b b8 1f 51 8e c9 f5 cb 23 e3 38 |..._.k..Q....#.8|
00000150 35 1d bb 0f d8 ec 57 70 c7 6b cb 7d 74 7e 2f 62 |5.....Wp.k.}t~/b|
00000160 44 12 08 b3 11 17 e5 54 6d f5 76 53 bd 91 ba d2 |D......Tm.vS....|
00000170 cb 15 4d 94 34 d0 1e 2b 63 f3 05 eb d7 4f 42 e6 |..M.4..+c....OB.|
00000180 a7 fa 66 21 40 42 ad 43 83 12 31 23 d0 cf b0 6b |..f!@B.C..1#...k|
00000190 b2 d7 b2 e8 e5 65 1d 88 dd 03 8a 3a 25 93 b9 fe |.....e.....:%...|
000001a0 c7 96 5c 66 52 b8 3f 14 c9 88 8a 53 19 c4 a1 0a |..\fR.?....S....|
000001b0 f6 be d4 a9 36 85 b8 4d 2f f8 eb ff da be 8a d7 |....6..M/.......|
000001c0 8e 48 58 02 ed fc 69 44 fe 7e 94 43 00 8e 74 7c |.HX...iD.~.C..t||
000001d0 b8 45 57 2b 0a 28 f5 a6 ba 3c 1d da 6c 70 23 41 |.EW+.(...<..lp#A|
000001e0 9f 89 74 fd 1b be 2a ff 18 6a 79 a6 b0 7c f9 36 |..t...*..jy..|.6|
000001f0 8d ce 2d b0 c4 83 78 fd cf 9c de 48 ec 46 9c 9f |..-...x....H.F..|
00000200
B2 5F 96 DA D0 5D 4E BA BC 6D 31 4F 4B 81 2C EB B9 CE 53 8E 6A E4 AB FC 98 7A
8A 81 D1 AD 41 AB 79 83 C0 C9 54 D7 C2 2F 0B AE 54 9E 38 FE 5C 2E CE 96 1D 51
F8 F2 16 9D EF DC 23 B9 3F B6 56 8C 92 1D 54 38 9B 8C B1 2B 5B 1E 2C 42 B3 89
DA 47 7E 95 2D 21 8A D7 77 B9 F9 DB 05 76 DE 04 CA 14 20 66 23 7E 93 FC FA FD
5F AD C9 1C 39 8D 47 65 AC EE D8 C2 C9 FE B6 16 13 04 A7 8B 43 77 70 A1 06 0F
88 9D 0A 0A 1A 14 37 51 57 BE FD 74 AB 0B 2F 50 64 9B 28 B7 98 DF 8A 74 AA 21
07 83 A5 4D D0 42 F2 B1 91 A5 83 64 2C 1E A5 81 80 E2 A7 8A 84 7D 14 78 9E 38
96 D5 FE B1 7E D6 AD D9 21 F9 D2 CF 32 92 5D 30 7B EF 93 99 23 81 AB C3 DB 87
C9 B8 62 4A 1F 0D 34 FE 9F DC 7D 82 98 BD 09 3D 91 04 60 42 D1 53 09 8F 50 3A
79 80 33 FA 16 23 76 48 76 3A 68 B6 7C 4C 7D 89 30 47 32 8E 56 41 9A 6D 19 CC
D6 C5 8B DB 6F 29 60 19 2E F9 4C 17 F8 31 8C A0 27 34 63 74 F7 ED 8E CF E5 53
16 E1 18 C2 47 2C 4B 7A ED 0B C8 F1 58 F1 72 36 BF C4 A4 F8 D4 9B F1 64 6B 57
DC 36 1B 22 4C 26 2A 87 E4 FD FA 5F A2 6B B8 1F 51 8E C9 F5 CB 23 E3 38 35 1D
BB 0F D8 EC 57 70 C7 6B CB 7D 74 7E 2F 62 44 12 08 B3 11 17 E5 54 6D F5 76 53
BD 91 BA D2 CB 15 4D 94 34 D0 1E 2B 63 F3 05 EB D7 4F 42 E6 A7 FA 66 21 40 42
AD 43 83 12 31 23 D0 CF B0 6B B2 D7 B2 E8 E5 65 1D 88 DD 03 8A 3A 25 93 B9 FE
C7 96 5C 66 52 B8 3F 14 C9 88 8A 53 19 C4 A1 0A F6 BE D4 A9 36 85 B8 4D 2F F8
EB FF DA BE 8A D7 8E 48 58 02 ED FC 69 44 FE 7E 94 43 00 8E 74 7C B8 45 57 2B
0A 28 F5 A6 BA 3C 1D DA 6C 70 23 41 9F 89 74 FD 1B BE 2A FF 18 6A 79 A6 B0 7C
F9 36 8D CE 2D B0 C4 83 78 FD CF 9C DE 48 EC 46 9C 9F AMIGA:barrywalker~/Desktop/Code/Shell>
AMIGA:barrywalker~/Desktop/Code/Shell>
AMIGA:barrywalker~/Desktop/Code/Shell> ./hex.sh hex.sh
00000000 23 21 2f 62 69 6e 2f 62 61 73 68 0a 23 20 68 65 |#!/bin/bash.# he|
00000010 78 2e 73 68 20 3c 2f 66 75 6c 6c 2f 70 61 74 68 |x.sh </full/path|
00000020 2f 74 6f 2f 66 69 6c 65 6e 61 6d 65 3e 0a 69 66 |/to/filename>.if|
00000030 73 5f 73 74 72 3d 22 24 49 46 53 22 0a 49 46 53 |s_str="$IFS".IFS|
00000040 3d 22 22 0a 66 6f 72 20 68 69 67 68 5f 6e 69 62 |="".for high_nib|
00000050 62 6c 65 20 69 6e 20 30 20 31 20 32 20 33 20 34 |ble in 0 1 2 3 4|
00000060 20 35 20 36 20 37 20 38 20 39 20 41 20 42 20 43 | 5 6 7 8 9 A B C|
00000070 20 44 20 45 20 46 0a 64 6f 0a 09 66 6f 72 20 6c | D E F.do..for l|
00000080 6f 77 5f 6e 69 62 62 6c 65 20 69 6e 20 30 20 31 |ow_nibble in 0 1|
00000090 20 32 20 33 20 34 20 35 20 36 20 37 20 38 20 39 | 2 3 4 5 6 7 8 9|
000000a0 20 41 20 42 20 43 20 44 20 45 20 46 0a 09 64 6f | A B C D E F..do|
000000b0 0a 09 09 65 63 68 6f 20 2d 65 20 2d 6e 20 22 5c |...echo -e -n "\|
000000c0 78 24 68 69 67 68 5f 6e 69 62 62 6c 65 24 6c 6f |x$high_nibble$lo|
000000d0 77 5f 6e 69 62 62 6c 65 22 20 3e 20 2f 74 6d 70 |w_nibble" > /tmp|
000000e0 2f 24 68 69 67 68 5f 6e 69 62 62 6c 65 24 6c 6f |/$high_nibble$lo|
000000f0 77 5f 6e 69 62 62 6c 65 27 2e 48 45 58 27 0a 09 |w_nibble'.HEX'..|
00000100 64 6f 6e 65 0a 64 6f 6e 65 0a 6c 69 73 74 69 6e |done.done.listin|
00000110 67 3d 24 28 6c 73 20 2f 74 6d 70 2f 2a 2e 48 45 |g=$(ls /tmp/*.HE|
00000120 58 29 0a 63 6f 75 6e 74 3d 30 0a 63 68 61 72 3d |X).count=0.char=|
00000130 22 22 0a 68 65 78 76 61 6c 3d 22 2f 74 6d 70 2f |"".hexval="/tmp/|
00000140 30 30 2e 48 45 58 22 0a 73 75 62 73 63 72 69 70 |00.HEX".subscrip|
00000150 74 3d 30 0a 63 68 72 3d 22 22 0a 68 65 78 64 75 |t=0.chr="".hexdu|
00000160 6d 70 20 2d 43 20 24 31 0a 77 68 69 6c 65 20 72 |mp -C $1.while r|
00000170 65 61 64 20 2d 73 20 2d 64 20 27 27 20 2d 72 20 |ead -s -d '' -r |
00000180 2d 6e 20 31 20 63 68 61 72 0a 64 6f 0a 09 73 75 |-n 1 char.do..su|
00000190 62 73 63 72 69 70 74 3d 30 0a 09 63 6f 75 6e 74 |bscript=0..count|
000001a0 3d 30 0a 09 77 68 69 6c 65 20 5b 20 24 63 6f 75 |=0..while [ $cou|
000001b0 6e 74 20 2d 6c 65 20 32 35 35 20 5d 0a 09 64 6f |nt -le 255 ]..do|
000001c0 0a 09 09 68 65 78 76 61 6c 3d 22 24 7b 6c 69 73 |...hexval="${lis|
000001d0 74 69 6e 67 3a 24 73 75 62 73 63 72 69 70 74 3a |ting:$subscript:|
000001e0 24 7b 23 68 65 78 76 61 6c 7d 7d 22 0a 09 09 63 |${#hexval}}"...c|
000001f0 68 72 3d 24 28 63 61 74 20 24 68 65 78 76 61 6c |hr=$(cat $hexval|
00000200 29 0a 09 09 69 66 20 5b 20 22 24 63 68 61 72 22 |)...if [ "$char"|
00000210 20 3d 3d 20 22 24 63 68 72 22 20 5d 0a 09 09 74 | == "$chr" ]...t|
00000220 68 65 6e 0a 09 09 09 69 66 20 5b 20 22 24 63 68 |hen....if [ "$ch|
00000230 72 22 20 3d 3d 20 22 22 20 5d 0a 09 09 09 74 68 |r" == "" ]....th|
00000240 65 6e 0a 09 09 09 09 65 63 68 6f 20 2d 6e 20 22 |en.....echo -n "|
00000250 30 30 20 22 0a 09 09 09 09 62 72 65 61 6b 0a 09 |00 ".....break..|
00000260 09 09 66 69 0a 09 09 09 69 66 20 5b 20 22 24 63 |..fi....if [ "$c|
00000270 68 72 22 20 21 3d 20 22 22 20 5d 0a 09 09 09 74 |hr" != "" ]....t|
00000280 68 65 6e 0a 09 09 09 09 65 63 68 6f 20 2d 6e 20 |hen.....echo -n |
00000290 22 24 7b 68 65 78 76 61 6c 3a 24 28 28 24 7b 23 |"${hexval:$((${#|
000002a0 68 65 78 76 61 6c 7d 2d 36 29 29 3a 32 7d 20 22 |hexval}-6)):2} "|
000002b0 0a 09 09 09 09 62 72 65 61 6b 0a 09 09 09 66 69 |.....break....fi|
000002c0 0a 09 09 66 69 0a 09 09 73 75 62 73 63 72 69 70 |...fi...subscrip|
000002d0 74 3d 24 28 28 73 75 62 73 63 72 69 70 74 2b 24 |t=$((subscript+$|
000002e0 7b 23 68 65 78 76 61 6c 7d 2b 31 29 29 0a 09 09 |{#hexval}+1))...|
000002f0 63 6f 75 6e 74 3d 24 28 28 63 6f 75 6e 74 2b 31 |count=$((count+1|
00000300 29 29 0a 09 09 69 66 20 5b 20 24 63 6f 75 6e 74 |))...if [ $count|
00000310 20 2d 65 71 20 32 35 36 20 5d 0a 09 09 74 68 65 | -eq 256 ]...the|
00000320 6e 0a 09 09 09 65 63 68 6f 20 2d 6e 20 22 30 41 |n....echo -n "0A|
00000330 20 22 0a 09 09 66 69 0a 09 64 6f 6e 65 0a 64 6f | "...fi..done.do|
00000340 6e 65 20 3c 20 24 31 0a 49 46 53 3d 22 24 69 66 |ne < $1.IFS="$if|
00000350 73 5f 73 74 72 22 0a 65 78 69 74 0a |s_str".exit.|
0000035c
23 21 2F 62 69 6E 2F 62 61 73 68 0A 23 20 68 65 78 2E 73 68 20 3C 2F 66 75 6C
6C 2F 70 61 74 68 2F 74 6F 2F 66 69 6C 65 6E 61 6D 65 3E 0A 69 66 73 5F 73 74
72 3D 22 24 49 46 53 22 0A 49 46 53 3D 22 22 0A 66 6F 72 20 68 69 67 68 5F 6E
69 62 62 6C 65 20 69 6E 20 30 20 31 20 32 20 33 20 34 20 35 20 36 20 37 20 38
20 39 20 41 20 42 20 43 20 44 20 45 20 46 0A 64 6F 0A 09 66 6F 72 20 6C 6F 77
5F 6E 69 62 62 6C 65 20 69 6E 20 30 20 31 20 32 20 33 20 34 20 35 20 36 20 37
20 38 20 39 20 41 20 42 20 43 20 44 20 45 20 46 0A 09 64 6F 0A 09 09 65 63 68
6F 20 2D 65 20 2D 6E 20 22 5C 78 24 68 69 67 68 5F 6E 69 62 62 6C 65 24 6C 6F
77 5F 6E 69 62 62 6C 65 22 20 3E 20 2F 74 6D 70 2F 24 68 69 67 68 5F 6E 69 62
62 6C 65 24 6C 6F 77 5F 6E 69 62 62 6C 65 27 2E 48 45 58 27 0A 09 64 6F 6E 65
0A 64 6F 6E 65 0A 6C 69 73 74 69 6E 67 3D 24 28 6C 73 20 2F 74 6D 70 2F 2A 2E
48 45 58 29 0A 63 6F 75 6E 74 3D 30 0A 63 68 61 72 3D 22 22 0A 68 65 78 76 61
6C 3D 22 2F 74 6D 70 2F 30 30 2E 48 45 58 22 0A 73 75 62 73 63 72 69 70 74 3D
30 0A 63 68 72 3D 22 22 0A 68 65 78 64 75 6D 70 20 2D 43 20 24 31 0A 77 68 69
6C 65 20 72 65 61 64 20 2D 73 20 2D 64 20 27 27 20 2D 72 20 2D 6E 20 31 20 63
68 61 72 0A 64 6F 0A 09 73 75 62 73 63 72 69 70 74 3D 30 0A 09 63 6F 75 6E 74
3D 30 0A 09 77 68 69 6C 65 20 5B 20 24 63 6F 75 6E 74 20 2D 6C 65 20 32 35 35
20 5D 0A 09 64 6F 0A 09 09 68 65 78 76 61 6C 3D 22 24 7B 6C 69 73 74 69 6E 67
3A 24 73 75 62 73 63 72 69 70 74 3A 24 7B 23 68 65 78 76 61 6C 7D 7D 22 0A 09
09 63 68 72 3D 24 28 63 61 74 20 24 68 65 78 76 61 6C 29 0A 09 09 69 66 20 5B
20 22 24 63 68 61 72 22 20 3D 3D 20 22 24 63 68 72 22 20 5D 0A 09 09 74 68 65
6E 0A 09 09 09 69 66 20 5B 20 22 24 63 68 72 22 20 3D 3D 20 22 22 20 5D 0A 09
09 09 74 68 65 6E 0A 09 09 09 09 65 63 68 6F 20 2D 6E 20 22 30 30 20 22 0A 09
09 09 09 62 72 65 61 6B 0A 09 09 09 66 69 0A 09 09 09 69 66 20 5B 20 22 24 63
68 72 22 20 21 3D 20 22 22 20 5D 0A 09 09 09 74 68 65 6E 0A 09 09 09 09 65 63
68 6F 20 2D 6E 20 22 24 7B 68 65 78 76 61 6C 3A 24 28 28 24 7B 23 68 65 78 76
61 6C 7D 2D 36 29 29 3A 32 7D 20 22 0A 09 09 09 09 62 72 65 61 6B 0A 09 09 09
66 69 0A 09 09 66 69 0A 09 09 73 75 62 73 63 72 69 70 74 3D 24 28 28 73 75 62
73 63 72 69 70 74 2B 24 7B 23 68 65 78 76 61 6C 7D 2B 31 29 29 0A 09 09 63 6F
75 6E 74 3D 24 28 28 63 6F 75 6E 74 2B 31 29 29 0A 09 09 69 66 20 5B 20 24 63
6F 75 6E 74 20 2D 65 71 20 32 35 36 20 5D 0A 09 09 74 68 65 6E 0A 09 09 09 65
63 68 6F 20 2D 6E 20 22 30 41 20 22 0A 09 09 66 69 0A 09 64 6F 6E 65 0A 64 6F
6E 65 20 3C 20 24 31 0A 49 46 53 3D 22 24 69 66 73 5F 73 74 72 22 0A 65 78 69
74 0A AMIGA:barrywalker~/Desktop/Code/Shell>
AMIGA:barrywalker~/Desktop/Code/Shell> _