最后一个 shell 命令的输出

发布于 2024-09-10 09:49:15 字数 245 浏览 3 评论 0原文

直到 3 小时的构建脚本进行到一半时,我才会记得我想在完成后在输出的开头看到一些内容。此时,我已经超出了终端中的行数,因此我无法向上滚动查看它(或者很难找到开头)。当然,我可以更好地存储我的输出,但我一直很好奇这是否可能。

在 Linux shell 中,是否可以返回最后一个命令的输出。我意识到我可以通过管道传输它或将输出发送到文件,但我的要求是在命令运行后检索该输出。

使用csh,但会听到任何shell。

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

装迷糊 2024-09-17 09:49:16

script(1) 正是您所需要的:

script
make
exit
vim typescript

script 程序将启动一个新的 shell,并将输入和输出保存到 typescript 文件中。完成后,只需使用 exit^D 关闭 shell。如果您不想启动新 shell 而只是运行构建,则可以使用:script -c

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>.

无语# 2024-09-17 09:49:16

如果再次运行该命令不会改变任何内容,您可以使用 !!访问历史堆栈的顶部以重新运行最后一个命令。在反引号中这样做将允许您使用结果。

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

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.

gt; find . -iname fileInUnknownLocationInHierarchy.txt
./something/buried/deep/where/you/dont/want/to/retype/fileInUnknownLocationInHierarchy.txt
gt; vim `!!`
独行侠 2024-09-17 09:49:16

对于某些使用光标寻址的命令来说,这可能会变得非常混乱。你的 vim 会话的输出是什么?或者一个稍微更理智的例子:您正在使用 wget 下载一个文件,它显示一个从 0% 到 100% 的百分比条。其输出是什么?

我认为您基本上想要当前行和上一个提示之间的任何内容的硬拷贝。这不是 shell 重定向的一个很好的例子,这基本上是终端会做的事情。您可以通过在屏幕中复制和粘贴来手动完成此操作,也许您可​​以使用其 exec 命令。 tmux 可能有类似的东西,你可以使用 Emacs 的 shell 模式做一些奇怪的事情,但是这是一个相当重量级的解决方案。 rxvt-unicode 有一个 perl 扩展

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

糖粟与秋泊 2024-09-17 09:49:16

怎么样使用替换呢?像这样:
<代码>

command1
command2
RESULT=`LASTCOMMAND`
echo $RESULT

How about using substitution. Like this:

command1
command2
RESULT=`LASTCOMMAND`
echo $RESULT

挖个坑埋了你 2024-09-17 09:49:16

至少在 bash 中,您可以使用 $? 查看最后一个命令的返回码。

就标准输出而言,我认为你做不到。

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.

玩心态 2024-09-17 09:49:16

如果您纯粹要查找最后执行的命令的返回代码,请使用 echo $?

当您想要了解最后一个命令是否成功执行时,这通常会有所帮助,并且您也可以将其模制到构建脚本中以用于通过/失败警报。

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.

谷夏 2024-09-17 09:49:16

将输出保存为变量?

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

save output as a variable?

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

我想你可以使用 tee 命令将输出放入 STDOUT 以及文件中,然后使用它。

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

懵少女 2024-09-17 09:49:16

这是您的终端的功能。您应该专注于创建一个无需记住执行任何操作的设置。一种可能的解决方案是在启用日志记录的情况下在登录脚本中启动“屏幕”(这并不容易正确执行)。

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.

抱着落日 2024-09-17 09:49:16

您始终可以在 emacs shell 窗口内运行该进程。它会保留一切。

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

无语# 2024-09-17 09:49:15

不会。程序的输出永远不会经过 shell 的处理。如果没有重定向,它会直接转到 TTY。通过重定向,它可以直接转到它所定向到的任何文件或管道。 shell 不知道进程发送到 stdout/stderr 的内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文