简单地将命令的输出分叉并重定向到 /dev/null
我经常从 shell(在我的例子中是 Bash)执行我想立即分叉并且我想忽略其输出的命令。事实上,我经常创建一个脚本(silent
)来执行此操作:
#!/bin/bash
$@ &> /dev/null &
然后我可以运行,例如
silent inkscape myfile.svg
,我的终端不会被我刚刚分叉的进程的调试输出污染。
我有两个问题:
是否有一种“官方”方式可以做到这一点?,即更短但相当于
> /dev/null &
?如果没有,有没有办法可以在我的
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:
Is there an "official" way of doing this?, i.e. something shorter but equivalent to
&> /dev/null &
?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 typedsilent inksc
, I'd like bash to auto-complete my command tosilent inkscape
when I press[tab]
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
旁白:可能想要
exec "$@" &> /dev/null &
在silent
脚本中,使其丢弃子 shell,并且"$@"
周围的引号将保留空格以免妨碍。至于#2:
complete -F _commandsilent
应该做你想要的事情。 (我将该脚本的版本称为launch
,并在我的.bash_profile
中有complete -F launch
)aside: probably want to
exec "$@" &> /dev/null &
in yoursilent
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 scriptlaunch
and havecomplete -F launch
in my.bash_profile
)看起来 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.
这是我用来从 xterm 等启动交互式(且健谈的)X 应用程序的小脚本。
没有输出,如果终端退出也不会死,但万一出现问题,/tmp 中会有所有输出的日志
。不想要日志只需使用 /dev/null 即可。
如果您对脚本过敏,也可以通过函数工作。
Here's a little script I use for launching interactive (and chatty) X apps from e.g. an xterm
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.
也许您可以“重新绑定”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?