Translate

Archives

Standard Streams Buffering

STDIO (Standard In/Out) streams are either: Fully buffered – the buffer is not flushed until the buffer is full. Line buffered – the buffer is flushed when a complete line is in the buffer or the buffer is full. Unbuffered – the buffer is flushed whenever there is data in the buffer. This is usually at the end of a call to a stdio function such as putc or printf. By default, stderr is not fully buffered and stdin and stdout are not fully buffered unless the system can determine that the stream is not connected to a tty device

Strong and Weak Quoting in Shell Scripts

I do now know when the terms “strong quoting” and “weak quoting” entered Unix (or Linux) shell lexicon. Despite having been the maintainer of various shells for a number of years while working for an operating system vendor, I have only come across this particular terminology over the last few years. Strong and weak quoting is not mentioned any of the shell man pages that I checked. Typically quoting is described in a manner similar to that of the ksh88 manpage: “Second, a single quote (‘) quotes everything up to the next single quote (this can span lines). Third, adouble

Create a Repeated String

This question is often asked in the various Unix and Linux forums. For example, suppose you want to create a string of 50 asterisks other than str=”***********************************” Here are some of the ways I have come across over the years. perl -e ‘print “*”x50’ ruby -e ‘puts “*”*50’ awk ‘BEGIN { while (a++<50) s=s “x”; print s }’ printf ‘%*s’ “50” ‘ ‘ | tr ‘ ‘ “*” echo “*****” | sed ‘s/.*/&&&&&&&&&&/’ The following works for gawk but not oawk. echo | awk NF=51 OFS=*

Sprintf Portability and Leading Zeros

On some platforms, code such as the following can be used to output a string with leading zeros left padded to a length of 4. sprintf(*str, “%04s”, *pstr); This works on AIX for example. However if the same code is compiled on Linux using gcc, it outputs leading spaces, padded to a length of 4, instead of leading zeros. There is no simple way to fix this behavior. This particular usage of leading zero padding with the string format is explicitly undefined in the various C standards. What is outputted depends on a platform’s libraries rather than the compiler. As

The Strange Birth and Long Life of Unix

The December 2011 issue of the IEEE Spectum magazine has an interesting article on Unix by Warren Toomey. It includes some material on the origins and evolution of Unix which I have never come across before. End runs around AT&T’s lawyers indeed became the norm—even at Bell Labs. For example, between the release of the sixth edition of Unix in 1975 and the seventh edition in 1979, Thompson collected dozens of important bug fixes to the system, coming both from within and outside of Bell Labs. He wanted these to filter out to the existing Unix user base, but the