Sponsored Content
Top Forums UNIX for Advanced & Expert Users A hexdumper using echo only... Post 302931159 by sea on Sunday 11th of January 2015 07:07:55 AM
Old 01-11-2015
Nice one.

I'm quite confused that the 'difficult', as in structured, output is printed increadible fast, where as the 'long single string' takes that long.

I tried a bit and.. well you not gonna like it...
I actualy just commented out all your translating, so the script ends alot faster.
Code:
#!/bin/bash
# hex.sh </full/path/to/filename>
# Barry Walker @ https://www.unix.com/unix-for-advanced-and-expert-users/254451-hexdumper-using-echo-only.html
#

....
while read -s -d '' -r -n 1 char
do
	subscript=0
	count=0
	while [ $count -le 255 ]
	do
		...
		count=$((count+1))
		if [ $count -eq 256 ]
		then
			#echo -n "0A "
			break
		fi
	done
done < $1
IFS="$ifs_str"
exit


What i do not understand is, how do you get it formated in the first place?
Is that read doing it?
Not sure if this could be displayed on a moblie phone display.

Code:
time hex.sh blubber.sh ;ll blubber.sh 

00000000  23 2f 62 69 6e 2f 62 61  73 68 0a 23 0a 23 09 46  |#/bin/bash.#.#.F|
00000010  75 6e 63 74 69 6f 6e 0a  23 0a 09 6e 75 6d 32 72  |unction.#..num2r|
00000020  6f 6d 61 6e 28 29 20 7b  20 23 20 4e 55 4d 0a 09  |oman() { # NUM..|
00000030  23 20 52 65 74 75 72 6e  73 20 4e 55 4d 20 69 6e  |# Returns NUM in|
00000040  20 72 6f 6d 61 6e 20 6c  65 74 74 65 72 73 0a 09  | roman letters..|
00000050  23 0a 09 09 69 6e 70 75  74 3d 24 31 09 23 20 69  |#...input=$1.# i|
00000060  6e 70 75 74 20 6e 75 6d  0a 09 09 6f 75 74 70 75  |nput num...outpu|
00000070  74 3d 22 22 09 23 20 43  6c 65 61 72 20 6f 75 74  |t="".# Clear out|
00000080  70 75 74 20 73 74 72 69  6e 67 0a 09 09 6c 65 6e  |put string...len|
00000090  3d 24 7b 23 69 6e 70 75  74 7d 09 23 20 49 6e 69  |=${#input}.# Ini|
000000a0  74 69 61 6c 20 6c 65 6e  67 74 68 20 74 6f 20 63  |tial length to c|
000000b0  6f 75 6e 74 20 64 6f 77  6e 0a 09 09 0a 09 09 72  |ount down......r|
000000c0  6f 6d 61 6e 5f 76 61 6c  28 29 20 7b 20 23 20 4e  |oman_val() { # N|
000000d0  55 4d 20 6f 6e 65 20 66  69 76 65 20 74 65 6e 0a  |UM one five ten.|
000000e0  09 09 23 20 54 68 69 73  20 73 75 62 20 64 6f 65  |..# This sub doe|
000000f0  73 20 74 68 65 20 62 61  73 69 63 20 27 72 6f 6d  |s the basic 'rom|
00000100  61 6e 27 20 61 6c 67 6f  72 79 74 68 6d 0a 09 09  |an' algorythm...|
00000110  23 0a 09 09 09 4e 3d 24  31 0a 09 09 09 6f 6e 65  |#....N=$1....one|
00000120  3d 24 32 0a 09 09 09 66  69 76 65 3d 24 33 0a 09  |=$2....five=$3..|
00000130  09 09 74 65 6e 3d 24 34  0a 09 09 09 6f 75 74 3d  |..ten=$4....out=|
00000140  22 22 0a 09 09 09 0a 09  09 09 63 61 73 65 20 24  |""........case $|
00000150  4e 20 69 6e 0a 09 09 09  30 29 09 6f 75 74 2b 3d  |N in....0).out+=|
00000160  22 22 09 3b 3b 0a 09 09  09 5b 31 32 33 5d 29 09  |"".;;....[123]).|
00000170  77 68 69 6c 65 20 5b 5b  20 24 4e 20 2d 67 74 20  |while [[ $N -gt |
00000180  30 20 5d 5d 0a 09 09 09  09 64 6f 09 6f 75 74 2b  |0 ]].....do.out+|
00000190  3d 22 24 6f 6e 65 22 0a  09 09 09 09 09 4e 3d 24  |="$one"......N=$|
000001a0  28 28 24 4e 2d 31 29 29  0a 09 09 09 09 64 6f 6e  |(($N-1)).....don|
000001b0  65 0a 09 09 09 09 3b 3b  0a 09 09 09 34 29 09 6f  |e.....;;....4).o|
000001c0  75 74 2b 3d 22 24 6f 6e  65 24 66 69 76 65 22 09  |ut+="$one$five".|
000001d0  3b 3b 0a 09 09 09 35 29  09 6f 75 74 2b 3d 22 24  |;;....5).out+="$|
000001e0  66 69 76 65 22 09 3b 3b  0a 09 09 09 5b 36 37 38  |five".;;....[678|
000001f0  5d 29 09 6f 75 74 2b 3d  22 24 66 69 76 65 22 0a  |]).out+="$five".|
00000200  09 09 09 09 4e 3d 24 28  28 24 4e 2d 35 29 29 0a  |....N=$(($N-5)).|
00000210  09 09 09 09 77 68 69 6c  65 20 5b 5b 20 24 4e 20  |....while [[ $N |
00000220  2d 67 74 20 30 20 5d 5d  0a 09 09 09 09 64 6f 09  |-gt 0 ]].....do.|
00000230  6f 75 74 2b 3d 22 24 6f  6e 65 22 0a 09 09 09 09  |out+="$one".....|
00000240  09 4e 3d 24 28 28 24 4e  2d 31 29 29 0a 09 09 09  |.N=$(($N-1))....|
00000250  09 64 6f 6e 65 0a 09 09  09 09 3b 3b 0a 09 09 09  |.done.....;;....|
00000260  39 29 09 77 68 69 6c 65  20 5b 5b 20 24 4e 20 2d  |9).while [[ $N -|
00000270  6c 74 20 31 30 20 5d 5d  0a 09 09 09 09 64 6f 09  |lt 10 ]].....do.|
00000280  6f 75 74 2b 3d 22 24 6f  6e 65 22 0a 09 09 09 09  |out+="$one".....|
00000290  09 4e 3d 24 28 28 24 4e  2b 31 29 29 0a 09 09 09  |.N=$(($N+1))....|
000002a0  09 64 6f 6e 65 0a 09 09  09 09 6f 75 74 2b 3d 22  |.done.....out+="|
000002b0  24 74 65 6e 22 0a 09 09  09 09 3b 3b 0a 09 09 09  |$ten".....;;....|
000002c0  65 73 61 63 0a 09 09 09  65 63 68 6f 20 24 6f 75  |esac....echo $ou|
000002d0  74 0a 09 09 7d 0a 09 09  0a 09 09 77 68 69 6c 65  |t...}......while|
000002e0  20 5b 5b 20 24 6c 65 6e  20 2d 67 74 20 30 20 20  | [[ $len -gt 0  |
000002f0  5d 5d 0a 09 09 64 6f 09  23 20 54 68 65 72 65 20  |]]...do.# There |
00000300  61 72 65 20 6c 65 74 74  65 72 73 20 74 6f 20 61  |are letters to a|
00000310  64 64 0a 09 09 09 6e 75  6d 3d 24 7b 69 6e 70 75  |dd....num=${inpu|
00000320  74 3a 30 3a 31 7d 0a 09  09 09 23 20 44 6f 20 61  |t:0:1}....# Do a|
00000330  63 74 69 6f 6e 20 61 63  63 6f 72 64 69 6e 67 20  |ction according |
00000340  70 6f 73 69 74 69 6f 6e  0a 09 09 09 63 61 73 65  |position....case|
00000350  20 24 6c 65 6e 20 69 6e  0a 09 09 09 31 29 09 23  | $len in....1).#|
00000360  20 31 0a 09 09 09 09 6f  75 74 70 75 74 2b 3d 22  | 1.....output+="|
00000370  24 28 72 6f 6d 61 6e 5f  76 61 6c 20 24 6e 75 6d  |$(roman_val $num|
00000380  20 49 20 56 20 58 29 22  0a 09 09 09 09 3b 3b 0a  | I V X)".....;;.|
00000390  09 09 09 32 29 09 23 20  31 30 0a 09 09 09 09 6f  |...2).# 10.....o|
000003a0  75 74 70 75 74 2b 3d 22  24 28 72 6f 6d 61 6e 5f  |utput+="$(roman_|
000003b0  76 61 6c 20 24 6e 75 6d  20 58 20 4c 20 43 29 22  |val $num X L C)"|
000003c0  0a 09 09 09 09 3b 3b 0a  09 09 09 33 29 09 23 20  |.....;;....3).# |
000003d0  31 30 30 0a 09 09 09 09  6f 75 74 70 75 74 2b 3d  |100.....output+=|
000003e0  22 24 28 72 6f 6d 61 6e  5f 76 61 6c 20 24 6e 75  |"$(roman_val $nu|
000003f0  6d 20 43 20 44 20 4d 29  22 0a 09 09 09 09 3b 3b  |m C D M)".....;;|
00000400  0a 09 09 09 2a 29 09 23  20 31 30 30 30 2b 0a 09  |....*).# 1000+..|
00000410  09 09 09 23 20 31 30 27  30 30 30 20 67 65 74 73  |...# 10'000 gets|
00000420  20 61 20 6c 69 6e 65 20  61 62 6f 76 65 2c 20 31  | a line above, 1|
00000430  30 30 27 30 30 30 20 67  65 74 73 20 61 20 6c 69  |00'000 gets a li|
00000440  6e 65 20 6f 6e 20 74 68  65 20 6c 65 66 74 2e 2e  |ne on the left..|
00000450  20 68 6f 77 20 74 6f 3f  0a 09 09 09 09 6e 75 6d  | how to?.....num|
00000460  3d 24 7b 69 6e 70 75 74  3a 30 3a 28 2d 33 29 7d  |=${input:0:(-3)}|
00000470  0a 09 09 09 09 77 68 69  6c 65 20 5b 5b 20 24 6e  |.....while [[ $n|
00000480  75 6d 20 2d 67 74 20 30  20 5d 5d 0a 09 09 09 09  |um -gt 0 ]].....|
00000490  64 6f 09 6f 75 74 70 75  74 2b 3d 22 4d 22 0a 09  |do.output+="M"..|
000004a0  09 09 09 09 6e 75 6d 3d  24 28 28 24 6e 75 6d 2d  |....num=$(($num-|
000004b0  31 29 29 0a 09 09 09 09  64 6f 6e 65 0a 09 09 09  |1)).....done....|
000004c0  09 0a 09 09 09 09 3b 3b  0a 09 09 09 65 73 61 63  |......;;....esac|
000004d0  0a 09 09 09 69 6e 70 75  74 3d 24 7b 69 6e 70 75  |....input=${inpu|
000004e0  74 3a 31 7d 20 3b 20 6c  65 6e 3d 24 7b 23 69 6e  |t:1} ; len=${#in|
000004f0  70 75 74 7d 0a 09 09 64  6f 6e 65 0a 09 09 65 63  |put}...done...ec|
00000500  68 6f 20 24 6f 75 74 70  75 74 0a 09 7d 0a 23 0a  |ho $output..}.#.|
00000510  23 09 43 61 6c 6c 20 69  74 0a 23 0a 09 6e 75 6d  |#.Call it.#..num|
00000520  32 72 6f 6d 61 6e 20 24  31                       |2roman $1|
00000529

