Flylib.com

Books Software

 
 
 

Locating Commands


Locating Commands

As discussed earlier in this chapter, two or more commands may have the same name , but be implemented differently. In such a case, Qshell uses the following order of precedence to decide which implementation to run:

  1. Reserved word

  2. Alias

  3. Special built-in

  4. Function

  5. Regular built-in

  6. Executable file

You can override this search order to a point because of the following rules:

  • The command utility ignores aliases and functions.

  • The builtin utility will only run a built-in.

  • An initial backslash ignores aliases and functions.

For example, in Figure 12.20, an echo function has been created. It displays all arguments, preceding them with an arrow made of two equal signs and a greater-than sign. Qshell gives precedence to this function unless echo is run under the built-in utility.

start figure


function echo ()


>


{


>


print "==>

$

@"


>


}


/home/jsmith $


type -a echo


echo is a shell built-in.


echo is a function.


echo is /usr/bin/echo.


/home/jsmith $


echo

$

QSH_VERSION


==> V5R2M0


/home/jsmith $


builtin echo

$

QSH_VERSION


V5R2M0

end figure

Figure 12.20: Echo is a regular built-in utility, so it normally has precedence.

In Figure 12.21, an alias is created with the same name as the ls utility, which is an executable file in the /usr/bin directory. The alias takes precedence over the file unless the alias is preceded by a backslash.

start figure


ls *.txt


edity.txt       goodoleboys.txt   temp.txt           test1a.txt


ftpmodel.txt    myfile.txt        test1.txt


/home/jsmith $


alias ls=ls -l


/home/jsmith $


type -a ls


ls is an alias for ls -l.


ls is /usr/bin/ls.


/home/jsmith $


ls *.txt


-rwx---rwx  1  JSMITH  0          800 Oct 19 15:59 edity.txt


-rwxrwxrwx  1  JSMITH 0            43 Feb 22  2002 ftpmodel.txt


-rwxrwxrwx  1  JSMITH 0           746 Oct 22 22:18 goodoleboys.txt


-rwxrwxrwx  1  JSMITH JSMITHGP      0 Oct 24 18:03 myfile.txt


-rw-rw-rw-  1  JSMITH JSMITHGP     51 Oct 23 17:34 temp.txt


-rw-rw-rw-  2  JSMITH 0             7 Feb  4  2002 test1.txt


-rw-rw-rw-  2  JSMITH 0             7 Feb  4  2002 test1a.txt


/home/jsmith $


\ls *.txt


edity.txt       goodoleboys.txt   temp.txt         test1a.txt


ftpmodel.txt    myfile.txt        test1.txt

end figure

Figure 12.21: Preceding the command name ls with a backslash tells Qshell to ignore the alias.

The Path Variable

When you run an application or script, Qshell does not search all directories in the IFS. It searches only the directories listed in the PATH variable. If Qshell does not find an executable file of the proper name, it writes an error message to stderr.

The PATH variable contains a list of directories that are to be searched for external files. The directories must be separated by colons. The default value for PATH is /usr/bin: . If you want Qshell to search the current directory, you must include the current directory in the path name.

There are four ways to indicate the current directory. You can use:

  1. An initial colon in the path value

  2. Two adjacent colons within the path value

  3. A trailing colon on the path

  4. A period in the path variable

The following example uses the print utility to display the current path:


print $PATH


/usr/bin:/home/jsmith/bin:

In this example, there are two directories in the path. Because the path name ends with a colon, Qshell will also search the current directory after searching the directories in the path.

To define the path, assign a value to the PATH variable:

PATH=/:/home/jsmith:/usr/bin:/home/jsmith/bin

To add more directories to a path, include the $PATH expansion in an assignment to the PATH variable. Surround the assigned value with double quotes, not single quotes, so that Qshell can substitute the current path into the assignment:


print $PATH


/usr/bin:/home/jsmith/bin:


/home/jsmith $


PATH="$PATH/QOpenSys/usr/bin"


/home/jsmith $


print $PATH


/usr/bin:/home/jsmith/bin:/QOpenSys/usr/bin

The Hash Utility

The hash utility, shown in Figure 12.22, maintains a list of the locations of utilities. When a utility is executed for the first time in a Qshell session, hash searches for the utility and stores its location in a list. When the utility is later executed in the same process, Qshell does not have to look through directories for the utility, but retrieves the utility from the location named in the list.

start figure

{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}


hash


ls=/usr/bin/ls


donde.qsh=/home/jsmith/bin/donde.qsh


mv=/usr/bin/mv


function echo

end figure

Figure 12.22: The hash utility stores the locations of utilities in a list.

You may use the -r option of hash to clear the list of utility locations, as shown in Figure 12.23.

start figure


hash


rm=/usr/bin/rm


ls=/usr/bin/ls


/home/jsmith $


hash -r


/home/jsmith $


hash


/home/jsmith $

end figure

Figure 12.23: The - r option clears the hash table.

With V5R2, IBM added the p option to the hash utility, which allows you to store a utilitys location in the list. The utility name may be different from the base file name. The syntax of hash 's p option is shown here:


hash -p


filename utility

For example, in the following command, Qshell is to look for the donde.qsh script in directory /home/jsmith:

hash -p /home/jsmith/donde.qsh donde.qsh

In the following example, whenever Qshell is told to run the wer command, it will run script donde.qsh in directory /home/jsmith:

hash -p /home/jsmith/donde.qsh wer