plug-discuss at lists.plug.phoenix.az.us

Eric "Shubes" plug at shubes.net
Wed Jan 3 10:41:36 MST 2007


Sourcing a script is indeed a handy feature. I've used it to invoke a script
that sets environment variables that need to 'stick'. It indeed can be used
to define functions as well, creating a library.

If there's an occasion where your script needs to be invoked both ways
(sourced and 'normal') and the script needs to return/exit (with a return
value), here's a way I've used to accomplish this:

myname=name-of-this-script
if [ "`basename $0`" == $myname ]; then
  retexit=exit
else
  retexit=return
fi

Then simply "$retexit 0" where appropriate, and the script will either exit
or return depending on whether it was sourced or not. A library would always
need to be sourced of course, but there might come a time where you'd want
to be able to invoke a script either either way.

My .02

Shawn Badger wrote:
> For the people who don't already receive this!!
>  Check out the "TECH TIP: #12 Building a Shell Script Library"
>  I didn't know about the ". SCRIPT.SH" would run a script as if you
> typed it into the shell. Some thing new to me!!
> 
>  -------- Forwarded Message --------
>  From: Linux Journal News Notes <newsletter at linuxjournal.com>
>  To: lj-announce at ssc.com
>  Subject: Linux Journal Weekly Newsletter - January 3
>  Date: Wed, 03 Jan 2007 04:32:00 -0600
> 
> 
> 
> 
> 
>                  Linux Journal Weekly News Notes -- January 3, 2007
> 
>                                      Sponsor:  Appro
> 
>    A Better Way to Keep your Cool in the Data Center
> 
>    A Better Way to Keep your Cool in the Data Center Providing adequate cooling
>    for data center equipment is increasingly critical for reducing downtime,
>    extending equipment life and optimizing energy use. The combination of
>     Appro's server solutions with Fluent's thermal analysis provides enterprise
>    business customers with reliable and cost-effective high-performance IT
>    infrastructure.   Learn More:
>    http://www.appro.com/whitepaper/datacenter%20cooling_white%20paper.asp
>  _________________________________________________________
> 
> 
>  Happy New Year, Everyone!
> 
>    Welcome to the January 3rd, 2007, edition of Linux Journal Weekly News
>    Notes. Although we're gradually revving back up to business normalcy here,
>    our editors have been posting like mad over the last week. Give these guys
>    some free time and all they can do is think Linux! We're sure you'll enjoy
>    the links to great stories, musings and tech tips.
> 
>    We also hope that this newsletter finds you itching to send us your
>    fascinating contributions, such as tech tips, hidden Linux sightings and
>     cool dot-orgs that are making the Linux Community ever richer. Repeat after
>    me: "My new year's resolution for 2007 is to contribute to Linux Journal
>    very, very, very often!
> 
>    We wish you only good things...and lots of Linux fun and success in 2007
> 
>    James Gray and The Linux Journal Editorial Team
>    jgray at linuxjournal.com
> 
> 
>  FEATURED LINKS
> 
>     The Ultimate Distro by Glyn Moody
>    http://www.linuxjournal.com/node/1000150
>     Glyn talks about the alpha release of Ulteo, a new distro from Gael Duval,
>     the chap who created Mandrake in 1998. Ulteo is a self-upgrading,
>    easy-to-use Linux distribution. Glyn's point is to show how dynamic the
>    world of Linux distributions remains today.
> 
>     Can We Relate? by Doc Searls
>    http://www.linuxjournal.com/node/1000153
>    One of Doc's passions these days is enabling Vendor Relationship Management
>    -- i.e. relating to vendors productively, on mutually agreeable terms rather
>    than just paying them money for whatever they're selling -- and exciting
>    things are happening. Here's a summary of the latest developments and Doc's
>     work with Harvard's Berkman Center for Internet and Society.
> 
>     Formatting Cells in OpenOffice.org Calc by Bruce Byfield
>    http://www.linuxjournal.com/node/1000154
>     Bruce continues his pragmatic, useful series on OpenOffice.org, this time
>    focusing on formatting your data.
> 
>     Ruby in 2006 -- A Retrospective Collection by Pat Eyler
>    http://www.linuxjournal.com/node/1000155
>    Pat has compiled a retrospective of Ruby retrospectives for 2006, making
>    totally sure you miss nothing important from the past year.
> 
>    The Buzz About Aldrin by Dave Phillips
>    http://www.linuxjournal.com/node/1000156
>     For the past month, Dave has been playing with Leonard 'paniq' Ritter's
>    "Aldrin", a music production system that combines a tracker-style
>    composition interface with audio synthesis and processing modules called
>    machines. Here's his report.
> 
>    Directory Services as the Foundation of Organizational Infrastructures by
>     Tom Adelstein
>    http://www.linuxjournal.com/node/1000157
>     Tom muses about the role of LDAP as a foundation for enterprise-level
>    infrastructures.
> 
>     Happy New Year - What's Ahead? by Tom Adelstein
>    http://www.linuxjournal.com/node/1000158
>    Here's another post by Tom. This time he's taking an end-of-year,
>    start-of-year perspective, looking at where he's come from and where he
>    wants to take himself in 2007. It's always fascinating to read each other's
>    histories and dreams for new ideas and perspectives.
> 
> 
>  THE BRAIN TRUST: READERS SHARE THEIR EXPERTISE
> 
>    Thanks to everyone for your forthcoming technical tips! Please send more of
>    them to share with our community of readers! My email is
>    jgray at linuxjournal.com. We'll send you a free t-shirt for your efforts.
>    Thanks!
> 
> 
>  FROM THE ARCHIVES
> 
>     LJ Interviews Linus Torvalds (1996) by Phil Hughes and Gena Shurtleff
>    http://www.linuxjournal.com/article/0146
>    This interview with the father of Linux, Linus Torvalds, occurred in 1996,
>    just after Version 2.0 was released. This is the best kind of trip down
>    memory lane for us Linux geeks, isn't it?
> 
> 
>  LINUX INCOGNITO: THERE'S LINUX INSIDE!
> 
>    Our readers want to hear about your interesting experiences uncovering Linux
>    in an unexpected or innovative situation. Email them to me at
>    jgray at linuxjournal.com. If your contribution is selected for
>    publication, we'll send you a t-shirt!
> 
> 
>  DOT ORG OF THE WEEK
> 
>    Do you know of an organization or person making a unique contribution to the
>    Linux community despite not getting paid for it? If so, we'd like to share
>    information about them with our readers. Send your recommendation, along
>    with why the organization/people are worthy of recognition, to
>    jgray at linuxjournal.com.
> 
> 
>  TECH TIP: #12 Building a Shell Script Library
> 
>    This tip comes courtesy of Linux Journal columnist Dave Taylor and No Starch
>    Press.  This is the 10th in a series of Tech Tips on shell scripting from
>    Dave where he explains the "how it works" factor behind the script.
> 
>    Many of the scripts in this chapter have been written as functions rather
>    than as stand-alone scripts so that they can be easily and gracefully
>    incorporated into other scripts without incurring the overhead of making
>    system calls. While there's no #include feature in a shell script, as there
>    is in C, there is a tremendously important capability called sourcing a file
>    that serves the same purpose.
> 
>    To see why this is important, let's consider the alternative. If you invoke
>    a shell script within a shell, by default that script is run within its own
>     subshell. You can immediately prove this experimentally:
> 
>        $ cat tinyscript.sh
>       test=2
>       $ test=1
>        $ tinyscript.sh
>       $ echo $test
>       1
> 
>    Because this script changed the value of the variable test within the
>     subshell running the script, the value of the existing test variable in the
>    current shell's environment was not affected. If you instead use the "."
>    source notation to run the script, it is handled as though each command in
>    the script was typed directly into the current shell:
> 
>        $ . tinyscript.sh
>       $ echo $test
>       2
> 
>    As you might expect, if you have an exit 0 command within a script that's
>    sourced, for example, it will exit that shell and log out of that window.
> 
>    The Code
> 
>    To turn the functions in this chapter into a library for use in other
>    scripts, extract all the functions and concatenate them into one big file.
>    If we call this file library.sh , a test script that accesses all of the
>    functions might look like this:
> 
>       #!/bin/sh
> 
>       # Library test script
> 
>       . library.sh
> 
>        initializeANSI
> 
>        echon "First off, do you have echo in your path? (1=yes, 2=no) "
>       read answer
>        while ! validint $answer 1 2 ; do
>        echon "${boldon}Try again${boldoff}. Do you have echo "
>        echon "in your path? (1=yes, 2=no) "
>       read answer
>       done
> 
>        if ! checkForCmdInPath "echo" ; then
>       echo "Nope, can't find the echo command."
>       else
>       echo "The echo command is in the PATH."
>        fi
> 
>       echo ""
>        echon "Enter a year you think might be a leap year: "
>       read year
> 
>        while ! validint $year 1 9999 ; do
>        echon "Please enter a year in the ${boldon}correct${boldoff} format: "
>       read year
>       done
> 
>        if isLeapYear $year ; then
>        echo "${greenf}You're right!  $year was a leap year.${reset}"
>       else
>        echo "${redf}Nope, that's not a leap year.${reset}"
>        fi
> 
>       exit 0
> 
>    Notice that the library is incorporated, and all functions are read and
>    included in the run-time environment of the script, with the single line
> 
>       . library.sh
> 
>    This is a useful approach in working with the many scripts in this book, and
>    one that can be exploited again and again as needed.
> 
>    Running the Script
> 
>    To run the test script given in the previous section, simply invoke it at
>    the command line.
> 
>    The Results
> 
>       $ library-test
>       First off, do you have echo in your path? (1=yes, 2=no) 1
>       The echo command is in the PATH.
>       Enter a year you think might be a leap year: 432423
>       Your value is too big: largest acceptable value is 9999
>       Please enter a year in the correct format: 432
>       You're right!  432 was a leap year.
> 
>    On your computer screen, the error messages just shown will be a bit more
>    blunt because their words will be in bold, and the correct guess of a leap
>    year will be displayed in green.
> 
>     Excerpted with permission from the book Wicked Cool Shell Scripts: 101
>     Scripts for Linux, Mac OS X, and UNIX Systems by Dave Taylor.  Published by
>     No Starch Press.  http://www.nostarch.com/wcss.htm.
> 
>     Dave Taylor is a long-time Unix and Linux geek and runs the popular
>     http://www.AskDaveTaylor.com/ tech support blog.  His book Wicked Cool
>     Shell Scripts can be found at: http://www.intuitive.com/wicked/ and the
>     entire library of scripts at:
>     http://www.intuitive.com/wicked/wicked-cool-shell-script-library.shtml.
> 
> 
>  FEATURED EVENTS
> 
>    The Southern California Linux Expo (Feb 10-11, 2007)
> 
>    "We are bringing businesses, academic institutions and the Linux community
>    together in a way that no other conference does!"
>    http://www.socallinuxexpo.org
> 
>     Join us February 14-15, 2007 for LinuxWorld OpenSolutions Summit
> 
>     A new conference from the producers of LinuxWorld. OpenSolutions Summit will
>    feature two days of peer-to-peer case studies, technical training, and
>    insightful keynotes that will provide best practices and the latest
>    innovations across the enterprise.
>    http://www.linuxworldsummit.com/
> 


-- 
-Eric 'shubes'


More information about the PLUG-discuss mailing list