I have been doing binary experiments yet again and came across a superb piece of code...
I extracted a very small piece and re-wrote to suit my needs:-
Results:-
NOTE: Binary 0, (zero), has been accepted...
QUESTION:
I understand how "exec" is creating a file descriptor from "$1" but how does this affect "read"'s ability to accept binary 0, (zero), for manipulation by "printf"...
This is just a wild guess: i suppose read itself doesn't have anything to do with it. Probably the shell itself "cooks away" binary zeroes as it digests input and read only gets what was put through the shell. As the shell never gets to see the file (at least not as "input", that is, from stdin) read is served the whole story and not only parts of it.
I hope this helps.
bakunin
Moderator's Comments:
ahh, forgot: i move the thread over to the Experts forum. This is clearly not "Dummy" stuff but pretty advanced.
The -n 1 makes read only one character instead of the line (up to a \n character).
Further the -r tells to not discard leading space characters (that only fits for line mode).
IMHO the stuff can be simplified to
Surprisingly seems a little different!?
This User Gave Thanks to MadeInGermany For This Post:
QUESTION:
I understand how "exec" is creating a file descriptor from "$1" but how does this affect "read"'s ability to accept binary 0, (zero), for manipulation by "printf"...
exec has no effect whatsoever on the handling of the nullbyte.
Quote:
Originally Posted by wisecracker
NOTE: Binary 0, (zero), has been accepted...
No, it hasn't. That code is simply the beneficiary of serendipitous default behavior. The null byte is not in the variable, but printf fills any vacuums with zeroes/nullstrings, which in this case gives the correct result.
Arguments to non-string format specifiers are treated as C language constants, except that a leading plus or minus sign is allowed, and if the leading character is a single or double quote, the value is the ASCII value of the following character.
The format is reused as necessary to consume all of the arguments. If the format requires more arguments than are supplied, the extra format specifications behave as if a zero value or null string, as appropriate, had been supplied. The return value is zero on success, non-zero on failure.
In action:
In short, it's a lucky break (but I don't think it's an unsafe dependence).
The code that you posted will not work under all circumstances. read works with characters but in a hexdump, bytes are what matter. If the locale specifies a multibyte encoding, the results will be incorrect.
I observed such breakage on one of my machines with the following environment:
The solution is to ensure that a single-byte encoding is in effect.
I believe the following code is a simpler, more robust improvement.
Without LC_CTYPE=C in the script to override my system's default of LC_CTYPE=en_US.UTF-8:
The preceding result, with its string of incorrect zeroes, occurred on a Windows machine using Cygwin. In a Linux (Ubuntu) virtual machine, bytes 128-255 were also mangled, but instead of double zeroes the output consisted of 16-digit hexadecimal numbers.
Multibyte character issues are also a concern when generating the binary data with a locale-aware awk implementation (in this case, gawk):
The correct way:
Regards,
Alister
Last edited by alister; 10-14-2013 at 02:58 PM..
Reason: Changed LANG to LC_CTYPE
OK a few tests, OSX 10.7.5, default bash terminal:-
The first block is MadeInGermany's, the last three characters should be newlines not NULLs.
Second is alister's and only allows KB input much akin to INKEY$ on this tool.
Third is mine unchanged from the original post, last three characters correct.
Lastly a real hexdump command showing the last three characters.
Alister, the original code I found did include LANG=C but I removed it. Thanks for the info...
The test binary code is attached as a "txt" file...
EDIT:
I forgot to add that I want to use the bash builtins only and not rely on transient commands like od,
xxd or hexdump to get my required results purely as an exercise to learn more about the limitations
of bash, (or any shell for that matter), scripting... You guys are a godsend to people like me and
certainly have put me on the straight and narrow...
Hi 2 all,
i have had AIX 7.2
:/# /usr/IBMAHS/bin/apachectl -v
Server version: Apache/2.4.12 (Unix)
Server built: May 25 2015 04:58:27
:/#:/# /usr/IBMAHS/bin/apachectl -M
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
mpm_worker_module (static)
... (3 Replies)
Hello.
System : opensuse leap 42.3
I have a bash script that build a text file.
I would like the last command doing :
print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt
where :
print_cmd ::= some printing... (1 Reply)
How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address
and column 3 contains “cc” e-mail address to include with same email.
Sample input file, email.txt
Below is an sample code where... (2 Replies)
I want to print any matching IP addresse in List1 with List 2;
List 1
List of IP addresses;
161.85.58.210
250.57.15.129
217.23.162.249
74.76.129.101
30.221.177.237
3.147.200.59
170.58.142.64
127.65.109.33
150.167.242.146
223.3.20.186
25.181.180.99
2.55.199.32 (3 Replies)
Hi,
I have line in input file as below:
3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL
My expected output for line in the file must be :
"1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL"
Can someone... (7 Replies)
Hallo,
i need a Prompting read in my script:
read -p "Enter your command: " command
But i always get this Error:
-p: is not an identifier
When I run these in c-shell i get this error
/usr/bin/read: read: bad option(s)
How can I use a Prompt in the read command? (9 Replies)
Hi Friends,
Can any of you explain me about the below line of code?
mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`
Im not able to understand, what exactly it is doing :confused:
Any help would be useful for me.
Lokesha (4 Replies)
Hi
I have installed solaris 10 on an intel machine. Logged in as root. In CDE, i open terminal session, type login alex (normal user account) and password and i get this message
No utpmx entry: you must exec "login" from lowest level "shell" :confused:
What i want is: open various... (0 Replies)
Dears,
I've written a script which allows me to send mails in different formats with different attaches. Now I still want to add a feature to this script. My users would like to be able to receive a "read" or "delivered" receipt for their mails.
The script send mails on behalve of an specific... (1 Reply)