Bash: Nested functions and including other scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: Nested functions and including other scripts
# 1  
Old 08-24-2008
Java Bash: Nested functions and including other scripts

Hello.

I have the following problem with bash code:

Code:
function fl1_load_modules_and_get_list()
...........
	for module in $FL_MODULES_TO_PROCESS
	do
		source "${FL_MODULE_DIR}/${module}/module.sh"
	done
...........
}

function fl1_handle_install
{
	local FL_MODULES_TO_INSTALL=$(fl1_load_modules_and_get_list $1)
	
	#Executing pre-install routines
	for module in $FL_MODULES_TO_INSTALL
	do
		$(fl_mod_${module}_pre_install)
	done
}

First function is called inside the second one. First function loads scripts in which functions with predefined names reside. The second function calls the first and then attempts to call functions with names, that must be loaded with first one.
Loading files with source seem to complete successfully because I have
Code:
set -o errexit
set -o nounset
set -o pipefail

at the main script.

But when $(fl_mod_${module}_pre_install) call is done, I can see a message like /usr/local/fractal/fl/lib/handle_install.sh: line 8: fl_mod_php_pre_install: command not found
Somehow functions loaded by the first function are not imported into the global scope... Any idea why?

Thanks.
# 2  
Old 08-24-2008
Quote:
Originally Posted by FractalizeR
Hello.

I have the following problem with bash code:

Code:
function fl1_load_modules_and_get_list()

Use the standard syntax to define functions:

Code:
fl1_load_modules_and_get_list()

Quote:
...........

Please post real code so that it can be copied verbatim.
Quote:
Code:
	for module in $FL_MODULES_TO_PROCESS
	do
		source "${FL_MODULE_DIR}/${module}/module.sh"
	done
...........
}

function fl1_handle_install
{
	local FL_MODULES_TO_INSTALL=$(fl1_load_modules_and_get_list $1)
	
	#Executing pre-install routines
	for module in $FL_MODULES_TO_INSTALL
	do
		$(fl_mod_${module}_pre_install)


Do you really want to execute the output of the command?
Quote:
Code:
	done
}

First function is called inside the second one. First function loads scripts in which functions with predefined names reside. The second function calls the first and then attempts to call functions with names, that must be loaded with first one.
Loading files with source seem to complete successfully because I have
Code:
set -o errexit
set -o nounset
set -o pipefail

at the main script.

But when $(fl_mod_${module}_pre_install) call is done, I can see a message like /usr/local/fractal/fl/lib/handle_install.sh: line 8: fl_mod_php_pre_install: command not found
Somehow functions loaded by the first function are not imported into the global scope... Any idea why?
# 3  
Old 08-25-2008
Here is the source. I would appreciate if you will help me to get it working. I am starting to learn bash and this seem to be a good exercise for me.
# 4  
Old 08-25-2008
I have forgotten to add
execute
Code:
f-linsey install all

And the error I was talking about appears.
# 5  
Old 08-25-2008
Quote:
Originally Posted by FractalizeR
I have forgotten to add
execute
Code:
f-linsey install all

And the error I was talking about appears.
$ f-linsey install all
bash: f-linsey: command not found
$ ./f-linsey install all
bash: ./f-linsey: No such file or directory

Last edited by cfajohnson; 08-25-2008 at 07:24 PM..
# 6  
Old 08-25-2008
Quote:
Originally Posted by FractalizeR
Here is the source. I would appreciate if you will help me to get it working. I am starting to learn bash and this seem to be a good exercise for me.

This is overly complex for a learning exercise.

I would recommend that you learn POSIX shell scripting first, then add bash extras as you find them necessary or helpful.

I have gone over the top level script from the zip file and made a few comments:

Code:
#!/bin/sh


Since you use bash-specific code in the script, you should specify
bash in the shebang.
Code:
# Setting bash options
set -o errexit
set -o nounset
set -o pipefail

