Книга: Fedora™ Unleashed, 2008 edition

Running a Shell Program

Running a Shell Program

You can run your new shell program in several ways. Each method produces the same results, which is a testament to the flexibility of using the shell with Linux. One way to run your shell program is to execute the file myenv from the command line as if it were a Linux command:

$ ./myenv

A second way to execute myenv under a particular shell, such as zsh, is as follows:

$ zsh myenv

This invokes a new zsh shell and passes the filename myenv as a parameter to execute the file. A third way requires you to create a directory named bin in your home directory, and to then copy the new shell program into this directory. You can then run the program without specifying a specific location or using a shell. You do this like so:

$ mkdir bin
$ mv myenv bin
$ myenv

This works because Fedora is set up by default to include the executable path $HOME/bin in your shell's environment. You can view this environment variable, named PATH, by piping the output of the env command through fgrep, like so:

$ env | fgrep PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:
/usr/X11R6/bin:/sbin:/home/andrew/bin

As you can see, the user (andrew in this example) can use the new bin directory to hold executable files. Another way to bring up an environment variable is to use the echo command along with the variable name (in this case, $PATH):

$ echo $PATH
/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/andrew/bin

CAUTION

Never put . in your $PATH to execute files or a command in the current directory — this presents a serious security risk, especially for the root operator, and even more so if . is first in your $PATH search order. Trojan scripts placed by crackers in directories such as /tmp can be used for malicious purposes, and are executed immediately if the current working directory is part of your $PATH.

After you execute the command myenv, you should be able to use ldir from the command line to get a list of files under the current directory and ll to get a list of files with attributes displayed. However, the best way to use the new commands in myenv is to put them into your shell's login or profile file. For Fedora, and nearly all Linux users, the default shell is bash, so you can make these commands available for everyone on your system by putting them in the /etc/bashrc file. Systemwide aliases for tcsh are contained in files with the extension .csh under the /etc/profile.d directory. The shell can use these command aliases, too.

Оглавление книги


Генерация: 0.954. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз