Terminal redirection?

Hi, I'm working on my first real shell script and I've hit a wall. My script opens commands in new terminal windows like this..

gnome-terminal -x bash -l -c "ls -l; bash"

My question is, how can I get the information that command outputs? I know how to redirect output to a file with > or even | tee but my problem is with terminals that are running commands that keep going. The output files just seem to stay empty. I was looking for some sort of way to pipe the output back to my script but I don't even know if that can be done. Anyhow, I hope this was clear enough for someone smarter than me to understand haha.

Well, there are sorts of solutions in expect, screens, ssh. I logged in program may write the terminal with stdout (buffered), stderr (unbuffered) or /dev/tty (the terminal, not usually redirected). You want to capture the output on the latter. Sometimes it is easier to put the app data on a log file and then 'tail +0f' the log file to the screen.

I would be happy to redirect it to a file but it doesn't seem to work when the program being run has to update data in the terminal output. Works fine for anything that just outputs it's data and ends though.

Well, if it is addressing the screen, that is tough to map to a file. Bash does the occasional backspace or carriage return to move the cursor, but that can be recorded even if it is hard to present. What are you trying to log?

It's nothing specific yet but what I've been using for testing is just a little program that that has a list of the last 10 coordinates returned by a GPS. instead of just scrolling the list it moves everything up 1 when it adds a new one to the list. as far as I can tell that's why it's not working the same.

You can use truss/tusc/strace to see what it calls to write the data, and what data it is writing.

Is there an easy sample you can post here or is it something I should just go and do some research on? Thanks for the help by the way.

Here, I am tracing an xterm running bash to do pwd and to close window:

$ (
truss -faelo /tmp/bash.tr xterm -e bash -i &
sleep 1
tail +0f /tmp/bash.tr
)
[1]     17913
NOTE: /usr/bin/X11/xterm: losing setuid/setgid privileges when tracing
17925/29482:    execve(0x7b0f5bf0, 0x7b0f54c0, 0x7b0f54d4)    [entry]
                              argv[0] @ 0x7b0f0020: "xterm"
                              argv[1] @ 0x7b0f0026: "-e"
                              argv[2] @ 0x7b0f0029: "bash"
                              argv[3] @ 0x7b0f002e: "-i"
 .
 .
 .
