0

Here is a part of my .fluxbox/startup file

(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 &)  

As written, the redirection fails, all such output appearing in the xterm instead. The man page for xterm reads, in part,

   -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 plays the content of array a as desired, and can be controlled through the keyboard as option -C specifies, but xterm seems to frustrate the redirect to file. Is that redirection possible in this context?

Alternatively, I can run it without the xterm to contain mpg123, in which case I get the redirect, but cannot control mpg123 thru the keyboard because it is running in some background subshell with no connections to the keyboard. Is there any way to establish that connection?

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
user985675
  • 293
  • 3
  • 10

1 Answers1

1

You have redirected the stdout and stderr of the xterm process, but xterm does not normally print anything on its own stdout and stderr. The only things that would show up there would be errors related to xterm itself (like if it unexpectedly lost its connection to the X server).

xterm creates a tty and runs the child process (-e command or a shell) with stdin, stdout, and stderr attached to that tty. You need to put the redirection inside the -e to have it apply in the child process, like this:

xterm -e 'your command > whatever'

SECOND ATTEMPT

To keep the ${a[@]} argument list intact but also use the shell redirection operator, you're going to have to explicitly invoke a shell with -c. Like this:

xterm -e sh -c 'your command "$@" > whatever' dummy "${a[@]}"
  • Actually, I tried double-quoting the xterm -e parameter before starting this thread. For that it produces an xterm containing the message of the form 'xterm: Can't execvp mpg123 -C /home/g/Music/Dvrak-Humoresque-GabrielChodosPiano.mp3: No such file or directory'. When single quotes are used it produces an xterm whose title bar reads 'mpg123.dat', but no other activity at all – user985675 Jun 23 '13 at 19:44
  • Oh, I see the bigger problem. You want to pass an argument array to the command (which requires the multi-arg execvp-ish form of `-e`) but you also want to apply shell redirection (which requires the single-arg system-ish form of `-e`). It won't be easy to make both happen. –  Jun 23 '13 at 19:48
  • Guess I'd better try using a file instead of an array then. Thanks much for your help. – user985675 Jun 23 '13 at 19:59
  • No, it's doable, it just requires some trickery with `sh -c` and `$@`. Try the updated answer. –  Jun 23 '13 at 20:03
  • Works fine like so: grep "^1 " $HOME/Documents/4.n3u|awk '{print "/home/g" $2}'|sort -R|head -20 > $HOME/Documents/rand.m3u; mpg123 -C --list $HOME/Documents/rand.m3u &>$HOME/Documents/mpg123.dat – user985675 Jun 23 '13 at 20:09
  • As you wish, but that's running away from the problem instead of solving it (or even trying the solution I provided because it was 2 minutes too late?) –  Jun 23 '13 at 20:15
  • Many thanks, your modified answer works fine, so I will use it rather than the file version – user985675 Jun 23 '13 at 20:30
  • Please don't take offense. I assumed originally that by "It won't be easy..." you meant that it wouldn't work the way I was trying, so I switched to using a file, didn't see your modified answer till after I posted the file method. – user985675 Jun 23 '13 at 20:38