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.
|Non-Linux FOSS: libnotify, OS X Style||Jun 18, 2013|
|Containers—Not Virtual Machines—Are the Future Cloud||Jun 17, 2013|
|Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer||Jun 12, 2013|
|Weechat, Irssi's Little Brother||Jun 11, 2013|
|One Tail Just Isn't Enough||Jun 07, 2013|
|Introduction to MapReduce with Hadoop on Linux||Jun 05, 2013|
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- Linux Systems Administrator
- Validate an E-Mail Address with PHP, the Right Way
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Introduction to MapReduce with Hadoop on Linux
- RSS Feeds
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?