Книга: 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.
- Running a Shell Program
- Interpreting Shell Scripts Through Specific Shells
- Using Variables in Shell Scripts
- Using a Simple Script to Automate Tasks
- Built-In Variables
- Special Characters
- Comparison of Expressions in bash
- Special Statements: for, while, and Others
- Using Functions in Shell Scripts
- Reference
- The Shell Command Line
- Console-Based Monitoring
- Using Variables in Shell Scripts
- Running INN
- 5.2.1 Running the Simulation
- 5.1.1 Running One Command at a Time
- 14.4.3. Shell
- 5.2.5 Running Shell Commands on Files
- 2. How to Apply These Terms to Your New Programs
- The Programmers
- Для чего нужны папки Windows, Documents and Settings, Program Files и Temp?
- Можно ли указать использование по умолчанию вместо C:Program Files другого каталога для установки программ?