LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-10-2012, 05:24 AM   #1
Michal Krzyz
Member
 
Registered: Nov 2011
Distribution: OpenBSD, Ubuntu, Debian, Slackware, LFS
Posts: 44

Rep: Reputation: Disabled
Bash multiple include/source problem


Hello

I am writing installation script with following files' hierarchy:


build_scripts/
build_scripts/build_all.sh <- should contain source config.in
build_scripts/temporary_toolchain/build_all.sh <- should contain source config.in
build_scripts/system_build/build_all.sh <- should contain source config.in
...

build_scripts/config_files/
build_scripts/config_files/config.in <- should contain source error_handler.in source paths.in etc.
build_scripts/config_files/error_handler.in <- should contain output_messages.in
build_scripts/config_files/output_messages.in
build_scripts/config_files/paths.in
...


how can I include config.in from any build_all.sh script and then from config.in include for example paths.in knowing only relative path between config.in and paths.in.
HINT: relative path between script including config.in and config.in path depends on path of currently executing build script


Is there any nice solution?

I found one which is not very nice: call each build script from one path (for example build/) and then use relative path between build/ and each included script.
 
Old 12-10-2012, 05:57 AM   #2
Michal Krzyz
Member
 
Registered: Nov 2011
Distribution: OpenBSD, Ubuntu, Debian, Slackware, LFS
Posts: 44

Original Poster
Rep: Reputation: Disabled
I found one solution. It is not very nice because each included script should contain couple lines of code, but it works.

This link was helpful
http://stackoverflow.com/questions/5...-its-stored-in

finally I added following code to each included file:

Code:
SOURCE="${BASH_SOURCE[0]}"
DIR="$( dirname "$SOURCE" )"
while [ -h "$SOURCE" ]
do 
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
  DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd )"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
And I am doing this kind of stuff in the config.in:

Code:
#!/bin/bash

function find_current_script_dir()
{
  SOURCE="${BASH_SOURCE[0]}"
  DIR="$( dirname "$SOURCE" )"
  while [ -h "$SOURCE" ]
  do
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
    DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd )"
  done
  echo "$( cd -P "$( dirname "$SOURCE" )" && pwd )"
}

LFS_BUILD_CONFIG_SCRIPTS_DIR="$(find_current_script_dir)"

source $LFS_BUILD_CONFIG_SCRIPTS_DIR/output_messages.in
source $LFS_BUILD_CONFIG_SCRIPTS_DIR/error_handling.in
source $LFS_BUILD_CONFIG_SCRIPTS_DIR/paths.in
and in similar way I included output_messages.in in error_handling.in
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
include path for multiple versions of gcc hydrogeek Linux - General 5 11-18-2007 02:08 PM
Include source problem with sh elmu Programming 5 06-20-2007 08:53 AM
Bash backup script - If multiple files starting with a exist problem demoncheese Programming 2 07-29-2004 10:47 PM
multiple #include 'Error' Hady Programming 6 02-07-2004 09:53 AM
Bash commands not include??? fxlee Linux - Newbie 4 10-26-2001 10:51 PM


All times are GMT -5. The time now is 07:34 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration