Matching Customized Prompts

This new script (above) manages to find yet another excuse for a regular expression. The string it is capable of finding is either "%" or "$" or "#“. This is a pretty good stab at recognizing common prompts since traditionally most C-shell users end prompts with "%“, most Bourne shell users end prompts with "$“, and root prompts in any shell typically end with "#“. The $ has to be quoted because it is special to the pattern matcher. (More on this on page 123.)

Of course, users are free to customize prompts further, and some do, wildly so. There is no way to pick a pattern for the prompt that suffices for everyone. If you are just writing a script for yourself, you can always find a pattern that will match your prompt. But if you want a script that works for any user, this can be a challenge. Users are free to change their prompt at any time anyway and you cannot predict the future.

One reasonable solution is to have each user define a pattern that matches their own prompt. A good way to do this is to have users store their patterns in the environment variable EXPECT_PROMPT. As a reminder, this variable should be set immediately after the customization of the real prompt in their .cshrc or wherever they do set it. By keeping them together, when the user changes their prompt, they will naturally think to change their prompt pattern at the same time.

Here is Tcl code to read the environment variable EXPECT_PROMPT. The variable is retrieved from env, a predefined ...

Get Exploring Expect now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.