4

I often run searches on my source code repo like this:

ack -l "foo bar" .

The searches can take 10-60 seconds and produce output like this:

path/to/file1       
path/to/file2
path/to/file3

I frequently use this alias to open all of the files found in vim:

alias go='gvim `fc -s` -p'

This works OK, but fc -s reruns the previous command, which can take seconds or minutes.


It would be very useful to append something like this to every command I run interactively:

| tee /tmp/lastCommand to all commands.  

This way, if the output reveals itself to be useful, then I can do something else to it.

Example usage:

Type:

find . -type f 

Which executes the following:

find . -type f | tee $lastOutLocation # Where lastOutputLocation=/tmp/lastOutput

You could then use something like this, to filter the previous command:

lastOut | grep "SomeString" # Where lastOut is an alias to cat $lastOutputLocation

Is there a mechanism I can use to do something like this? I don't think that aliases expose this kind of behaviour.

Potential Hurtles:

  • Getting something that works with compound commands: echo 'baz' ; echo 'bar'
  • Figure out how to modify the command the user typed before running it
  • Ignore things like interactive input. Don't want to capture password prompts

Possible solutions, I am currently exploring:

  • The simplest solution would be to wrap it up in a shell script (like run) that would call the quoted command you want to run. Then you'd run 'find . -type f' and the script would execute find . -type f | tee $lastOutLocation. Note that I used quotes to allow multiple commands as you mentioned. – Julie Pelletier Jan 13 '17 at 17:36
  • 2
    consider the typescript (script) command – Jeff Schaller Jan 13 '17 at 17:38
  • The biggest hurdle would be programs that want access to the terminal and not just to write to stdout. Piping to tee (or anything else) wouldn't be compatible with this. – Gilles Jan 14 '17 at 15:28
1

It would be very useful to append something like this to every command I run interactively:

| tee /tmp/lastCommand to all commands.  

A potential start might be to use ~/.inputrc to bind a key. (That's the configuration file for the GNU Readline library.) For example, using the letter, o, as a mnemonic for output ...

Control-o: " | tee /tmp/lastCommand"

One could bind to the Return/Enter key, but that action would have unintended effects.

-1

If I understood you want to redirect the output of your commands to a file, that can be done by:

#command > output_file
Or also #command | output_file

The first one changes the output channel so everything it would write to the terminal it'll write to the output_file, and the second one pipe both programs...

  • command | foo will only work if foo is something that can be executed, and it will not write to foo. – dhag Jan 19 '17 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.