简单地将命令的输出分叉并重定向到 /dev/null

发布于 2024-12-20 06:10:19 字数 560 浏览 1 评论 0原文

我经常从 shell(在我的例子中是 Bash)执行我想立即分叉并且我想忽略其输出的命令。事实上,我经常创建一个脚本(silent)来执行此操作:

#!/bin/bash
$@ &> /dev/null &

然后我可以运行,例如

silent inkscape myfile.svg

,我的终端不会被我刚刚分叉的进程的调试输出污染。

我有两个问题:

  1. 是否有一种“官方”方式可以做到这一点?,即更短但相当于 > /dev/null &

  2. 如果没有,有没有办法可以在我的silent命令之后使制表符完成工作,就好像它不存在一样?举个例子,在我输入 silent inksc 后,当我按 [tab] 时,我希望 bash 自动完成对 silent inkscape 的命令.

I frequently execute from a shell (in my case Bash) commands that I want to fork immediately and whose output I want to ignore. So frequently in fact that I created a script (silent) to do it:

#!/bin/bash
$@ &> /dev/null &

I can then run, e.g.

silent inkscape myfile.svg

and my terminal will not be polluted by the debug output of the process I just forked.

I have two questions:

  1. Is there an "official" way of doing this?, i.e. something shorter but equivalent to &> /dev/null & ?

  2. If not, is there a way I can make tab-completion work after my silent command as if it weren't there ? To give an example, after I've typed silent inksc, I'd like bash to auto-complete my command to silent inkscape when I press [tab].

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

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

发布评论

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

评论(4

云柯 2024-12-27 06:10:19

旁白:可能想要exec "$@" &> /dev/null &silent 脚本中,使其丢弃子 shell,并且 "$@" 周围的引号将保留空格以免妨碍。

至于#2:complete -F _commandsilent应该做你想要的事情。 (我将该脚本的版本称为 launch,并在我的 .bash_profile 中有 complete -F launch

aside: probably want to exec "$@" &> /dev/null & in your silent script, to cause it to discard the sub-shell, and the quotes around "$@" will keep spaces from getting in the way.

As for #2: complete -F _command silent should do something like what you want. (I call my version of that script launch and have complete -F launch in my .bash_profile)

无声情话 2024-12-27 06:10:19

看起来 nohup 或多或少做了你想要的事情。制表符补全问题是因为 bash 认为您正在尝试补全文件名作为脚本的参数,而它的补全规则知道 nohup 将命令作为其第一个参数。

Nohup 将 stout 和 stderr 重定向到 nohup.out,并且如果您的 shell 退出,也会使命令保持运行状态。

It looks like nohup does more or less what you want. The tab-completion problem is because bash thinks that you are trying to complete a filename as an argument to the script, whereas its completion rules know that nohup takes a command as its first argument.

Nohup redirects stout and stderr to nohup.out and will also leave the command running if your shell exits.

赠佳期 2024-12-27 06:10:19

这是我用来从 xterm 等启动交互式(且健谈的)X 应用程序的小脚本。

#!/bin/bash
exe="$1"
shift
"$exe" "$@" 2>/tmp/$."$exe".err 1>&2 & disown $!

没有输出,如果终端退出也不会死,但万一出现问题,/tmp 中会有所有输出的日志

。不想要日志只需使用 /dev/null 即可。

如果您对脚本过敏,也可以通过函数工作。

Here's a little script I use for launching interactive (and chatty) X apps from e.g. an xterm

#!/bin/bash
exe="$1"
shift
"$exe" "$@" 2>/tmp/$."$exe".err 1>&2 & disown $!

No output, won't die if the terminal exits, but in case something goes wrong there's a log of all output in /tmp

If you don't want the log just use /dev/null instead.

Also will work from a function if you're script-alergic.

玩套路吗 2024-12-27 06:10:19

也许您可以“重新绑定”tab 键?显示了超级用户 Stackoverflow 上使用 Enter 键的示例。这是正确的想法吗?

Perhaps if you could 'rebind' the tab key? An example on superuser Stackoverflow with the enter key is shown. Is this the right idea?

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