4

In this context I've encountered a situation where ed file < script would print ? and exit with error 2, while typing or pasting the commands from script one at a time worked fine. Even having a shell process pass the file through one line at a time with a sleep in between worked correctly. So it seems to me as though my version of ed, which is the one from MacOS 10.12, has problems if commands appear at inconvenient times.

  1. Why doesn't ed simply avoid reading stdin until it's ready to process whatever commands it gets? Or am I misinterpreting the situation?

  2. The error message ? isn't too helpful. In an interactive session I could type h afterwards to get an error message, according to the man page. But with input from a non-TTY it exits immediately. Is there some way to get at the error message in this case, too?

  3. Is this a known issue? If so, are there any suggested workarounds instead of passing the commands in slowly? What versions of ed would be affected?

3
  • 2
    Yeah, I had this too... with gnu ed. It only happened when piping (|) the commands to ed, it never happened when using here-docs or here-strings so my advice is to try using a here-doc to pass the commands to ed (e.g. ed -s somefile.sql <<IN...) Commented Dec 8, 2016 at 12:37
  • Item #2 really should be a separate question in its own right. Commented Dec 8, 2016 at 13:52
  • And don_crissti has just demonstrated why it should be a separate question in its own right. A real question and answer would cover something that don_crissti has missed and that is inappropriate in question comments. Commented Dec 8, 2016 at 22:14

1 Answer 1

2

The issue could be caused by carriage returns in the ed script. Copying and pasting a piece of text would not transfer these to the running ed process, but if they are in a script file, then I can understand that ed got confused.

GNU ed exits with an exit status of 2 "to indicate a corrupt or invalid input file", which supports the speculation that it may be a DOS formatted script file. However, the default ed on macOS is BSD ed and the exit codes are not properly documented in the manual. Looking at the (OpenBSD) source, it does look like most errors causing this exit code has to do with the reading of the script file though.

To remove the \r before each newline (will perform an in-place edit):

printf '%s\n' ',s/\r$//' 'wq' | ed -s file

The carriage returns would have got there from having written the script in a Windows editor, for example, or in any editor that saved the file as a DOS formatted text file.

Question 1: I'm pretty sure it does.

Question 2 has to do with the rather terse diagnostic messages that ed produces (?, most of the time). If you start your editing script with a call to the H function, any error will be verbose rather than the plain ?, as if you had typed h after an error. This does not help if there are carriage returns at the end of each line though, as these would prevent ed from recognizing H as a valid command.

Question 3: I have used GNU ed (1.14.2) and the BSD ed on OpenBSD, and I have not seen this issue caused by feeding ed commands "too quickly".

1
  • 1
    Reminds me of gnu.org/fun/jokes/ed-msg.html Note the consistent user interface and error reportage. Ed is generous enough to flag errors, yet prudent enough not to overwhelm the novice with verbosity Commented Aug 27, 2019 at 10:38

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.