18907/640:      write(2, "$   ", 2)                             = 2
18906/637:      select(7, 0x4000de90, 0x4000de8c, NULL, NULL)   = 1
18907/640:      sigprocmask(SIG_SETMASK, 0x4001a640, NULL)      = 0
18906/637:      read(6, "$   ", 4096)                           = 2
18907/640:      sigsetstatemask(0x17, NULL, 1073852280)         = 0
18906/637:      read(6, 0x4000fb80, 4096)                       = 0
18906/637:      write(5, "L 01\00502\0\01502\0\010\011\01 ".., 60) = 60
18906/637:      select(7, 0x4000de90, 0x4000de8c, NULL, NULL)   [sleeping]
18906/637:      select(7, 0x4000de90, 0x4000de8c, NULL, NULL)   = 1
18906/637:      ioctl(5, FIONREAD, 0x7b0f5ce0)                  = 0
18906/637:      read(5, "\b04\0{ j 85! I \0\0\0& 02\0\00e".., 32) = 32
18906/637:      select(7, 0x4000de90, 0x4000de8c, NULL, NULL)   = 1
18906/637:      ioctl(5, FIONREAD, 0x7b0f5ce0)                  = 0
18906/637:      read(5, "\n03\0{ 02\0\00e0186a2| ffffffff".., 32) = 32
18906/637:      write(5, "L 01\00502\0\01502\0\010\0# \01 ".., 52) = 52
18906/637:      select(7, 0x4000de90, 0x4000de8c, NULL, NULL)   = 1
18906/637:      ioctl(5, FIONREAD, 0x7b0f5ce0)                  = 0
18906/637:      read(5, "\n03\0{ 02\0\00e030 \ra8j 85! a1".., 32) = 32
18906/637:      write(5, "L 01\00502\0\01502\0\010\0# \01 ".., 52) = 52
18906/637:      select(7, 0x4000de90, 0x4000de8c, NULL, NULL)   = 1
18906/637:      ioctl(5, FIONREAD, 0x7b0f5ce0)                  = 0
18906/637:      read(5, "\t03\07f02\0\00e03a e6@ U U U U ".., 32) = 32
18906/637:      write(5, "L 01\00502\0\01502\0\013\0# \01 ".., 20) = 20
18906/637:      select(7, 0x4000de90, 0x4000de8c, NULL, NULL)   = 1
18906/637:      ioctl(5, FIONREAD, 0x7b0f5ce0)                  = 0
18906/637:      read(5, "\t03\07f02\0\00e02a e6@ U U U U ".., 32) = 32
18906/637:      write(5, "L 01\00502\0\01502\0\013\0# \01 ".., 20) = 20
18906/637:      select(7, 0x4000de90, 0x4000de8c, NULL, NULL)   = 1
18906/637:      ioctl(5, FIONREAD, 0x7b0f5ce0)                  = 0
18906/637:      read(5, "a1  \08002\0\00e\0\0\0x \0\0\0y ".., 32) = 32
18906/637:      writev(5, 0x7b0f6008, 3)                        = 16
18906/637:      read(5, "01\0\082\0\0\0\0\0\0019f\0\t' bc".., 32) = 32
18906/637:      writev(5, 0x7b0f6008, 3)                        = 24
18906/637:      read(5, "01\0\083\0\0\0\0\0\001a0\0\t' bb".., 32) = 32
18906/637:      writev(5, 0x7b0f6008, 3)                        = 24
18906/637:      read(5, "01\0\084\0\0\0\0\0\001a1\0\t' bb".., 32) = 32
18906/637:      writev(5, 0x7b0f6008, 2)                        = 24
18906/637:      read(5, "01\0\085\0\0\0\0\0\001a2\0\t' bb".., 32) = 32
18907/640:        Received signal 1, SIGHUP, in read(), [caught], no siginfo
18906/637:      kill(-18907, SIGHUP)                            = 0
18907/640:      read(0, 0x7b0f673c, 1)                          = 0
18907/640:      stat64("/dhome1/n/myid/.sh_history", 0x7b0f6e38) = 0
18907/640:      open("/dhome1/n/myid/.sh_history", O_WRONLY|O_APPEND|O_LARGEFILE, 0600) = 6
18907/640:      write(6, "p w d \n", 4)                         = 4
18907/640:      close(6)                                        = 0
18907/640:      open("/dhome1/n/myid/.sh_history", O_RDONLY|O_LARGEFILE, 0666) = 6
18907/640:      fstat64(6, 0x7b0f6ef0)                          = 0
18907/640:      sigprocmask(SIG_BLOCK, 0x7b0f70ac, 0x7b0f70cc)  = 0
18907/640:      brk(0x40266000)                                 = 0
18907/640:      sigprocmask(SIG_SETMASK, 0x7b0f70cc, NULL)      = 0
18907/640:      read(6, "0101r e = ' ( < n a m e > ) ( ? ".., 392008) = 392008
18907/640:      close(6)                                        = 0
18907/640:      brk(0x401e6000)                                 = 0
18907/640:      sigprocmask(SIG_BLOCK, 0x7b0f6e68, 0x7b0f6e88)  = 0
18907/640:      ioctl(255, TIOCSPGRP, 0x7b0f6eb8)               = 0
18907/640:      sigprocmask(SIG_SETMASK, 0x7b0f6e88, NULL)      = 0
18907/640:      setpgid(18907, 18907)                           ERR#1 EPERM
18907/640:      sigaction(SIGHUP, 0x7b0f6e20, 0x7b0f6e48)       = 0
18907/640:      getpid()                                        = 18907 (18906)
18907/640:      kill(18907, SIGHUP)                             = 0
18907/640:        Received signal 1, SIGHUP, in kill(), [SIG_DFL], no siginfo
18907/640:      kill(0, 0x447)                                  [entry]
18907/640:      exit(1) [implicit (kill failure)]               WIFSIGNALED(SIGHUP)
18906/637:        Received signal 18, SIGCLD, in select(), [caught], no siginfo
18906/637:      select(7, 0x4000de90, 0x4000de8c, NULL, NULL)   = 0
18906/637:      waitpid(-1, NULL, WNOHANG)                      = 18907
18906/637:      kill(-18907, SIGHUP)                            ERR#3 ESRCH
18906/637:      open("/etc/utmp", O_RDWR|O_CREAT, 0644)         ERR#13 EACCES
18906/637:      open("/etc/utmp", O_RDONLY, 0124730)            = 7
18906/637:      read(7, "\0\0\0\0\0\0\0\0\0\0\0\0s y s t ".., 60) = 60
18906/637:      read(7, "\0\0\0\0\0\0\0\0\0\0\0\0r u n - ".., 60) = 60
 .
 .
 .
18906/637:      close(7)                                        = 0
18906/637:      close(6)                                        = 0
18906/637:      chown("/dev/ttyp3", 0, 0)                       ERR#1 EPERM
18906/637:      chown("/dev/pty/ttyp3", 0, 0)                   ERR#1 EPERM
18906/637:      chmod("/dev/ttyp3", 0666)                       ERR#1 EPERM
18906/637:      chmod("/dev/pty/ttyp3", 0666)                   ERR#1 EPERM
18906/637:      exit(0)                                         WIFEXITED(0)
$

At first glance I have no idea what to do with this information but I'll do some research and try to find out how I can use it to get the current text from another terminal. Thanks again.

To get full I/O data, you can add '-wall -rall' to truss. It may not be the end solution, but it shows you where the data you want to capture is being written.

It's also very educational about the operation of system calls and the division of functions in UNIX between applications and the kernel, like malloc() become brk() and the synamic linker ld() uses mmap() to map parts of libraries into VM, so all users of these libraries share the same RAM pages.