Sponsored Content
Full Discussion: Binary write POSIX-ly.
Top Forums UNIX for Advanced & Expert Users Binary write POSIX-ly. Post 302980443 by Don Cragun on Saturday 27th of August 2016 11:13:05 PM
Old 08-28-2016
Even though string operations on shell variables are relatively fast, stripping three characters off of a huge string takes a while. Consider splitting your huge string into shorter strings and feed them into your existing code in a loop. Note also that the standards say that octal values passed to the printf %b format specifier need to be in the format:
Quote:
"\0ddd", where ddd is a zero, one, two, or three-digit octal number that shall be
converted to a byte with the numeric value specified by the octal number
and your code is not supplying the leading 0 for octal values larger than 077.
I don't have dash installed on my system, but sh, bash, and ksh on OS X El Capitan Version 10.11.6 all produce the output you specified when running the following modified version of your script:
Code:
#!/bin/ksh
# alt_o2b.sh
# This has not been tested by ShellCheck and runs under sh, bash, and ksh!

# 256 bytes of octal values from 000 to 377.
split_octal="000001002003004005006007010011012013014015016017
020021022023024025026027030031032033034035036037040041042043044045046047
050051052053054055056057060061062063064065066067070071072073074075076077
100101102103104105106107110111112113114115116117120121122123124125126127
130131132133134135136137140141142143144145146147150151152153154155156157
160161162163164165166167170171172173174175176177200201202203204205206207
210211212213214215216217220221222223224225226227230231232233234235236237
240241242243244245246247250251252253254255256257260261262263264265266267
270271272273274275276277300301302303304305306307310311312313314315316317
320321322323324325326327330331332333334335336337340341342343344345346347
350351352353354355356357360361362363364365366367370371372373374375376377"

binary()
{
	printf '%s\n' $split_octal | while read octal
	do
		# Obtain octal string length.
		length="${#octal}"
		# Subscript position starts at 1 NOT 0."
		position=1
		# 3 character octal value to be read.
		ooo="???"
		while [ "$position" -lt "$length" ]
		do
			# These two lines obtain the octal value.
			subtext1=${octal%"${octal#${ooo}}"}
			subtext2=${subtext1#"${subtext1%???}"}
			# Convert to pure binary.
			printf '%b' "\0$subtext2"
			# Increment values needed.
			position=$(( position + 3 ))
			ooo=$ooo'???'
		done
	done
}

binary > /tmp/binary2

# The line below is just for checking...
hexdump -C /tmp/binary2

exit 0

Note that I changed the output file name form /tmp/binary to /tmp/binary2 so you can compare the results of the two scripts directly if you'd like to compare run-times of your script against this script and compare the output files produced.

When testing your script (using ksh instead of dash and with the 2nd operand to printf '%b' modified as shown in the script above, I get the same output as you got with both scripts. But, the script above ran in about 10% of the time needed to run your script. I would expect a considerably greater run time improvement for considerably longer input data.

Note also that although you were passing an argument to the binary function, the function you have defined does not use any positional parameters. Therefore, I have removed that operand from the function invocation.

Last edited by Don Cragun; 08-28-2016 at 08:16 AM.. Reason: Fix typo: s/iexdump/hexdump/ as noted in post #3.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Programming

ANSI C vs POSIX

can somebody explain about the ANSI C vs POSIX. say i was using open and fopen, i know that open is POSIX, and fopen is ANSI C. i read that that POSIX is a system call and ANSI C is like a standard library function. wouldn't the fopen function has to call on open function anyway to open any kind... (2 Replies)
Discussion started by: bb00y
2 Replies

2. UNIX for Advanced & Expert Users

what can I get the posix standard?

I wanted study and write a unix like system. who can help me. ------------- Removed the garbled characters... not sure why they were there... (2 Replies)
Discussion started by: crashsky
2 Replies

3. Programming

Posix

HI, When i am configuring php in SUN Solaris. I am getting the below error. configure: error: Your system seems to lack POSIX threads. Do i need to install POSIX? If so can somebody let me know where can i download POSIX for Solaris 8? Thanks, (2 Replies)
Discussion started by: Krrishv
2 Replies

4. Programming

how to write a file to binary format in C ?

I'm in the Solaris environment. I want to write data to a file, but I don't want it to be easily read from the C shell. For example, here's my code: main () { FILE *fo; fo = fopen ("filename", "w"); fprintf (fo, "This is a test.\n"); fclose (fo); } Anyone can open up... (3 Replies)
Discussion started by: serendipity1276
3 Replies

5. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

6. UNIX for Advanced & Expert Users

System V or POSIX

Hi , I am using UNIX network programming Vol1 (by R Stevens) book to learn about IPC. I would be using HP-UX,Solaris and Linux at my work. I have sections for POSIX and for System V in that book. I am quite confused in indentifying those OSs as POSIX or SYstem V. Can anyone please... (1 Reply)
Discussion started by: kumaran_5555
1 Replies

7. Programming

POSIX Thread Help

I want to create a program that creates 2 child process, and each of them creates 2 threads, and each thread prints its thread id. I0ve allread done that the outuput isn't the outuput i want. When a run the following comand "$./a.out | sort -u | wc -l" I have the folowing output 2 $: It should... (3 Replies)
Discussion started by: pharaoh
3 Replies

8. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

9. Shell Programming and Scripting

Is it possible to write write multiple cronjobs in shellscript??

Hi All, I need the answer of below question? 1) How to write multiple cronjobs in shellscript? Is there any way or we cant write in shellscript... Regards, Priyanka (2 Replies)
Discussion started by: pspriyanka
2 Replies

10. UNIX for Advanced & Expert Users

Change value for POSIX

Hi, I have a VM with following configration . 3.10.0-693.1.1.el7.x86_64 #1 SMP Thu Aug 3 08:15:31 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux My current POSIX is :-- Your environment variables take up 2011 bytes POSIX upper limit on argument length (this system): 2093093 POSIX smallest... (15 Replies)
Discussion started by: Abhayman
15 Replies
All times are GMT -4. The time now is 05:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy