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:
delpath /opt/CC/test/bin
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.
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.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Nice article, thanks for the
53 min 46 sec ago - I once had a better way I
6 hours 39 min ago - Not only you I too assumed
6 hours 57 min ago - another very interesting
8 hours 50 min ago - Reply to comment | Linux Journal
10 hours 43 min ago - Reply to comment | Linux Journal
17 hours 37 min ago - Reply to comment | Linux Journal
17 hours 53 min ago - Favorite (and easily brute-forced) pw's
19 hours 45 min ago - Have you tried Boxen? It's a
1 day 1 hour ago - seo services in india
1 day 6 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
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?




Comments
Source Code Availability - pathfunc.tgz
Great article! Can the code be made available?? Email will do.
Thanks,
Rick
Where's the source code?
The ftp link at the end is password protected. Shouldn't it be open to the public?