Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过xterm和mpg123进行bash重定向_Bash_Redirect_Xterm_Subshell - Fatal编程技术网

通过xterm和mpg123进行bash重定向

通过xterm和mpg123进行bash重定向,bash,redirect,xterm,subshell,Bash,Redirect,Xterm,Subshell,这是我的.fluxbox/startup文件的一部分 (a=($(grep "^1 " $HOME/Documents/4.n3u|awk '{print "/home/g" $2}'|sort -R|head -20)); \ xterm -e mpg123 -C ${a[@]} &>$HOME/Documents/mpg123.dat &) 如前所述,重定向失败,所有此类输出将显示在xterm中。xterm的手册页部分内容如下: -e program

这是我的
.fluxbox/startup
文件的一部分

(a=($(grep "^1 " $HOME/Documents/4.n3u|awk '{print "/home/g" $2}'|sort -R|head -20)); \  
  xterm -e mpg123 -C ${a[@]} &>$HOME/Documents/mpg123.dat &)  
如前所述,重定向失败,所有此类输出将显示在
xterm
中。
xterm
的手册页部分内容如下:

   -e program [ arguments ... ]
           This  option  specifies the program (and its command line argu‐
           ments) to be run in the xterm window.  It also sets the  window
           title  and  icon  name  to be the basename of the program being
           executed if neither -T nor -n are given on  the  command  line.
           This must be the last option on the command line.
mpg123
根据需要播放数组a的内容,并且可以通过键盘控制
-C
选项指定的内容,但是
xterm
似乎会阻碍重定向到文件。在这种情况下,重定向是可能的吗


或者,我可以在不使用
xterm
来包含
mpg123
的情况下运行它,在这种情况下,我会得到重定向,但无法通过键盘控制
mpg123
,因为它在一些后台子shell中运行,没有与键盘的连接。有没有办法建立这种连接?

您已经重定向了
xterm
进程的stdout和stderr,但是xterm通常不会在自己的stdout和stderr上打印任何内容。只有与xterm本身相关的错误才会出现(比如它意外地失去了与X服务器的连接)

xterm创建一个tty并运行子进程(
-e
命令或shell),并将stdin、stdout和stderr附加到该tty。您需要将重定向放在
-e
中,以便在子进程中应用重定向,如下所示:

xterm -e 'your command > whatever'
第二次尝试

要保持
${a[@]}
参数列表的完整性,同时使用shell重定向操作符,您必须使用
-c
显式调用shell。像这样:

xterm -e sh -c 'your command "$@" > whatever' dummy "${a[@]}"

实际上,在启动这个线程之前,我尝试了双引号引用xterm-e参数。为此,它生成一个xterm,其中包含“xterm:cannotexecvpmpg123-C/home/g/Music/Dvrak-Humoresque-GabrielChodosPiano.mp3:没有这样的文件或目录”形式的消息。当使用单引号时,它会生成一个xterm,其标题栏显示为“mpg123.dat”,但allOh没有其他活动,我看到了更大的问题。您希望将参数数组传递给命令(需要多arg execvp ish形式的
-e
),但也希望应用shell重定向(需要单arg system ish形式的
-e
)。实现这两个目标并不容易。我想我最好尝试使用文件而不是数组。非常感谢您的帮助。不,这是可行的,只需要使用
sh-c
$@
进行一些技巧。试试更新后的答案。效果很好:grep“^1”$HOME/Documents/4.n3u | awk'{print”/HOME/g“$2}”| sort-R | head-20>$HOME/Documents/rand.m3u;mpg123-C--列出$HOME/Documents/rand.m3u&>$HOME/Documents/mpg123.dat