Hi folks,

on my Debian box I'm running GNU bash, version 3.2.39(1)-release 
(i486-pc-linux-gnu).

Now, today I ran across a slight problem that I fail to understand. I have one 
script that acts as a library of a kind. I.e. it is being sourced by other 
scripts and performs some actions upon startup.

I have the following three files for a minimal working example of the problem 
(I trimmed a lot of error/prerequisite handling):
* test.sh (requires chmod a+x test.sh)
* common-bash-routines.sh (in the same folder)
* etc/defaults

test.sh uses common-bash-routines.sh as a library and common-bash-routines.sh 
is supposed to inspect the variable SCRIPTCONF to find out whether it is set. 
If it is set there are two choices (inner while loop):
1.) it's a file, it means it will get sourced
2.) it's a directory

Both cases can occur in several places (outer while loop). Since the paths can 
contain blanks, I resorted to a "while read" loop because "for" simply would 
tokenize the file names more than desirable.

Now since I would like to know whether I have successfully sourced *something* 
I was going to set a variable (SOURCED in my case). However, this variable does 
not get set in the outer or the inner loop. Also, using "export" instead of the 
normal variable assignment makes no difference whatsoever. How can I carry the 
information as to whether I have sourced *something* outside of the loops?


Thanks for any helpful pointers or hints,

// Oliver

PS:
Code contents follow:
---------------------------------------
###############################################
## test.sh (uses the library)
###############################################
#!/usr/bin/env bash
SCRIPTCONF=$PWD/etc
for i in . ${PATH//:/ }; do [[ -e "$i/common-bash-routines.sh" ]] && \
  source "$i/common-bash-routines.sh" && break; done

echo "Here is $BASH_SOURCE:$LINENO"


###############################################
## common-bash-routines.sh (this file is being sourced
###############################################
function errorexit()
{
  [ -n "$1" ] && echo -e \
    "\033[0;31mFATAL ERROR: $1 Can't continue!\033[0m\n"
  local ERRCODE=254
  [ -n "$2" ] && ERRCODE=$2
  exit $ERRCODE
}

SOURCED=''
if [[ -n "$SCRIPTCONF" ]]; then # if set
  echo -e "$SCRIPTCONF\n/etc/$SCRIPTCONF"|while read fname; do
    [[ -n "$fname" ]]   || continue
    if [[ -f "$fname" ]]; then
      source "$fname" || errorexit "Could not source ($fname)"
      SOURCED="$SOURCED:$srcname"
    elif [[ -d "$fname" ]]; then
      find "$fname/" -type f|while read srcname; do
        source "$srcname" || errorexit "Could not source ($srcname)"
        SOURCED="$SOURCED:$srcname"
      done
    fi
  done
  echo "$SOURCED"
  [[ -n "$SOURCED" ]] || errorexit "Could not *find* ($SCRIPTCONF).";
fi


###############################################
## etc/defaults
###############################################
# etc/defaults
echo "Here is $BASH_SOURCE"
---------------------------------------
(end of source snippets)

-- 
---------------------------------------------------
DDKWizard and DDKBUILD: <http://ddkwizard.assarbad.net>

Trunk (potentially unstable) version: 
<http://ddkwizard.assarbad.net/trunk/ddkbuild.cmd>


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: http://lists.debian.org/20100823211611.69...@gmx.net

Reply via email to