Thursday 18 July 2019

readline with bash's read -e

Want to use bash's read -e for full editing and readline support, but without bash completion or exposing the bash history?

bind -f /dev/stdin <<<"set disable-completion on"
HISTSIZE=0 HISTIGNORE="*" HISTFILE=/dev/null read -e ...

Thursday 6 June 2019

synchronous pipe based task monitoring

I want a process to monitor another and know when it quits.

An obvious way if I control the launch is to tie them together with a pipe and they can detect when the pipe closes.

Maybe the other process would launch many further processes, all inheriting the pipe-fd, which I want to avoid.

So clearly the launcher needs to hold the pipe-fd but not share it (close-on-exec, don't fork too much), and then wait in the usual way for the launched process to quit, and then close the pipe.

Here's a bash incantation lifetime_fd which runs a simple list of arguments and can share an fd with another process through process substitution.

# just run a simple list, without affecting $?
just() {
  set -- $? "$@"
  "${@:2}"
  return $1
}

lifetime_fd() {
  set -- $_ "$@" ; eval "$1>&-" '"${@:2}"' ; just eval exec "$1>&-"
}

So if you want to run command fooly barly bazly and link it to the lame read && echo done then this will do the trick

lifetime_fd fooly barly bazly {_}> >( read && echo done )

So to attach a pipe descriptor to a sub-process, it is clear to use the trailing invocation {_}> >( sub-process ) which will attach stdin of the sub-process to a file descriptor to be stored in $_ which is managed in lifetime_fd

The variable $_ is used to avoid messing with any other variables. $_ is constantly adjusted and should do no harm if we abuse it; but as it is constantly adjusted, the first thing we do in lifetime_fd is to save it.

We don't use a local variable in case of a name clash that affects something else, so we store it as $1

We then run "$@" (or "${@:2}" as it now would be) but with the fd closed, so that it is not inherited.

We then close the fd while preserving the exit code.

You can invoke it in a pipeline like this:

get_report_request | lifetime_fd get_report {_}> >( monitor ) | send_report

An illustrative example of monitor (which reads until eof), might be:

monitor() {
  while read -t 1 || test $? = 142 # 142 is timeout code
  do echo -n '*'
  done
}

which displays a star every second until stdin closes; by continually waiting up to 1 second to fail to read anything from stdin (until it closes, having a different exit code), and displays a star.

Of course it might read other data too.... if you can send it...

Monday 25 February 2019

The Old Summer House


The Old Summer House

Sam Liddicott
The Fat Controller didn't seem at all bothered when he heard that Diesel had lost all the jobi wood into the sea. Diesel didn't seem too worried either, though a trifle scared at nearly falling into the sea himself.
Instead of salvaging what he could, the Fat Controller declared "More jobi wood must be found", "the search and rescue will be delayed".
My suspicious were not aroused, this waste of resources was typical on the Island of Sodor railway.
But my suspicions were aroused when Rocky the large breakdown crane happened to mention one morning that he was surprised just how deep the water was where Diesel lost the jobi wood. Rocky had come to the island to help put Gordon back on the rails, and wasn't involved in the jobi incident, and it was Thomas that had rescued Diesel. So what did Rocky know about it?
Later that year, after the search and rescue centre was complete, the Fat Controller invited everyone to a summer garden party, and to formally open his new summer house and gazebo.
I was there of course, as were most of the engines. Goodness knows how much it cost to lay the siding so that they could attend. It's hardly putting the shareholders money to good use.
I examined the summer house and gazebo. They were made-to-measure, and done very nicely, but the wood was rather cheaper than I expected, and not properly finished. "It must be painted with preservative" said the fat controller, "but there was not time to do it before the party". I carved my initials behind one the posts at the back. They would remember that "I woz ere".
The works manager was very pleased as the fat controller had promised him the old summer house and gazebo, if he would come and collect it. I went with him to help load the disassembled pieces from the front of the Fat Controllers driveway, and onto the back of a lorry, and then helped to assemble it.
"It's not very large!" said the works manager after we had finished assembling it a few days later, "but it will make a good shed for my model railway, though it is in worse condition than I expected."
As we sat looking at it with out drinks, I got to thinking about the poor use of shareholders money in laying a siding to the Fat Controllers house, and I decided to pay another visit.
As I walked up the driveway, I saw Harvey on the new siding by the Fat Controllers summer house, tearing up the track behind him.
The Fat Controller's wife came out showed me into the garden where the Fat Controller sat under the gazebo with the company auditor enjoying some drinks together, and I was cordially invited to join them.
"Our Fat Controller has rather carelessly cut himself carving some wood!" explained the auditor, pointing towards the bandaged.
The newness of the wound was evident by the bright red mark leaking through, whose colour I thought matched a bloodstain on the floor at the back of the post just where I could just see my initials carved, if I lent back in my chair.
After some talk about assets and amortisations which I could not understand, the Fat Controller excused himself, and so the auditor and I left.
"That's rather a nice summer house," I remarked, and a lovely colour.
"Yes," says the auditor, "it required the treatment of a special preservative which I understand affects it in a remarkable way, and imparts a uniquely heavy feel to the wood. It should last a long time"
And then, after a thought he added as we reached a lorry at the end of the driveway, "the Fat Controller has given me his old summer house, and I understand that you have experience assembling this sort of thing and may be able to help".
One of the posts had my initials carved on it. I noticed this as the auditor slapped the tarpaulin and said "You can fit a lot of bad boys on this one"
I noped out of there.