# Getting full path to current script
prg=$0
if [ ! -e "$prg" ]; then
  case $prg in
    (*/*) exit 1;;
    (*) prg=$(command -v -- "$prg") || exit;;
  esac
fi


There is rarely, if ever, a good reason to need the location of the running script.

When there is, it should be done in a wrapper that is in a standard location, e.g., /usr/local/bin/linsey, and contain something like:
Code:

cd /path/to/working/directory
./f-linsey.sh

Code:
# Setting variables
FL_THIS_DIR=$(
  cd -P -- "$(dirname -- "$prg")" && pwd -P
) || exit


Why do you need $FL_THIS_DIR?

In a POSIX shell (which bash is), the current directory is always in ${PWD}.
Code:
FL_LIB_DIR="${FL_THIS_DIR}/lib"
FL_FLINSEY_VERSION="1.0"

# Loading libraries
if [ ! -d "${FL_LIB_DIR}" ];
then
	echo "ERROR: library directory does not exist. F-Linsey was not properly installed."
	exit 1
fi

# Including important library files
source "${FL_LIB_DIR}/errors.sh"
source "${FL_LIB_DIR}/handle_help.sh"
source "${FL_LIB_DIR}/modules.sh"

# Checking if modules directory exist
FL_MODULE_DIR="${FL_THIS_DIR}/modules"
if [ ! -d "${FL_MODULE_DIR}" ]
then
	fl1_err_no_modules


Using a function for a trivial piece of code that is only used once
makes the script harder to understand.

Put the body of the function in-line; it's only two lines.
Code:
fi
# Parsing command line

# Checking if parameters passed
if [ $# -lt 1 ]
then
	fl1_handle_help


You should keep lines shorter than 80 characters for your help text.
Code:
fi
# Reacting to command
FL_COMMAND=$1
shift

# Checking if command handler exists
if [ ! -f "${FL_LIB_DIR}/handle_${FL_COMMAND}.sh" ]
then
	fl1_err_unknown_command $FL_COMMAND
fi


Ditto.
Code:
# Loading command handler file and calling handler
source "${FL_LIB_DIR}/handle_${FL_COMMAND}.sh"


Rather than bash-specific code, use the portable dot command:
Code:

. "${FL_LIB_DIR}/handle_${FL_COMMAND}.sh"

Code:
fl1_handle_${FL_COMMAND} $*

echo "F_Linsey finished."


Now, which line causes the error?
# 7  
Old 08-25-2008
Thank you for valuable comments!

Sorry, I meant
./f-linsey.sh install all
Of course, it wouldn't start without .sh.

Line, which causes error is
Code:
fl_mod_${module}_install

in lib/handle_install.sh

It just says, that command not found. But specific module /modules/httpd/module.sh should be loaded to that moment.

I declared $FL_THIS_DIR to make modules and library directory from it to ensure script can be executed from every place in filesystem, not only from current directory. But now I think I don't need it Smilie
I am not yet familiar with Linux and this style of code seem to come from PHP Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call functions from other scripts

i have a file that contains functions and i want the functions to be available in another script. of course, the ideal situation here would be to put the functions in the same script, but i dont own these scripts. so I have to call the functions file from a different script. how do i... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. UNIX for Dummies Questions & Answers

Nested loop -bash

I am using the following nested loop for i in {1..3} do for y in {1..3} do if ; then echo P0${i}R${y}.fas mv P0${i}R${y}.fas P${i}R${y}.fas read -t 5 fi done done I was wondering if I can use a character such as * or ? instead of my second variable y. I tried R in... (3 Replies)
Discussion started by: Xterra
3 Replies

3. AIX

including netsnmp with rc scripts

Hi Admins, I have configured net-snmp with my aix 5.3 server. how to add the same in rc scripts,so that net-snmp will start automatically post server reboot. As per now , i have to start snmp manually after server reboot. Thanks in advance newaix (0 Replies)
Discussion started by: newaix
0 Replies

4. Shell Programming and Scripting

Nested for loop in bash

Hi, How to use values in one for loop to other for loop. say "$sf_rel" variable has values "2011/W2 2011/G2" I want to use these values in inner for loop to process properly. $branch variable has G2 and 6 What is happening is outer for loop $i has 2011/W2 it is entering into inner... (3 Replies)
Discussion started by: Anjan1
3 Replies

5. Shell Programming and Scripting

Nested if question BASH

Just started learning bash ,and I am confused with sintaksis line 16: syntax error near unexpected token `else' thanks #!/bin/bash echo -n "Enter: " read num if(($(echo ${#num}) == 0 )) then echo No arguments passed.Try again elif rem=$(echo $num | tr -d ) ... (7 Replies)
Discussion started by: lio123
7 Replies

6. Shell Programming and Scripting

Changing the Bash Scripts to Bourne Scripts:URGENT

Hi, I have to write a program to compute the checksums of files ./script.sh I wrote the program using bash and it took me forever since I am a beginner but it works very well. I'm getting so close to the deadline and I realised today that actually I have to use normal Bourne shell... (3 Replies)
Discussion started by: pgarg1989
3 Replies

7. Shell Programming and Scripting

nested functions search

I want to write a shell script which traverses a cpp file. Suppose there is function fncn_name6 .. which is called by fncn_name5 which in turn called by fncn_name4 and so on .. in a single cpp class. ie fncn_name1 { fncn_name2 { fncn_name3 } { fncn_name4 ... (0 Replies)
Discussion started by: ultimatix
0 Replies

8. Shell Programming and Scripting

Scripts in ~/bin vs. functions in ~/.bashrc

Hi there, Anyone knows what would be the cons and pros of adding a script in ~/bin vs. a function in ~/.bashrc? I'm not sure how the system keeps tracks of some of the settings loaded in ~/.bashrc (like functions and aliases). Would I be right in thinking that this would all be loaded into... (2 Replies)
Discussion started by: victorbrca
2 Replies

9. Shell Programming and Scripting

How to call C functions in shell scripts?

How can i call dynamic C functions in shell scripts? (5 Replies)
Discussion started by: agarwal
5 Replies

10. Shell Programming and Scripting

Calling functions in scripts directly

Hi, I have a menu driven script that does various tasks, I want to be able to call functions directly from within other unix scripts from my menu script. Is this possible? (12 Replies)
Discussion started by: LiquidChild
12 Replies
Login or Register to Ask a Question