Sponsored Content
Top Forums Shell Programming and Scripting Bash: Nested functions and including other scripts Post 302228915 by cfajohnson on Monday 25th of August 2008 06:46:40 PM
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?
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 11:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy