Category Archives: unix

UNIX Script Goodness And Variable Prefix/Suffix Stripping Fun


Here’s something you probably have no interest in, but it’s a script I wrote to help me with UNIX shell programming that I engage in from time to time.

There’s a nice way of easily taking a piece of text (often called a “String” by programmer-types) and stripping off pieces of it, either from the front or the back, using variable evaluation.

One thing to note about UNIX is that are many, many different ways to do the same thing. This is just one little feature of UNIX I like to use.

I wrote a script to help me when I want to do this prefix/suffix stripping kind of variable evaluation.

Here is the script in its entirety:

function showUsage {
print
print ‘USAGE:’
print ‘ksh -f variableTest.sh
print
print ‘=============================================================================================================’
print ‘${variable#pattern} evaluates variable, but removes the smallest portion of its prefix which matches pattern.’
print ‘${variable##pattern} evaluates variable, but removes the largest portion of its prefix which matches pattern.’
print ‘${variable%pattern} evaluates variable, but removes the smallest portion of its suffix which matches pattern.’
print ‘${variable%%pattern} evaluates variable, but removes the largest portion of its suffix which matches pattern.’
print ‘=============================================================================================================’
print
print ‘Special instructions:’
print ‘======================’
print ‘To stop the shell from interpreting wildcards you may use for patterns,’
print ‘run this script like the following:’
print
print ‘ksh -f variableTest.sh
print
print EXAMPLE: ksh -f variableTest.sh aabbcc \’a*\’
print
print
exit 1
}

clear

if [ $# -ne 2 ]
then
showUsage
fi

variable=$1
pattern=$2

print
print ‘=============================================================================================================’
print ‘${variable#pattern} evaluates variable, but removes the smallest portion of its prefix which matches pattern.’
print ‘${variable##pattern} evaluates variable, but removes the largest portion of its prefix which matches pattern.’
print ‘${variable%pattern} evaluates variable, but removes the smallest portion of its suffix which matches pattern.’
print ‘${variable%%pattern} evaluates variable, but removes the largest portion of its suffix which matches pattern.’
print ‘=============================================================================================================’
print
print ‘variable: ‘ $variable
print ‘pattern: ‘ $pattern
print
print ‘${variable#pattern} ‘ ${variable#$pattern}
print ‘${variable##pattern} ‘ ${variable##$pattern}
print ‘${variable%pattern} ‘ ${variable%$pattern}
print ‘${variable%%pattern} ‘ ${variable%%$pattern}

Most of this script is just printing stuff out to the screen. There’s a whole big piece of code that just tells you how to run the script.

Anyways, say you want to find out what directory you are in on a UNIX file system, and want to save this off in a variable, but without all the nested subdirectories your directory rests in (UNIX is all about the nested subdirectories).

You can use this script to figure out the right pattern to get your current directory minus the path.

Example:

Let’s say I’m in:
/usr/appl/abc/very/very/long/directory

After some trial and error running my script, I can eventually figure out how to get my current directory, minus the path.


>ksh -f variableTest.sh /usr/appl/abc/very/very/long/directory ‘*/’

variable: /usr/appl/abc/very/very/long/directory
pattern: */

${variable#pattern} usr/appl/abc/very/very/long/directory
${variable##pattern} directory
${variable%pattern} /usr/appl/abc/very/very/long/directory
${variable%%pattern} /usr/appl/abc/very/very/long/directory

Your current working directory (including the path) is stored in a variable called $PWD.

So, to get your current working directory only in a script you are writing, you can just write the following line:


MY_DIRECTORY=${PWD##*/}

Why did I write a script to do this? Because I can never remember how the pattern matching works, and thought it would be easier to write a script to show me instead.

Now, you’re probably asking me, “Splotchy, why would you put something in a program that many, including you, do not fully understand or remember how it works?”

As Matty Boy would say, that’s a great question, hypothetical question asker!

One thing that I neglected to mention about UNIX programming is that it is notoriously squirrelly. And this feature I am making use of is pretty damned squirrelly, too.

It Was 25 Years Ago Today

This Day In Usenet History: April 9, 1982
(according to a Google Groups search)

It was all in place, folks, lo those many moons ago.
UNIX, Deadheads and Leprechauns.

Negative gold
Group: net.games.rogue
First off , my thanks to Ken et.al. for providing us with 5.2. I have but one flame. It seems that a lot of the gold at lower levels, particularly any dropped by Leprechauns, 1. produces no message …

thoughts on a vi quiz
Group: fa.editor-p
From sdcsvax!draper@NPRDC Fri Apr 9 11:28:29 1982 Here are some comments on issues underlying Bill Mitchell’s recent quiz on vi – issues that are sort of obvious but perhaps would benefit from being raised explicitly. …

An APL quiz question
Group: net.lang.apl
The problem (changing a vector to a matrix with rows as separate words) is well-known. One solution appears in “APL: An Interactive Approach” by Gilman & Rose. Still another appears in APL News, V. 2 No. 1 (1977) in the APL Play column. …
Apr 9 1982 by G:shal… – 2 messages – 2 authors

A UNIX APL
Group: net.lang.apl
In reply to ihuxi!otto, I have used the APL here at Purdue EE, and it seems fairly good. The number quoted for concurrent users is a bit low. I have run APL while 45 other users were running APL …
Apr 9 1982 by pur-ee… – 1 message – 1 author

More on ASCII APL
Group: net.lang.apl
I do not see that the problem mentioned by ihuxi!otto is a serious one, except for a special case that I will mention. First, each APL character is a single entity, even if it is formed on a conventional …
Apr 9 1982 by rabbit!… – 2 messages – 2 authors

Run VMS Binaries
Group: net.general
I should have known … I could only be a matter of time … I have a request for a method to run VMS binaries under 4.1BSD UNIX. Has anyone already done this, or even currently working on it? How about tried it and failed? …
Apr 9 1982 by pur-ee!mah… – 1 message – 1 author

Quiz time
Group: net.lang.apl
A solution to this problem was given by Michael Halpern in his IBM technical report “Algebra, Scan, and Permutations”. I would be glad to mail copies of his functions to interested parties. /Jeff …
Apr 9 1982 by G:shal… – 2 messages – 2 authors

single elements vs. scalars
Group: net.lang.apl
In regards to Ned Horvath’s comments of april 7. I don’t know of any primative scalar function that does not distinguise between scalars and degenerate, one element, higher order arrays. Anyone care to find one? charles honton (CWRU)
Apr 9 1982 by cwruecmp!hon… – 1 message – 1 author

Dead Heads
Group: net.music
I’m new to the net; is net.music where music lovers of the One True Religion hang out? If not, then where?
Apr 9 1982 by G:w… – 1 message – 1 author

Whats a paradox?
Group: net.math
A very small clinic, of course. Sorry, I’ve been waiting years to use that joke in public. If you think I’m going to sign this…..
Apr 9 1982 by eagle… – 2 messages – 2 authors