Sponsored Content
Top Forums Shell Programming and Scripting Emulate ksh's FPATH variable in bash Post 302451603 by bakunin on Tuesday 7th of September 2010 11:01:20 AM
Old 09-07-2010
Emulate ksh's FPATH variable in bash

Over time i have developed a library of useful (ksh) functions which i use in most of my scripts. I use the ksh's FPATH variable to locate all these functions and use a standard environment-setting-function to always have the same environment in all my scripts.

Here is how i begin scripts:

Code:
#!/bin/ksh
# foo.ksh

# main()

if [ -z "$DEVELOP" ] ; then                      # set environment
     . /usr/local/lib/ksh/f_env
else
     . ~/lib/f_env
fi

# ... rest of code ....

$DEVELOP is a variable i set if i want to use a local copy of the function library for testing purposes. "/usr/local/lib/ksh/f_env" looks like this:

Code:
if [ -z "$NEVER_USE_THIS_VAR" ] ; then           # recursion protection
    # .... some more settings ...

    if [ -z "$DEVELOP" ] ; then
          typeset -x FPATH="/usr/local/lib/ksh"  # set fnc path for fnc lib
          FPATH="$FPATH:/usr/local/bin"
          FPATH="$FPATH:/usr/local/sbin"
     else
          typeset -x FPATH=~/lib                 # for lib development
     fi

     # ...
     typeset -x NEVER_USE_THIS_VAR="KILROY_WAS_HERE"
fi

This mechanism is quite handy because many functions in the library depend on other functions in the same library and this way all these depencies are solved automatically.

I would like to make this library compatible with the bash shell (ideally i would be able to use these functions from ksh and bash equally), but bash lacks the FPATH mechanism. I came up with the following solution, but obviously it is quite unsatisfactory and i wonder if there is a better way to achieve my goal:

(within f_env(), after setting the FPATH variable: )

Code:
                                                 # simulate FPATH for bash
     if [ -n "$BASH_VERSION" ] ; then
          IFS=:
          for fFPathDir in $FPATH ; do
               for fFFile in $fFPathDir/* ; do
                    if [ $( \
                            sed -n '/^$/d;/^#/!{p;q}' $fFFile |\
                            grep -c '^\(function \)*[^ ][^ ]* ()' \
                          ) -gt 0 ] ; then
                          echo $fFFile
                         . $fFFile
                    fi
               done
          done
     fi

Basically i circle through FPATH, look into every file and if the first non-comment-line begins with "function <some-name> ()" (this is the sed-grep-line) i parse it into the environment. This looks quite clumsy, unreliable and downright ugly, but with my limited knowledge of bash i was not able to find a better solution. Your suggestions are welcome.

bakunin
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using FPATH and PATH together

If you specify the same directory in your FPATH and PATH variables, and you type in a "command" (e.g. hello), and there exists a file called hello in that common directory, will the shell first attempt to interpret this file as a function, and failing this, then re-attempt to interpret it as a... (2 Replies)
Discussion started by: DeMented
2 Replies

2. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies

3. Shell Programming and Scripting

Function Libraries using FPATH

At our site we have a function library that contains several functions that are called via FPATH. We're using ksh and have environment variables in our .profile that link us to these as follows: export CIRC_LIB=/apps/usr/circ/circ_lib export FPATH=$CIRC_LIB My questions are: When are the... (4 Replies)
Discussion started by: BCarlson
4 Replies

4. Shell Programming and Scripting

Script to emulate ls -lh?

Does anyone have a script they would like to share that emulates "ls -lh" in ksh on Solaris 8? Yeah, I know. Real men don't need that wimpy "h." Well, I'm a wimp. ;) (0 Replies)
Discussion started by: shew01
0 Replies

5. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

6. UNIX for Dummies Questions & Answers

bash preferred or ksh with bash features

I'm a user on a fairly locked down sys V server. By default, I have ksh set as my default shell. I added to my .profile: bash -o vi so when I login, it goes into bash so I can take advantage of tab completion and use the up key to bring up previous commands. However, whenever I want to exit, I... (2 Replies)
Discussion started by: mrwatkin
2 Replies

7. Shell Programming and Scripting

How to emulate ^S/^Q from a script

Hi, I wrote a little menu script that searches through another script you specify and displays step-names and next to it the text of the step. The scripts are converted JCL from mainframe. It alows you to select steps you want and will then create a new script which includes only the steps you... (5 Replies)
Discussion started by: AliceD
5 Replies

8. Shell Programming and Scripting

Variable to command to Variable Question KSH

Hello, First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID,... (3 Replies)
Discussion started by: Coyote270WSM
3 Replies

9. Shell Programming and Scripting

Variable scop ksh vs bash

I am not clear why the cnt variable is not increased in the script below: #!/bin/bash INPF=${1:-a.txt}; KWDS=${2:-lst} cnt=0; grep -v '^#' $KWDS | while read kwd; do grep -q $kwd $INPF; if ; then echo Found; ((cnt=cnt+1)); fi... (5 Replies)
Discussion started by: migurus
5 Replies

10. Shell Programming and Scripting

Bash FPATH code update

In this post at 302451613-post2.html the link to the code comes up not found. The thread is closed, so I was unable to ask on the thread itself and I do not have enough posts yet to send a private message (or write out a proper html link). Does the author (jim mcanamara) have an updated link? ... (2 Replies)
Discussion started by: matthewpersico
2 Replies
LIBBASH(7)							  libbash Manual							LIBBASH(7)

NAME
libbash -- A bash shared libraries package. DESCRIPTION
libbash is a package that enables bash dynamic-like shared libraries. Actually its a tool for managing bash scripts whose functions you may want to load and use in scripts of your own. It contains a 'dynamic loader' for the shared libraries ( ldbash(1)), a configuration tool (ldbashconfig(8)), and some libraries. Using ldbash(1) you are able to load loadable bash libraries, such as getopts(1) and hashstash(1). A bash shared library that can be loaded using ldbash(1) must answer 4 requirments: 1. It must be installed in $LIBBASH_PREFIX/lib/bash (default is /usr/lib/bash). 2. It must contain a line that begins with '#EXPORT='. That line will contain (after the '=') a list of functions that the library exports. I.e. all the function that will be usable after loading that library will be listed in that line. 3. It must contain a line that begins with '#REQUIRE='. That line will contain (after the '=') a list of bash libraries that are required for our library. I.e. every bash library that is in use in our bash library must be listed there. 4. The library must be listed (For more information, see ldbashconfig(8)). Basic guidelines for writing library of your own: 1. Be aware, that your library will be actually sourced. So, basically, it should contain (i.e define) only functions. 2. Try to declare all variables intended for internal use as local. 3. Global variables and functions that are intended for internal use (i.e are not defined in '#EXPORT=') should begin with: __<library_name>_ For example, internal function myfoosort of hashstash library should be named as __hashstash_myfoosort This helps to avoid conflicts in global name space when using libraries that come from different vendors. 4. See html manual for full version of this guide. AUTHORS
Hai Zaar <haizaar@haizaar.com> Gil Ran <ril@ran4.net> SEE ALSO
ldbash(1), ldbashconfig(8), getopts(1), hashstash(1) colors(1) messages(1) urlcoding(1) locks(1) Linux Epoch Linux
All times are GMT -4. The time now is 08:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy