Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux Timesaving Techniques For Dummies.pdf
Скачиваний:
59
Добавлен:
15.03.2015
Размер:
15.98 Mб
Скачать

Seeing a Red Alert When You Have Superuser Privileges

27

TABLE 4-2: SOME USEFUL TPUT SEQUENCES

Sequence

What You Use It For

tput sgr0 Reset all formatting. tput bold Display text in bold font.

tput rev Display inverse-colored text (white on black instead of black on white, for example).

tput smul Start underlining text. tput rmul Stop underlining text. tput setaf Set foreground color. tput setab Set background color.

These are your choices for foreground and background colors:

0 Black

1 Red

2 Green

3 Yellow

4 Blue

5 Magenta

6 Cyan

7 White

Just find the color you want to use and stick it at the end of the tput setaf or tput setab command.

You can combine the different text effects to produce colored and underlined prompts, boldface inverse fonts, and any combination of your terminal supports. For example, you can display an underlined blue prompt:

[freddie@bastille] BLUE=”\[$(tput setaf 4)\]”

[freddie@bastille] ULINE=”\[$(tput smul)\]” [freddie@bastille] RESET=”\[$(tput sgr0)\]” [freddie@bastille] PS1=”$BLUE$ULINE[\

u@\h]$RESET “ [freddie@bastille]

Notice that we used tput sgr0 to restore the text back to its normal state (default color, no underline, no bold). That’s usually a good idea when you use tput to customize your prompt. Otherwise, whatever you type in after the colorized prompt will be colorized as well.

Seeing a Red Alert When You

Have Superuser Privileges

We mention earlier in this technique that we can show you a better way to remind yourself that you hold dangerous superuser privileges. The typical way to distinguish between superuser status and mere-mortal status is to change one character in your prompt (usually the last character) from $ to #. But that’s a pretty small change and can easily go unnoticed. Superuser privileges are dangerous: one mistake, and you’re looking at hours of cleanup.

Here’s a way to make your privilege level jump out at you: When you hold superuser privileges, your prompt is displayed in red, and when you don’t, your prompt is displayed in blue. The following steps explain how to make this change:

1. Open a terminal window and give yourself superuser privileges with the su command.

$ su Password:

#su

2.Open the /etc/bashrc file in your favorite editor.

#kedit /etc/bashrc

If you prefer GNOME, you can use gedit instead:

# gedit /etc/bashrc

If you’re using SuSE, modify the

/etc/bash.bashrc.local file. If the file doesn’t already exist, it is automatically created when you save your changes.

28 Technique 4: Prompting Yourself with a Custom Prompt

3. Add the following code to the end the file:

function setprompt

{

local BLUE=”\[$(tput setaf 4)\]” local RED=”\[$(tput setaf 1)\]” local RESET=”\[$(tput sgr0)\]”

#If ‘id –u` returns 0, you have

#superuser privileges

if [ `id -u` = 0 ] then

PS1=”$RED[\u@\h:\W]$RESET “ else

PS1=”$BLUE[\u@\h:\W]$RESET “

fi

}

setprompt

4. Save your work and close the editor; you’re finished!

We want to note a couple of interesting points about this sample code:

First, you must add this function to the /etc/ bashrc file, not your own personal ~/.bashrc file. Why? Because you want to modify the prompt not only for yourself, but also for the superuser. (Remember, /etc/bashrc is executed for all users, and ~/.bashrc is executed only when you log in.)

Second, notice that we created a shell function and put most of the code inside that function. By declaring the $BLUE, $RED, and $RESET variables as local, they’re destroyed as soon as the function (setprompt) ends. If you don’t wrap the variables inside a function, you’ll find them in your list of environment variables and probably wonder where they came from. We give some more words of shell-scripting wisdom in Techniques 8 and 10.

Saving Your Work

When you find a prompt that you’d like to keep, you want to store the $PS1 variable somewhere so that your prompt returns the next time you log in. The safest place to set $PS1 is in your ~/.bashrc login script. (This script is executed every time you start a new shell.) To save your fancy new prompt:

1. Start your favorite editor (kate, kedit, or the

GNOME Text Editor will do).

2. Open the file /home/user-name/.bashrc.

Make sure you type in your Linux user name instead of user-name and make sure you include the period before the word bashrc. Your .bashrc file will probably look something like this:

#.bashrc

#User specific aliases and functions

#Source global definitions

if [ -f /etc/bashrc ]; then

. /etc/bashrc

fi

3. Add the following code to the end of the file:

# Customize the prompt BLUE=”\[$(tput setaf 4)\]” ULINE=”\[$(tput smul)\]” RESET=”\[$(tput sgr0)\]” PS1=”$BLUE$ULINE[\u@\h]$RESET “

4. Now save your changes and close the editor.

If you want to change the default prompt for newly created user accounts, give yourself superuser privileges and modify the /etc/skel/.bashrc file. /etc/skel/.bashrc is copied to a user’s home directory when his or her user account is created.

Saving Your Work 29

Your ~/.bashrc script is executed whenever you log in. If another user logs in, the .bashrc script in that user’s home directory is executed. If you want to customize the prompt for all users (not just for new users), store your changes in /etc/bashrc.

Technique 8 spells out the rules for deciding which login script you want to modify — see that technique for all the details.

If you’re intrigued by the idea of customizing your bash prompt and want more information, browse around the bashprompt project Web site at

www.gilesorr.com/bashprompt/howto/

book1.html

This site offers some great examples and background information.

5 Getting There Quick

with Dynamic

Technique Shortcuts

Save Time By

Using shortcuts to complete filenames

Using environment variables to filter results

Customizing name completion for remote logins

Graphical applications look nice, but it’s hard to beat the command line for pure speed and raw power. Using bash is an obvious choice when you need to do something fast, but unless you’re the perfect

typist, keyboard errors can slow you down — especially if you type faster backwards (that is, with the Delete key) or if you’re working in a casesensitive environment like Linux.

With a few shortcut keystrokes, bash will complete your command line for you — we call that feature a dynamic shortcut. Not having to retype incorrectly entered commands or filenames can save you hours in no time. With dynamic shortcuts, you make fewer keystrokes . . . and fewer keystrokes mean fewer wrong keystrokes. In this technique, we show you how to use shortcuts at the command line to save time and keystrokes and to avoid typing errors.

Completing Names Automatically

bash knows how to complete filenames, command names, user names, and host names on your behalf. Try the following steps:

1. Open a terminal window. You can find one in the GNOME or KDE

Menu under System Tools (or System, if your version of GNOME or KDE doesn’t have a System Tools menu choice).

2. Type the first few letters of the command, variable, or whatever you’re looking for and press the Tab key twice.

For example, if you type host, a list of commands that start with the letters host appears. See Table 5-1 for more details on autocompleting variables, user names, and so on.

3. If you need to narrow down your options further, type the next few letters of the command and press the Tab key again.

To tell bash how to complete the hostname command, you’d type n. If you have more than one command that begins with the letters hostn, bash shows you a list of those commands. Just type enough letters to

Соседние файлы в предмете Операционные системы