Using Input to Customize Your Environment


Way back in Chapter 8, we talked about setting up your environment variables by customizing the configuration files that run upon log in. You can further customize your environment variables by requiring input whenever a startup script runs. For example, you can set your configuration files (which are actually scripts) so that they request that you specify the default editor for the session (Code Listing 17.5).

Code Listing 17.5. When the system asks your preferences, you know you're on top.

[ejr@hobbes ejr]$ su - ejr Password: Which editor do you want as the default? (vi or pico)? vi You chose vi! [ejr@hobbes ejr]$ 

To Use Input to Customize Your Environment:

1.

vi .bash_profile

Use your favorite editor to edit your script, and move to the end of the file.

2.

echo -e "Which editor do you want as the default? (vi or pico)"

Using echo -e, specify the text that will prompt you to input information (Figure 17.3).

Figure 17.3. Add this mini-script to your .zprofile, .bash_profile, or .profile configuration file, right at the end.


3.

read choice

On the next line, add read followed by the name of the variable to read in. We chose choice because we're using this input to set the preferred EDITOR environment variable.

4.

if [ $choice = "vi" ]

Start an if statementin this case, one that tests for the vi option.

5.

then EDITOR=/usr/bin/vi ; export EDITOR ; echo "You chose vi!"

Here, the then clause sets the EDITOR environment variable to vi, exports the environment variable, and announces your choice.

6.

elif [ $choice = "pico" ]

Check for your other option with elif (else if). This statement covers the pico option.

7.

then EDITOR=/usr/bin/pico ; export EDITOR ; echo "You chose pico!"

This then clause sets the EDITOR environment variable to pico, exports the environment variable, and announces your choice.

8.

else echo "Editor unchanged"

Set up an else statement, which will be used if neither option was entered at the read prompt. In this example, if neither vi nor pico was entered, it'll just say that the editor was unchanged.

9.

fi

End the if statement.

10.

Save and exit.

11.

su - yourid

At the shell prompt, type su - followed by your userid to log in again and test the revised login script (Code Listing 17.5).

Tip

  • This technique is very useful for setting the TERM(inal) environment variable if you access the system from different remote locations with different capabilities.





Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net