SHELLdorado Newsletter 1/99 - February 21, 1999 ================================================================ The "SHELLdorado Newsletter" covers shell script related topics. To subscribe to this newsletter, leave your e-mail address at the SHELLdorado homepage: http://www.shelldorado.com/ "Heinerīs SHELLdorado" (http://www.shelldorado.com/) is a place for UNIX shell script programmers providing Many shell script examples, shell scripting tips & tricks + more... ================================================================ Contents o Editorial o What's new at the SHELLdorado? o Shell Tip: Positioning cursor from shell scripts o Shell Tip: How to set the title of an XTERM window o Q&A: How can I write a shell by myself? _________________________________________________________________ Dear readers, Welcome to the "SHELLdorado Newsletter"! This newsletter is intended to provide interesting and useful information for UNIX shell script programmers. Some articles will be for beginners, and some for profis, but hopefully everybody using sh, ksh, bash & co. will find some interesting article from time to time. Best regards, Heiner Steven (ed.) heiner.steven@odn.de ----------------------------------------------------------------- >> What's new at SHELLdorado? ----------------------------------------------------------------- o New Article "The Ignorant's Guide to Shell Programming": http://www.shelldorado.com/articles/ignorantsguide.html o The SHELLdorado "bibliography" list has been extended and now provides links to "amazon.com". This way interested readers can view the book cover and peruse other book reviews there: http://www.shelldorado.com/articles/unixbib/ ----------------------------------------------------------------- >> Shell Tip: Positioning the cursor from within shell scripts ----------------------------------------------------------------- [ Further shell scripting tips & tricks: ] [ http://www.shelldorado.com/shelltips/ ] For some shell scripts it would be desirable, if the script could position the cursor to arbitrary (row, column) pairs (i.e. to display a status line, ...) The following shell function uses the "tput" command to move the cursor to the specified (row, column) position: # move cursor to row $1, col $2 (both starting with zero) # usage: writeyx message rowno colno writeyx () { tput cup $2 $3 echo "$1" } Example usage: clear # clear the screen writeyx "This is a centered message" 11 26 writeyx "press any key to continue..." 22 0 read dummy The "tput" command looks up the escape command sequence for a feature needed for the current terminal. You can use it for other terminal related things, too: tput smso # "start mode shift out": usually # reverse echo "This is printed reverse" tput rmso # "reset mode shift out" All available capability names are listed on the terminfo(5) manual page. Portability: The "tput" command is available with the "terminfo" terminal information database ----------------------------------------------------------------- >> Shell Tip: How to set the title of a XTERM window ----------------------------------------------------------------- The title of a XTERM window can be set using the following escape sequence: ESC ] 0 ; title ^G Example: echo "^[]0;This is a title^G" Enter the escape character (the first character of the string) as CTRL-V ESC. On the screen you will see "^[". The last character is entered as CTRL-V CTRL-G. The example above is the core of the (lengthy) script "xtitle": http://www.shelldorado.com/scripts/cmds/xtitle ----------------------------------------------------------------- >> Q&A: How can I write a shell by myself? ----------------------------------------------------------------- Common shells like sh, ksh, bash, are quite complex, but basically a shell just reads an input line from standard input, parses the line into commands and arguments, calls the command with the arguments and waits for it to terminate. A small example follows: #include <stdio.h> #include <string.h> int main (int argc, char *argv []) { char inbuf [256]; /* input buffer */ char *cmd, *arg; /* command name, one argument */ int childstatus; for ( ; ; ) { /* Print the prompt string character */ fprintf (stderr, "%s ", "%"); fflush (stderr); if ( !gets (inbuf) ) break; /* EOF: terminate our "shell" */ /* Get the command name */ if ( !(cmd = strtok (inbuf, " \t\n")) ) continue; /* We only support one argument at this time */ arg = strtok (NULL, " \t\n"); if ( fork () == 0 ) { /* We are the child: execute command */ fprintf (stderr, "child: executing %s\n", cmd); execlp (cmd, cmd, arg, 0); return 2; /* "exec" only returns in case of errors */ } /* Parent: wait for child to terminate */ wait (&childstatus); fprintf (stderr, "parent: child returned %d\n", childstatus & 0xff); } } Now you just have to add i/o redirection, environment variables, parameter and command substitution, command line editing and all the other features that make us like a certain shell more than others ;-) Even though the source code for some shells (bash, pdksh, zsh, ...) is available freely, it is quite complex and hard to read. A beginner will like the well commented example of a small shell in the following book: Marc J. Rochkind: Advanced Unix Programming. Prentice Hall, Englewood Cliffs 1985. ---------------------------------------------------------------- If you want to comment on the newsletter, or even want to submit an article of your own, send a mail to mailto:heiner.steven@shelldorado.com ================================================================ To unsubscribe send a mail with the body "unsubscribe" to newsletter@shelldorado.com ================================================================