real	0m2.539s
user	0m3.583s
sys	0m0.066s
-rw-rw-r--. 1 sea sea 1.3K  9. Dez 12:27 blubber.sh

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

echo with \n

Hi, When I do /usr/ucb/echo "message \n" in bone shell program , \n never works as new line feeder. It just showed it as part of my message. Is there something wrong with my system? Thanks! (3 Replies)
Discussion started by: whatisthis
3 Replies

2. UNIX for Advanced & Expert Users

What does echo ^[[r do?

I tried echo ^[[r and it takes the cursor to first line in the telnet window... and echo ^[[32m changes font color to green .... Also echo ^[[00ff takes cursor to first command line in the current window (Note : ^ is entered as Ctrl + V and first [ as Escape character) Wud like to know how... (1 Reply)
Discussion started by: hamsasal
1 Replies

3. Shell Programming and Scripting

Difference between using "echo" builtin and /bin/echo

So in my shell i execute: { while true; do echo string; sleep 1; done } | read line This waits one second and returns. But { while true; do /bin/echo string; sleep 1; done } | read line continues to run, and doesn't stop until i kill it explicitly. I have tried this in bash as well as zsh,... (2 Replies)
Discussion started by: ulidtko
2 Replies

4. Shell Programming and Scripting

Using Echo

Hi All, Need help in resoving the issue . Can anyone let me know how to get fix length using unix shellscript. For Ex: echo "NUMBER OF RECORDS "${NO_OF_ROWS}"\nFILE CREATION DATE&TIME "${PROD_DT}" output should be : NUMBER OF RECORDS 2546 CREATIN DATE&TIME 2009-12-01 Each... (14 Replies)
Discussion started by: Samtel
14 Replies

5. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

6. Shell Programming and Scripting

Help with echo

Hi Guys, I need to print a value in the same line , But when we use the echo instead the loops (while), the value goes to the next line.. Can you help me in this.. Thanks For your help in advance. (6 Replies)
Discussion started by: mac4rfree
6 Replies

7. Shell Programming and Scripting

With that logic this echoes "echo". Question about echo!

echo `echo ` doesn't echoes anything. And it's logic. But echo `echo `echo ` ` does echoes "echo". What's the logic of it? the `echo `echo ` inside of the whole (first) echo, echoes nothing, so the first echo have to echo nothing but echoes "echo" (too much echoing :P):o (2 Replies)
Discussion started by: hakermania
2 Replies

8. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

9. UNIX for Dummies Questions & Answers

Difference between echo `ls -l` and echo "`ls -l`" ?

Hi guys, Been messing around with shell programming for a couple of days and I found something that was pretty odd in the behavior of the echo command. Below is an example-: When I type the following in my /home directory from my lxterminal in Debian-: echo "`ls -l`" I get the... (2 Replies)
Discussion started by: sreyan32
2 Replies

10. Programming

Python hexdumper with a difference.

Hi guys... I haven't done any real Python code since I started messing with Shell Scripting in January 2012 when I started AudioScope. Well I fished a little project out of my ANCIENT AMIGA files which created a raw hexdump of any binary file within reason for a stock A1200(HD). Well... (0 Replies)
Discussion started by: wisecracker
0 Replies
All times are GMT -4. The time now is 09:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy