7 bash shell user-interface posix stdout
每隔一段时间我就要从终端会话中启动一个GUI程序来做某事.它通常是Chrome显示一些HTML文件是一些相似的任务.然而,这些程序会在整个地方发出警告,编写任何东西实际上都变得荒谬,所以我一直想重定向stderr/ stdout到/dev/null.
虽然($PROGRAM &) &>/dev/null看起来没问题但我决定为它创建一个简单的Bash函数,所以我不必每次都重复自己.
所以现在我的解决方案是这样的:
#
# silly little function
#
gui ()
{
if [ $# -gt 0 ] ; then
($@ &) &>/dev/null
else
echo "missing argument"
fi
}
#
# silly little example
#
alias google-chrome='gui google-chrome'
Run Code Online (Sandbox Code Playgroud)
所以我想知道的是:
在提出这些问题时,我想指出你的策略和解决方案可能与我的大不相同.将输出重定向到/ dev/null并对其进行别名是我所知道的唯一方法,但可能有更完全不同的方式更有效.
因此这个问题:-)
正如其他人在评论中指出的那样,我认为真正的问题是在命令行上区分 gui 与非 gui 应用程序的方法。因此,我能想到的最简洁的方法是将脚本的这一部分放入:
#!/bin/bash
if [ $# -gt 0 ] ; then
($@ &) &>/dev/null
else
echo "missing argument"
fi
Run Code Online (Sandbox Code Playgroud)
到一个名为 的文件中gui,然后chmod +x将其放入您的~/bin/(确保~/bin位于您的$PATH)中。现在您可以使用以下命令启动 GUI 应用程序:
`gui google-chrome`
Run Code Online (Sandbox Code Playgroud)
在提示符上。
或者,您可以执行上述操作,然后使用bind:
bind 'RETURN: "\e[1~gui \e[4~\n"'
Run Code Online (Sandbox Code Playgroud)
这将允许您执行以下操作:
google-chrome
Run Code Online (Sandbox Code Playgroud)
在提示符上,它会自动附加gui在前面google-chrome
或者,您可以将上述操作绑定到F12而不是RETURNwith
bind '"\e[24~": "\e[1~gui \e[4~\n"'
Run Code Online (Sandbox Code Playgroud)
区分您想要使用 GUI 启动的内容与gui非 GUI 启动的内容。
这些替代方案为您提供了摆脱无尽别名的方法;混合:
gui你的~/bin/, 和guito的绑定使用F12如上所示似乎是最理想的(尽管很hacky)解决方案。
更新 - @Enno Weichert 的最终解决方案:
完善这个解决方案......
这将处理别名(尽管以一种有点古怪的方式)和不同的转义编码(以更实用的方式而不是详尽的方式)。
把这个放进去$(HOME)/bin/quiet
#!/bin/bash -i
if [ $# -gt 0 ] ; then
# Expand if $1 is an alias
if [ $(alias -p | awk -F "[ =]" '{print $2}' | grep -x $1) > 0 ] ; then
set -- $(alias $1 | awk -F "['']" '{print $2}') "${@:2}"
fi
($@ &) &>/dev/null
else
echo "missing argument"
fi
Run Code Online (Sandbox Code Playgroud)
而这在$(HOME)/.inputrc
#
# Bind prepend `quiet ` to [ALT][RETURN]
#
# The condition is of limited use actually but serves to seperate
# TTY instances from Gnome Terminal instances for me.
# There might very well be other VT emulators that ID as `xterm`
# but use totally different escape codes!
#
$if $term=xterm
"\e\C-j": "\eOHquiet \eOF\n"
$else
"\e\C-m": "\e[1~quiet \e[4~\n"
$endif
Run Code Online (Sandbox Code Playgroud)