开发者

Output of last shell command

Not until midway through a 3 hour build script, I'll remember that I want to see something at the beginning of the output after it's done. At this point I've exceeded the number of lines in my terminal so I can't scroll up to see it (or the beginning is hard to find). Of course I can be better about storing my output, but I've always been curious if this were possible.

In a linux shell, is it开发者_C百科 possible to return the output of the last command. I realize I could have piped it or sent the output to a file, but my requirement is to retrieve that output after the command has been run.

Using csh, but would hear about any shell.


No. The output of a program never passes through the shell's hands. Without redirection, it goes straight to the TTY. With redirection, it goes straight to whatever file or pipe it was directed to. The shell has no idea what the process sent to stdout/stderr.


script(1) is exactly what you need:

script
make
exit
vim typescript

The script program will start a new shell and save input and output to the typescript file. When you're done, just close the shell with exit or ^D. If you'd rather not start a new shell but just run your build, you can use: script -c <command>.


If running the command again won't change anything, you can use !! to access the top of the history stack to rerun the last command. Doing so in backticks will allow you to use the result.

$> find . -iname fileInUnknownLocationInHierarchy.txt
./something/buried/deep/where/you/dont/want/to/retype/fileInUnknownLocationInHierarchy.txt
$> vim `!!`


This could get very messy with certain commands that use cursor addressing. What's the output of your vim session? Or a slightly more sane example: You're downloading a file with wget, and it shows a percentage bar going from 0% to 100%. What's the output of that?

I think you basically want a hardcopy of anything between your current line and the previous prompt. That's not something shell redirection would be a good example for, that's basically something a terminal would do. You could do it manually by copying and pasting in screen, and maybe you could automate it with its exec command. tmux might have something similar, and you can do weird things with the shell mode(s) of Emacs, but that's a rather heavyweight solution. rxvt-unicode has a perl extension…


How about using substitution. Like this:

command1
command2
RESULT=`LASTCOMMAND`
echo $RESULT


You can see the return code of the last command, at least in bash, with $?.

As far stdout goes, I don't think you can.


If you're looking purely for the return code of the last executed command, use echo $?.

This usually helps when you want to find whether your last command executed successfully and you could also mould this into your build script for pass/fail alerts.


save output as a variable?

bash: Output=$(ls); echo $Output


My be you can use tee command to put the output to STDOUT as well to a file and then use it.


This is a function of your terminal. You should focus on creating a setup where you don't have to remember to do anything. One possible solution is to launch "screen" in your login script (which is not so easy to get right) with logging enabled.


You can always run the process inside an emacs shell window. It will keep everything.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