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).
[ejr@hobbes ejr]$ su - ejr Password: Which editor do you want as the default? (vi or pico)? vi You chose vi! [ejr@hobbes ejr]$ |
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. | |
4. | if [ $choice = "vi" ]
Start an if statement—in 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.