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

Customizing Variables for Rapid Transit

43

To set the $PATH environment variable for a user, follow these steps:

1. At the command line, enter gedit ~user/.bashrc and press Enter.

2. When the editor opens, add the following line to the end of the file:

PATH=$PATH:/foo/bar/baz

Substitute your directory name for

/foo/bar/baz.

The command you just entered appends the directory to the user’s current search path.

3. Click the Save icon and close the editor.

Now, when this user enters a program name, bash searches through all the directories listed in $PATH.

You can add as many directories to the user’s path as you’d like, but remember that as you hand out easy access to commands, you could invite accidents.

Customizing Variables for Rapid Transit

All the environment variables you’ve seen so far are automagic variables — bash defines them for you, and they can change value over time. You can

also create custom variables to make your life easier. For example, we spend a lot of time in the directory /usr/local/src (that’s where open-source source code typically lives). In our system-wide login script (/etc/bashrc), we define an environment variable named $SRC that equates to /usr/local/src. That makes it easy to navigate to our workplace — just cd $SRC and we’re there.

Of course, you can use environment variables to do things other than just cd: You can copy files (cp $SRC/foo.c $DST/), create archives (tar -zcvf

$SRC/mycode.tgz $SRC/kde/), or just about anything

else. Unfortunately, most graphical programs don’t know how to deal with environment variables, but they can sure save you time at the command line.

Just think of the pathnames you use over and over every day, and you’ll see why custom environment variables can be a great timesaver.

The following shell script defines a few custom variables you can use to get somewhere quickly:

#File name: setvars.bash

#Define a few shortcuts

export SRC=/usr/local/src

# cd

$SRC will take me to /usr/local/src

export

DESK=~/Desktop

#

cd

$DESK will take me to my desktop

 

 

export

ACCTG=/opt/data/accounting

#

cd

$ACCTG will take me to my bookkeeping data

echo “Your custom variables are ready for use”

To use the code:

1. Open your favorite editor and create the file

~/setvars.bash.

$ gedit ~/setvars.bash

2.Type in the variables that you want to define (be sure to put the word export in front of each one).

export SRC=/usr/local/src export DESK=~/Desktop

export ACCTG=/opt/data/accounting

Note: Be sure that you don’t have any spaces before or after the =; otherwise, bash will complain when you try to run your script.

3.Save your work and close the editor.

At the command line, adjust the permissions for the file you’ve just created, making it executable:

$ chmod a+x setvars.bash

44 Technique 7: Typing Less and Doing More with Handy Automagic Variables

Now, to execute your program and have your custom variables ready for use, just put a period (.) and a space at the beginning of the command line, like this:

$ . setvars.bash

Your custom variables are ready for use $ echo $SRC

/usr/local/src

$

When you run a shell script that defines environment variables (like this one does), you have to put a . at the beginning of the command line. The . character is also known as the source command. In fact, you can type source setvars.bash instead of using ., but that’s more typing. If you don’t source (or ‘.’)

the script, bash will start a new shell session, run your script, and immediately terminate the new shell session.

Why is immediate termination a problem? Because the environment variables are defined in the subshell (that new shell session) instead of your shell session. When the sub-shell ends, your fancy new variables disappear! The source command tells bash to execute a script within the current shell instead of firing up a new shell.

After you create the pathname shortcut, you can move to your source code directory by typing cd $SRC and pressing Enter. You’re at your location in a snap!

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