Bash functions sequence ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash functions sequence ?
# 1  
Old 08-30-2018
Bash functions sequence ?

OK,

I know function has to be defined first - in sequence - before it can be used.

So the script has to be build "bottoms -up style, if you pardon my expression.

I am running into a problem reusing function and breaking the sequence.

It would be nice to be able to see the function usage tree as used in script.


Is there such a thing /application ?



Or - will bash complain when I have multiple definitions of a function to keep them in sequence?
# 2  
Old 08-30-2018
Quote:
Originally Posted by annacreek
OK,

I know function has to be defined first - in sequence - before it can be used.

So the script has to be build "bottoms -up style, if you pardon my expression.

I am running into a problem reusing function and breaking the sequence.

It would be nice to be able to see the function usage tree as used in script.


Is there such a thing /application ?



Or - will bash complain when I have multiple definitions of a function to keep them in sequence?
I don't understand what you're trying to do. How does having multiple definitions of a function keep anything in sequence?

If you have defined a function more than once, the last function definition command executed for that function will be the one used when that function is invoked.
# 3  
Old 08-30-2018
Quote:
Originally Posted by Don Cragun
I don't understand what you're trying to do. How does having multiple definitions of a function keep anything in sequence?

If you have defined a function more than once, the last function definition command executed for that function will be the one used when that function is invoked.
Yuck, that sounds like a maintenance nightmare.

It is quite common to see a main function at the top a the script. Like this:

Code:
main() {
     foo
     bar
}

foo() {
   # code for foo
}

bar() {
   # code for bar
}

# Pass script arguments into main for argument parsing
main $@

But you still need to keep the functions in the correct order, i.e callee before caller.
# 4  
Old 08-31-2018
There is no need to have the functions in strict order. A function just needs to be known when it is invoked. Hence, if you put all the functions on the top of your script, you can arrange them in any order.
# 5  
Old 08-31-2018
To be honest i prefer Korn shell over bash on any day of the week and twice on Sundays and this (the missing FPATH) is one of the reasons.

Still it is possible to get some sort of order in bash too: put every function in its own separate file, then use the source command to load these at the beginning of the script. This way you can't ever have two functions with the same name because the filesystem has distinct entries.

Note that this only pays off in big script projects. Scripts with 50-100 lines are easy to oversee but once the script gets several thousands of lines it is equally easy to lose track.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 6  
Old 08-31-2018
So the bottom like is - I have to clean -up my own mess. O well...

------ Post updated at 11:14 AM -----




Right church wrong pew -

sorry I do not know how my NEW post got here.




OK, I have not got the full grasp of this "standard " input / output /error process.



I have manged to append (two) variables to a file.

I know how to reverse their order using tac.

I can see the "standard output" in terminal. - output from tac.

I like to "send" each line to $1 and $2 respectively. Actually to an array of $, but I'll tackle that later.



I know how to do "while read" , I like to learn more by using tac directly.



Here is the base I have so far , not much.



Code:
echo "$LINENO  reverse  file entries  FIFO "
tac $PWD$DEBUG_DIR$DEBUG_MENU 
#ouputs lines to terminal FIFO 
echo "$LINENO  reverse  file entries  FIFO "


Last edited by annacreek; 08-31-2018 at 01:20 PM.. Reason: wrong post place
# 7  
Old 08-31-2018
Hi bakunin...

Quote:
Still it is possible to get some sort of order in bash too: put every function in its own separate file, then use the source command to load these at the beginning of the script. This way you can't ever have two functions with the same name because the filesystem has distinct entries.
What a neat idea, I like it...
They make similar functions in other scripts effectively redundant.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling Bash Functions in the BG

I'm trying to call some functions in the background so that I can multitask in this script. Not working so hot. The functions I call don't ever seem to get called. I'm doing it the exact same way in another script and it's working like a champ so I'm very confused. Here's a pretty simple repro: ... (7 Replies)
Discussion started by: stonkers
7 Replies

2. Shell Programming and Scripting

functions and variables in bash

I have a bash script with some functions as below and am wondering if I can use the variables declared in setup in the other functions and in the rest of the bash script. setup(){ none=0; low=1; medium=2; high=3; debug=4 var1="red" var2="fred" } create_basemap() { ... (7 Replies)
Discussion started by: kristinu
7 Replies

3. Shell Programming and Scripting

Press Any Key script sequence using bash - HELP

hi to all. im a newbie in unix shell scripts. i want to make a simple unix shell script using the bash shell that asks a user to press any key after a series of commands, or an x if he wishes to exit. here's a sample script that i made: #!/usr/bin/bash pause(){ /usr/bin/echo "\t\t Press... (3 Replies)
Discussion started by: booghaw
3 Replies

4. UNIX for Dummies Questions & Answers

bash script to parse sequence...

Hi, I have 4000 list files and 4000 sequence data files. Each list file contains a number of 'headers' and data file contains 'header and data'. I would like to extract data from the data file using the list file and write into a new file. As each of the files are quite large, an efficient piece... (6 Replies)
Discussion started by: Fahmida
6 Replies

5. UNIX for Dummies Questions & Answers

create time sequence in bash

hi all, hope someone can assist in this. I'm trying to make a sequence of time in bash so that the output prints the following; 1950-01-01 1950-02-01 1950-03-01 ... 1999-11-01 1999-12-01 In R, i can issue the following command; t1 <- ISOdate(1950,1,1,0,0,0) t2 <-... (0 Replies)
Discussion started by: Muhammad Rahiz
0 Replies

6. Shell Programming and Scripting

Functions, exit, and kill in bash

Hello Okay, for reasons related to sourcing a script from another script, I've had to put my main loop into a function, and from there I call other functions. My problem then is exiting from deep within the function call stack. I used to simply call exit, and that would accomplish what I... (1 Reply)
Discussion started by: brsett
1 Replies

7. Shell Programming and Scripting

bash functions arguments

This script is called fuu; #!/bin/bash speak() { case $1 in 1)echo one ;; 2)echo two ;; 3)echo three ;; esac } speak exit 0 when i run fuu 2 i expect "two" like... (2 Replies)
Discussion started by: Tártaro
2 Replies

8. Shell Programming and Scripting

Mathematical functions in bash shell

Hi, How can i do the mathematical calculations in bash shell? Are the mathematical functions available in bash shell? Ex: pow ceil floor sqrt (5 Replies)
Discussion started by: cola
5 Replies

9. Shell Programming and Scripting

[bash] reassigning referenced variables in functions

Hello all, Problem. ---------- I'm trying to reassign a referenced variable passed to a 'local' variable in a function but the local variable refuses to be assigned the content of the referenced variable. Any ideas would be appreciated. Objective. ----------- Eliminate all $VAR... (1 Reply)
Discussion started by: ASGR
1 Replies

10. Shell Programming and Scripting

Bash: Nested functions and including other scripts

Hello. I have the following problem with bash 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... (12 Replies)
Discussion started by: FractalizeR
12 Replies
Login or Register to Ask a Question