Sponsored Content
Full Discussion: Binary write POSIX-ly.
Top Forums UNIX for Advanced & Expert Users Binary write POSIX-ly. Post 302980457 by Don Cragun on Sunday 28th of August 2016 07:14:30 AM
Old 08-28-2016
Quote:
Originally Posted by wisecracker
Hi Don...

Where would I be without you...

This is at least an order of magnitude faster than my test code.

Works perfectly in dash, except for your typo for hexdump near the end of the script.

Off to try some big binary files now. I will keep you informed over the next few days.

Shell Check makes minor warnings on line 20...

printf '%s\n' $split_octal | while read octal

...to be:-

printf '%s\n' "$split_octal" | while read -r octal

Bit I don't see why it needs changing as the file is always going to be multiple 3 digit octal values only, along with newlines of course, so I have left as is...

Thanks a lot I have something to get my teeth into now.

If only arrays were allowed...
Hi wisecracker,
Thank you for pointing out the iexdump (which has now been fixed in my post). I am not sure how that happened; I have hexdump in the code I copied and tried to paste.

It is good that you left the expansion $split_octal as is. With quotes around that expansion, the function won't work.

Quote:
Originally Posted by RudiC
Isn't that approach a bit overcomplicated? Why not save some lines of code, some variables, and reduce the amount of data shoved to and fro?
Code:
binary () 
{ 
    printf '%s\n' $1 | while read octal; do
        while [ "${#octal}" -gt 0 ]; do
            subtx=${octal%"${octal#???}"};
            octal=${octal#"${subtx}"};
            printf '%b' "\0$subtx";
        done;
    done
}

Call it like binary "$split_octal".
Yes. This is better. I hadn't noticed that the function was being called with an operand until after I had posted the script. One might also consider changing it to:
Code:
binary() { 
    while read octal
    do
        while [ "${#octal}" -gt 0 ]
        do
            subtx=${octal%"${octal#???}"}
            octal=${octal#"${subtx}"}
            printf '%b' "\0$subtx"
        done
    done
}

and invoke it with:
Code:
printf '%s\n' $split_octal | binary > output_file

or with:
Code:
binary < input_file > output_file

where input_file contains text similar to the string assigned to split_octal without the quotes (which would be handy if the data being processed is sometimes in a separate file and sometimes in a shell variable).
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 09:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy