start page | rating of books | rating of authors | reviews | copyrights

Unix Power ToolsUnix Power ToolsSearch this book

24.21. Terminal Windows Without Shells

xterm is an X client that runs a Unix process on a pty "inside" a window. By default, this process is a shell: an instance of the same shell you log into the system with. But it can be basically any Unix process. As you saw in Section 24.20, when the process exits, the xterm window closes because its child process has gone.

To override the default shell process in an xterm window, use the -e option (Section 5.22), followed by the command line to run the process. This must be the last thing on the xterm command line. If you want to open an xterm window with no scrollbar (the +sb option) and with the vi editor in it, to edit the log file named logfile, run the command below:

% xterm +sb -e vi logfile
%

An xterm window should open with vi running inside it. If you don't know how to use vi, the best thing to do is to leave it alone until you've finished this example -- then press the ESC key, type :q, and press ENTER to exit vi. When vi exits, its window should close too, and you'll get another shell prompt.

I chose to have you run vi in a window because the vi process keeps running until you tell it to quit, and then the window closes. Other Unix processes that don't wait for a "quit" command will terminate as soon as they're done, and the window closes before you can see the process output. For example, let's say you want to display a file in an xterm window with a scrollbar. Start by choosing a file and using wc -l (Section 16.6) to count the number of lines. Then open an xterm and a scrollbar, with the scrolling buffer length set to just the right number of lines:

cat Section 12.2

% wc -l somefile
     74 somefile
% xterm -sl 74 -sb -e cat somefile
%

What happened? Unless your window manager holds it there, the xterm window closes just after it opens. Why? Its child cat process exited, so the parent xterm did too. One easy answer is to use a shell that runs three commands. First is the command you want to run (here, cat). Next, echo a prompt. Finally, run the read command (Section 35.18) to pause until you give a dummy value -- just pressing ENTER will be enough to satisfy read, and then the shell will exit. Here's how:

% xterm -sl 76 -sb -e \
    sh -c 'cat somefile; echo "Press RETURN to exit..."; read dummy'

(First, two notes. The backslash (\) isn't needed if you type the entire command on one line. And we've increased the scroll length to 76 because the echo and the newline after it add two lines of text.) Here, xterm starts a shell, but it's not the default shell (whatever that happens to be): it's the sh shell you specify after the xterm -e option. The sh option -c tells the Bourne shell to run the single command line from the following argument and then exit. The command line is in quotes to be sure the shell inside the xterm interprets it. The three commands are separated by semicolons (;) (Section 28.16). If your command line is really complicated, you might want to change the sh -c '...' to run a little shell script (Section 35.1) instead, like sh $HOME/lib/catter.

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.