How Shell Scripts Are Executed
Recall that one of the file permissions you could assign to a file is named execute.With the execute permission assigned, any file, including a text file, can be allowed to execute from the shell prompt. This is a great option for making scripts easy for end users to run.
To do this, simply enable the execute attribute for Owner, Group, and/or Others. As you learned earlier, this is done using the chmod command. In the example we’re working with here, I can configure the runme file to be executed by the file Owner by entering
chmod u+x runme
at the shell prompt.
Notice in the preceding example that I had to enter
./runme
at the shell prompt.
Even though the file resided in the current directory (in this case, /home/rtracy), this directory is not in my PATH environment variable.If you’re creating scripts for yourself, this probably doesn’t pose a problem. By now, you should be familiar enough with Linux to understand why you have to specify the path to the file at the shell prompt and know how to do it. However, if you’re creating a script for your end users to run, this may present a problem. To save yourself frustration, you should consider putting the script in a directory that is part of the PATH or adding the directory to your existing PATH.
One option you can use is the ~/bin directory in each user’s home directory. Most Linux
distributions automatically create a directory named /bin for each user. Then, one of the bash
configuration files, discussed in an earlier chapter, is used to automatically add ~/bin to the PATH
environment variable when a shell is started. This is shown here:
rtracy@openSUSE:~> echo $PATH
/usr/lib/mpi/gcc/openmpi/bin:
/home/rtracy/bin
:/usr/local/bin:/usr/bin:
/bin:/usr/bin/X11:/usr/X11R6/bin:/usr/games
You could also, of course, create your own directory and manually edit your bash configuration files to add it to your user’s PATH environment variable.
LX0-104 Exam Objectives (F)
No comments:
Post a Comment