[Techtalk] Can tcsh report 'command had no output'?

Akkana Peck akkana at shallowsky.com
Sun Oct 12 20:02:01 UTC 2008


Kelly Jones writes:
> Can I force tcsh to say "previous command returned empty stdout" or
> something? I often cut and paste shell output for my cow-orkers, and
> it's crucial to note when a command returns nothing.
> 
> Currently, I insert the information manually:
> 
> > ls | fgrep 'phrase'
> [no results]
> 
> but it'd be nice if tcsh had a setting to do this automatically?

I don't know of a way to do that, but you can add them automatically
afterward in an editor like vim. For instance, if your tcsh prompt is
"% ", type this in vim:

:%s/\(^% .*$\n\)\(% \)/\1[no results]\r\2/

and it will insert [no output] after every command that didn't
produce output.

How it works:

:	starts a command

%	do the following command on every line of this file

s/	start a global substitute command

\(	start a "capture group" -- you'll see what it does soon

^	match only patterns starting at the beginning of a line

% 	look for a % followed by a space (your prompt)

.*	after the prompt, match any other characters until...

$	the end of the line, after which...

\n	there should be a newline character

\)	end the capture group after the newline character

\(	start a second capture group

% 	look for another prompt. In other words, this whole
	expression will only match when a line starting with a prompt
	is followed immediately by another line starting with a prompt.

\)	end the second capture group

/	We're finally done with the mattern to match!
	Now we'll start the replacement pattern.

\1	Insert the full content of the first capture group
	(this is also called a "backreference" if you want
	to google for a more detailed explanation).
	So insert the whole first command up to the newline
	after it.

[no results]	After the newline, insert your desired string.

\r	insert a carriage return here (I thought this should be
	\n for a newline, but that made vim insert a null instead)

\2	insert the second capture group (that's just the second prompt)

/	end of the substitute pattern

Of course, if you have a different prompt, substitute it for "% ".
If you have a complicated prompt that includes time of day or
something, you'll have to use a slightly more complicated match
pattern to match it.

Fun with regular expressions!

	...Akkana


More information about the Techtalk mailing list