Visit Our UNIX and Linux User Community


How to dynamically name as function in shell?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to dynamically name as function in shell?
# 1  
Old 09-10-2013
How to dynamically name as function in shell?

Hi all,

Does anyone know if it possible to append a parameter to a function name?

Something like the following:

Code:
function tnsrec_${SERVICE_NAME} {
code..
}

Any ideas?

Last edited by Scrutinizer; 09-10-2013 at 01:32 PM.. Reason: Removed proportional font definition within code tags
# 2  
Old 09-10-2013
Are you trying to achieve something like this?
Code:
# cat test.sh 
function tnsrec_SERVICE1 {
echo "Test ok"
}[

SERVICE_NAME=SERVICE1

eval tnsrec_${SERVICE_NAME}

Code:
# ./test.sh 
Test ok

# 3  
Old 09-10-2013
hmm, thnx for feedback.

I am supplying a parameter to a script and need to use that parameter to name a function.

In your code, where is the close ']' bracket?

also, when do I place the eval ?

Thanks again.

jd
# 4  
Old 09-10-2013
You can't use eval to dynamically name a function in that way.

But why would you want to?

If you can pass any value into a script and thus have a function with any name, why not just chose any name for the function to begin with?

What you are trying to do doesn't really make any sense.

BTW, I think the [ may have been a typo.
# 5  
Old 09-10-2013
I need to dynamically name a fnction so a watchdog script can identify a process.

so I need to append a parameter to a function name.

If you have any idea on how to do this I would be grateful.
# 6  
Old 09-10-2013
Code:
$ cat myScript
F=${1:-default}

. <(cat <<!
function somefunction_$F {
  echo "\$0: Test OK"
}
!)

eval somefunction_$F

Code:
# ./myScript blah
somefunction_blah: Test OK

# ./myScript
somefunction_default: Test OK

This method might be a bit inflexible since you have to escape anything in the function that belongs to the function, otherwise the shell will evaluate it.

Another method, using a temporary functions file:

Code:
# cat myScript
F=${1:-default}
sed "s/:name:/$F/" funcs.tmp > funcs.sh

. ./funcs.sh
        
eval somefunction_$F

Code:
# cat funcs.tmp
function somefunction_:name: {
  echo "$0: Test OK"
}

Code:
# ./myScript blah
somefunction_blah: Test OK
        
# ./myScript 
somefunction_default: Test OK


Last edited by Scott; 09-10-2013 at 11:21 AM.. Reason: Method without using temporary functions file; - added it back in again!
# 7  
Old 09-10-2013
hmm. appending a parameter to a function name is not proving easy.

Ideally I just need 1 script rather than several.

I am getting a little confused.

i would call the script in the following way:
Code:
bash myscript SERVICE_NAME

In my script I have:

Code:
export SERVICE_NAME=$1

function tnsrec_${SERVICE_NAME} {

..lines of code

}


Last edited by jonnyd; 09-10-2013 at 01:05 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #688
Difficulty: Medium
The first computer to implement architectures enabling reverse Polish notation (RPN) was the Apple I computer.
True or False?

10 More Discussions You Might Find Interesting

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

2. Homework & Coursework Questions

How to Dynamically Pass Parameter to plsql Function & Capture its Output Value in a Shell Variable?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: #! /bin/ksh v="ORG_ID" ... (2 Replies)
Discussion started by: sujitdas2104
2 Replies

3. UNIX for Beginners Questions & Answers

Putting query result dynamically to one cell of table from shell

I have to send a data in mail table format.only one cell need to get dynamically from query. my code is like (echo '<table boarder="1"> echo '<tr><td>stock</td><td>/path</td><td>.........</td></tr>' echo '</table> )sendmail.. in ......... I am trying to get query result.By putting query... (2 Replies)
Discussion started by: meera_123
2 Replies

4. Shell Programming and Scripting

Shell script variable names created dynamically

Hi, I'm trying to use a config file to define frequencies for checking log files. If the config file contains a frequency it will be used else a default value. The format of the config file (and hence the environment variable) is FREQ_log_logname=value A test shell script as below:... (2 Replies)
Discussion started by: u671296
2 Replies

5. Shell Programming and Scripting

how to create the files dynamically in c shell script

how can i CREATE a txt file dynamically in c shell: for instance: #! /bin/csh for each i (*) cat>file$i.txt for each j do .... (1 Reply)
Discussion started by: jdsignature88
1 Replies

6. Shell Programming and Scripting

dynamically adding values in c-shell

I am needing to create a variable(changing) and assign it a value(changing) ... I am using C-Shell.. Example: foreach account in ($Accountlist) set account_connect = "$account/$account_pass" end I want to make set account_connect to store various values ? $account_connect did not... (3 Replies)
Discussion started by: shafi2all
3 Replies

7. Programming

C function call dynamically

Hi. I need help with a C/C++ question and i hope someone can help me or give me some ideas. Is possible to build a function C/C++ call dynamically? I.e, i have an array with pointer functions and these functions can have different number of parameters and types. These parameters and their... (14 Replies)
Discussion started by: pogdorica
14 Replies

8. Shell Programming and Scripting

Dynamically creating text files using shell script

Hi All, I want to create a shell script which dynamically create text files. i am using the following script $i=1 while do cat > test_$i.txt done but while running the script it was stopping(the cursor not going to next step, i have to enter ctrl+c to make it stop). it is creating only... (2 Replies)
Discussion started by: KiranKumarKarre
2 Replies

9. Shell Programming and Scripting

Shell script dynamically case in VAR

Hallo, I am working on a kdialog. This shall be able to load the required commands from a .conf file. First step runs good by loading the entries (selectabel entries) in a variable: MIRRORSELECT=$(kdialog --radiolist "Select your nearest mirror" $VAR1) The kdialog is accordingly correct... (2 Replies)
Discussion started by: ACTGADE
2 Replies

10. Shell Programming and Scripting

Managing dynamically multiple shell

I want to launch some shell scripts. I would have the possibility to change the number of shell scripts launched dynamically by modifying a variable, or a configuration file. For example, I start to launch 4 scripts at the same time, and after that, by modifying a variable, 6 scripts are... (0 Replies)
Discussion started by: gonzo38
0 Replies

Featured Tech Videos