Shell Functions and Path Variables, Part 3
In this final article in the series, I'll describe the remaining path handling functions and point out a few implementation issues. Before I do that, however, I will describe a utility called makepath. This reads either standard input or its argument list, builds a colon-separated path variable (pathvar) from those lines read and echoes it to standard output. For example:
$ makepath /bin /usr/bin /opt/kde/bin /bin:/usr/bin:/opt/kde/binmakepath is used in several of the pathvar utilities to reconstruct a pathvar after its path elements (pathels) have been altered. I won't show you the innards of makepath, as they're somewhat tangential to the main topic and rather trivial.
First, let's look at listpath, which echoes the pathels making up a pathvar on separate lines, as in:
$ listpath -p MANPATH /usr/man /usr/local/man /opt/CC/man
Using listpath has two advantages over merely echoing $MANPATH. First, it's much easier to read the pathels when they appear on separate lines; and secondly, you can pipe its output through grep:
$ listpath | grep bin /opt/kde/bin /usr/local/bin /binThere is no option-handling code we did not see in the addpath function, so let's look at the main code:
eval echoThis is very simple. We just echo the contents of the specified pathvar into the colon2line function (included in the tar file mentioned at the end of this article), which turns the embedded : characters into newline characters. I described the operation of this piece of code in some detail in Part 2, so I won't repeat it here. Take a look at that article (http://www.linuxjournal.com/lj.issues/issue72/3768.html) if you're not sure why the eval is there.
We have already seen the addpath function, which performs an idempotent addition of a pathel to a pathvar. The converse of this behaviour is provided by delpath, which removes pathels from a pathvar. So, for example:
will remove the /opt/CC/test/bin directory from $PATH and:
delpath -e "(bill|steve)" -p MANPATHwill remove all pathels matching the egrep-style regular expression "(bill|steve)" from $MANPATH. The command
delpath -nremoves all non-existent directories from $PATH.
Although delpath is not a function you are likely to need often, there is one place where it can be useful. Many UNIX machines have a file called /etc/PATH, which is sourced by /etc/profile. It sets up a default PATH containing directories required by all users. Too often, though, /etc/PATH is not modified for years at a time, and the directories added either no longer exist or are not truly required by all. In this case, you can call delpath at the start of an appropriate login script (.profile or .bash_profile) to remove the directories you do not need.
Let's look at the delpath code. I'll skip most of the option handling, as much of it is identical to that in addpath.
MATCH="-x" # default [ -n "$opt_e" ] && MATCH= # make grep use regexps FILTER= # default [ -n "$opt_n" ] && FILTER="| realpath_filter"
Here, we see the final section of the option handling. The MATCH variable determines whether we handle the supplied path description as a regular expression or not. It is used later as an option to grep; grep -x tells grep to perform exact string matches.
The FILTER variable implements the -n option, i.e., the “remove non-existent directories” behaviour. If the user supplies the -n option, FILTER contains a string which pipes the output of a previous command through a program called realpath_filter. This program reads directory names from its standard input and writes the name to standard output only if it is an existing directory. I'll leave it as an easy exercise for the reader to implement such a filter.
The remainder of delpath is as follows:
eval listpath -p $pathvar $FILTER | grep -v -E $MATCH "$1"> /tmp/makepath_in.$$ eval $pathvar=$(makepath < /tmp/makepath_in.$$) rm /tmp/makepath_in.$$
The function does its work in three stages. The first command generates a file in /tmp containing those directories that are not to be deleted. The second command rebuilds the pathvar from that file using makepath. Finally we remove the file; we don't want it cluttering up the file system after the function finishes. (The shell expands $$ to the process ID of the shell running the command; I'll assume it is 20610 in this article.)
Let's look at the first line. Essentially, it uses listpath to break the appropriate pathvar into separate lines, and grep to remove those we don't want. It's slightly complicated, however, by the presence of the FILTER variable. Suppose the user types:
delpath -e "^opt"
which means “remove all directories starting with the opt string from $PATH”. In this case, pathvar will contain PATH, while MATCH and FILTER will be empty. The first line will therefore expand to:
eval listpath -p PATH | grep -v -E "^opt<" > /tmp/makepath_in.20610This is straightforward—listpath writes the pathels in PATH into the grep command, which we use to echo non-matching lines only (-v). We redirect the output into our temporary file, which will contain those pathels not starting with opt. In this case, the leading eval is unnecessary. However, if the user types
delpath -nto remove all non-existent directories from $PATH, then the first line expands to
eval listpath -p PATH | realpath_filter | grep -v -E "" > /tmp/makepath_in.20610During the initial processing of the line (i.e., before the eval has forced re-evaluation), the shell saw the pipe symbol preceding the grep, but it did not see the pipe symbol preceding realpath_filter. As things stand at the moment, the shell sees the first | as a literal character and will pass it as an argument to listpath. This happened because the shell looks for | characters before it expands variables, and the | character preceding realpath_filter was stored in a variable. The second evaluation caused by the eval ensures the pipeline that runs the realpath_filter command is constructed.
We now have a file containing only the required pathels. The second line in delpath rebuilds the pathvar from that file, using the following code:
eval $pathvar=$(makepath < /tmp/makepath_in.$$)
This shouldn't cause us too many problems. First, makepath simply reads the lines in the file, builds a colon-separated pathvar and echoes it. We run makepath in command-substitution mode (that's the $(...) which I described in Part 2), so makepath's output is used as the right-hand side of the variable assignment. The initial eval is required due to the order in which the shell evaluates a command. Because it looks for assignment statements before expanding variables, it won't recognize that the command contains a valid assignment. The eval ensures the assignment takes place the second time the line is processed.
|Nativ Disc||Sep 23, 2016|
|Android Browser Security--What You Haven't Been Told||Sep 22, 2016|
|The Many Paths to a Solution||Sep 21, 2016|
|Synopsys' Coverity||Sep 20, 2016|
|Naztech's Roadstar 5 Car Charger||Sep 16, 2016|
|RPi-Powered pi-topCEED Makes the Case as a Low-Cost Modular Learning Desktop||Sep 15, 2016|
- Android Browser Security--What You Haven't Been Told
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Nativ Disc
- The Many Paths to a Solution
- Naztech's Roadstar 5 Car Charger
- Synopsys' Coverity
- Securing the Programmer
- RPi-Powered pi-topCEED Makes the Case as a Low-Cost Modular Learning Desktop
- Identity: Our Last Stand
- Glass Padding
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide