Bash: Nested functions and including other scripts
Hello.
I have the following problem with bash code:
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
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?
Please post real code so that it can be copied verbatim.
Quote:
Do you really want to execute the output of the command?
Quote:
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
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?
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:
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:
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
I am not yet familiar with Linux and this style of code seem to come from PHP
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)
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)
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)
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)
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)
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)
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)
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)
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)