Bash functions sequence ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash functions sequence ?
# 8  
Old 09-01-2018
Quote:
Originally Posted by annacreek
So the bottom like is - I have to clean -up my own mess. O well...
Writing scripts is software development - programming - so all the do's and dont's of programming apply.

Programming is in my experience mostly about bringing your thought process into order. It always helped me to standardise and conventionalise as much as i can: i have a system for naming variables so that the name immediately tells me what it should contain and what type it is (note that shell variables aren't really typed but if you try to multiply a string by 5 it still leads to an error). I have a naming convention for my functions so i cannot name two functions the same name. And so on, and so on.

Picture programming as building a house but with the twist that you have to build the bricks for that first: if your bricks are all neat cubes or cuboids it is easy to build straight walls and it is equally easy to foresee if the wall will hold or not. If your bricks are all irregular shapes your walls will be equally irregular shapes and it might be impossible to calculate if they are going to hold or not until they come crushing down.

Quote:
Originally Posted by annacreek
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.
To be honest i have no idea what your question is. Please explain again. If it is a new question and unrelated to the topic of this thread please open a new thread. We like to organise our threads so that they are about one topic only.

Quote:
Originally Posted by wisecracker
What a neat idea, I like it...
They make similar functions in other scripts effectively redundant.
In Korn shell i have built myself a "library" using the FPATH variable and strive to write my functions in a way so that they can be included in this library if possible. When i started programming one of the first things i learned is write your functions always in a way so that they can be put into a library and i still try to adhere to that.

I also have a standard header for my function files which you might want to adopt/adapt. I always found it useful to have a standard format for documentation so that i know immediately where to look for specific information (like with man pages). As i use the same header for functions and scripts i have a "USAGE"-part for the user documentation and a "DOCUMENTATION"-part for the internal documentation:

Code:
# ------------------------------------------------------------------------------
# template.ksh                               template for ksh scripts/functions
# ------------------------------------------------------------------------------
# Author.....: 
# last update: 0000 00 00    by:
# ------------------------------------------------------------------------------
# Revision Log:
# - 0.99   0000 00 00   Revision title
#                       Revision description
#
# ------------------------------------------------------------------------------
# Usage:
#
#     Example:
#
# Prerequisites:
#
# ------------------------------------------------------------------------------
# Documentation:
#
#     Parameters:
#     returns:
# ------------------------------------------------------------------------------
# known bugs:
#
#     none
# ------------------------------------------------------------------------------
# ..............................(C) 2018 bakunin ...............................
# ------------------------------------------------------------------------------

I hope this helps.

bakunin

Last edited by bakunin; 09-01-2018 at 02:44 AM..
# 9  
Old 09-01-2018
I can understand that you prefer ksh over bash in general, but I don't see how for this particular case, ksh would give an advantage over bash. I must admit that I'm not proficient at all in ksh, and would appreciate an explanation of this matter.
# 10  
Old 09-01-2018
Thanks Bakunin.
I need to restate my "primary objective" was and still is to modify existing bash script,
I started with 2000+ lines and now it is doubled!

I realize I am asking stupid and basic questions, however I have no intention to make carrier out of writing bash scripts.

But I am thankful for all the support I have recieved so far.
# 11  
Old 09-01-2018
Quote:
Originally Posted by rovf
I can understand that you prefer ksh over bash in general, but I don't see how for this particular case, ksh would give an advantage over bash. I must admit that I'm not proficient at all in ksh, and would appreciate an explanation of this matter.
Its a bit off-topic, but i'll bend the rules slightly for you, as it is (remotely) connected with the problem here:

In ksh there is a Variable FPATH, which works just like PATH for executables, but for functions. Once set the shell will seek undeclared functions along this path(es) like it would search for unqualified executables along the contents of the PATH variable. This makes it easy to create a directory with standardised functions used in many scripts - just like a library in high-level-languages. I use this mechanism a lot and have about 50 functions in my "library" which i use over and over. It makes organising (and improving!) ones work a lot easier than copying and pasting all the "standard stuff" from one script to the next.

Suppose you have 50 scripts where you have copied and pasted alls sorts of functions into. Now you come up with an (internal) improvement to one of those functions. Are you gonna copy and paste it again through all those scripts? I can improve my library functions easily and all scripts using it will profit from that immediately.

@annacreek
I said it above but you probably missed it: i do not understand what your question is. Please explain again what you want to achieve and I'll gladly help.

I hope this helps.

bakunin
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