Functions in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Functions in a shell script
# 1  
Old 05-21-2014
Functions in a shell script

so i have a very big script that has the following format:

Code:
functionA () {

....
...
....

}

Results=$(functionA)

the code inside of functionA is very huge. so by the time the script gets to the "Results=" part, several seconds have already passed. the script is about 15MB in size.

how can i reorganize this so the script runs faster?

i didnt write this script but i have to make it run faster one way or another. any ideas?

i tried this:
Code:
....
...
.... | while read functionA
do
...
...
done

notice i got rid of the function and i just let the script run. the thought is to avoid making the script skip the huge functionA code and just have it run as soon as it is kicked off.
# 2  
Old 05-21-2014
I think if you have a 15MB shell script it's going to be slow no matter what. So if speeding things up is important the best approach is, unfortunately, to redesign the script and break functions into separate pieces. If you do that in a way that makes sense, by creating separate scripts that perform logical sub-tasks, then only the parts that need to run at any given time will be executed. It will probably be easier for you and whoever else inherits it to maintain going forward as well.

If you really have ONE shell function that's around 15MB, that's likely to be a painful redesign though.

And if the entire thing has to run each time, then breaking it up won't help much either. In that case, I think converting to another language that's compiled (C) or at least pre-parsed (python) would be your best bet.
This User Gave Thanks to cnamejj For This Post:
# 3  
Old 05-21-2014
If you are using bash, and if there's no bash-specific code, try dash or ksh93. Bash isn't an efficient shell.

We can't possibly make any targeted recommendations without knowing something about the code beyond it's a large script.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 05-22-2014
What sort of processing are you doing? If there are lots of files being read & re-read, that will cost time. If there are lots of calls to external programs (grep, cut, tr, sed, awk etc.) in loops, then that will cost time.

For example are you reading a 10,000 line input file and for each line you use grep to scan another large file or set of files for each one, then there may be a better way to re-work the logic of avoid repetitive calls.

Can you give us a logical description of what it does? Roughly how many lines does your function have? (excluding comments and blanks if you can)

Can you put some tracing in to see where it is spending most of it's time?



Robin
# 5  
Old 05-22-2014
15 MB shell script Smilie
How much lines is that if i may ask ?

It's time for a change.
Either reprogramming or starting from scratch, consider other tools for the job like databases Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find C programming functions using shell script?

I want to find c function definition with pattern with shell script by checking condition for each line: data_type functionname(param_list){ .... } I knew cscope or ctag is usable for this task, but if there any ways to do without using them. I am thinking of checking line condition... (3 Replies)
Discussion started by: cmdcmd
3 Replies

2. Shell Programming and Scripting

Shell script to apply functions to multiple columns dynamically

Hello, I have a requirement to apply hashing algorithm on flat file on one or more columns dynamically based on header sample input file ID|NAME|AGE|GENDER 10|ABC|30|M 20|DEF|20|F say if i want multiple columns based on the header example id,name or id,age or name,gender and hash and... (13 Replies)
Discussion started by: mkathi
13 Replies

3. Shell Programming and Scripting

Shell functions mystery

so i noticed that when a shell script has a function defined in it, running "sh -x" on that shell script from the command line doesnt show what the function is doing. i like this. is there anyway for anyone to get around that? to be able to see exactly what a function or functions are doing? (4 Replies)
Discussion started by: SkySmart
4 Replies

4. Shell Programming and Scripting

Shell functions usage

Hi all, I have a doubt.. If we create shell functions through a script itself, can we use the same functions in command line also.. for example: $ cat a.sh ##### Functions function system_info { } function show_uptime { } (4 Replies)
Discussion started by: raghu.iv85
4 Replies

5. Shell Programming and Scripting

using library functions in shell script

hey guys, i made up a library file called common.lib so as to reuse the same code without typing it again. here is the code. its pretty basic . ## This is the second function compare() { file1 = $1 file2 = $2 cmp $file1 $file2 if then echo "comparison is possible"... (1 Reply)
Discussion started by: Irishboy24
1 Replies

6. Shell Programming and Scripting

Creating functions in shell script

hi friends, I am working with shell commands and all these works properly. Then i created a small function which includes these commands. Now the problem arises. When the commands are run by calling this fuction.it shows error. Why i am not able to run the unix command inside a function.... (1 Reply)
Discussion started by: gjithin
1 Replies

7. Shell Programming and Scripting

Calling shell functions from another shell script

Hi, I have a query .. i have 2 scripts say 1.sh and 2.sh 1.sh contains many functions written using shell scripts. 2.sh is a script which needs to call the functions definded in 1.sh function calls are with arguments. Can some one tell me how to call the functions from 2.sh? Thanks in... (6 Replies)
Discussion started by: jisha
6 Replies

8. Shell Programming and Scripting

Functions in C-Shell

Hi All, I have a very long code called myfunction -> "if ..... else if .... else if ..end if " And i have several other codes which need to call the "myfunction" code. How can C-shell call a function "B]myfunction" ? Can any body give me an example ?? (1 Reply)
Discussion started by: Raynon
1 Replies

9. Shell Programming and Scripting

functions in c shell??

i've been told that c shell does not support functions/subroutines is that true? if not can somebody give me the basic syntax for creating a function. it would very much appreciated! thanks in advance (1 Reply)
Discussion started by: ballazrus
1 Replies

10. Shell Programming and Scripting

Shell script functions

Simple shell script : date test_fn() { echo "function within test shell script " } on the shell prompt I run > . test Then I invoke the function on the command line as below : test_fn() It echos the line function within test shell script and works as expected. ... (5 Replies)
Discussion started by: r_subrahmanian
5 Replies
Login or Register to Ask a Question