Functions across eval


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Functions across eval
# 1  
Old 03-12-2014
Question Functions across eval

Ok, I'm trying to understand something. I've done some searches, but couldn't find anything definitive.
Now, I have come across a few things around the web cautioning against "eval", so that's duly noted, and any advice to alternative methods is greatly appreciated Smilie

So, easiest thing is probably a simple example:
I have the following 3 scripts setup:

Code:
::::::::::::::
func.lib
::::::::::::::
## my functions

function f_echo {
   echo `date +%Y%m%d_%H%M%S`": $*"
}

::::::::::::::
main.ksh
::::::::::::::
#!/bin/ksh

v_RUN_JOB=$1

# setup functions
. ./func.lib

# prove it works
f_echo  hello world

eval ${v_RUN_JOB}

::::::::::::::
test.ksh
::::::::::::::
#!/bin/ksh

# does this work??
f_echo  quick brown fox

a quick chmod to make sure they are executable ...
Code:
chmod +x main.ksh
chmod +x test.ksh

Then I call main:

Code:
./main.ksh  ./test.ksh

Output is:

Code:
20140312_131301: hello world
./test.ksh[4]: f_echo:  not found

So, main can see the function, however, the called script cannot?
Can anyone suggest a way to do this?

In case it's not obvious, I am passing a script to another (wrapper) script (ie think framework).
So the "main" script will handle some common setup/logs/etc., then run the passed script.
This isn't designed for interactive users, it's for a backend batch framework.

Should I be using the "eval" in this manner? or is there another command/option to ensure the functions can be handled?
So far, variables have worked just fine, but I only hit this with the function declarations.

Thanks!

(I'm on Sun Unix 5.10, however, any (mostly?) generic method of doing this would be appreciated)
# 2  
Old 03-12-2014
Your first shell script is defining a function and after that it knows about that function. But other shell scripts do not. Each shell script must define its own functions.
This User Gave Thanks to Perderabo For This Post:
# 3  
Old 03-12-2014
Quote:
Originally Posted by Perderabo
Your first shell script is defining a function and after that it knows about that function. But other shell scripts do not. Each shell script must define its own functions.
I was wondering about that.

so need to add:

Code:
. ./func.lib

to each script?
I can live with that Smilie

I was just hoping there was a "better" way ...
# 4  
Old 03-12-2014
You could source the second script with the . command. Then the 2nd script runs in the first process. However the second script is really just some commands (like your .profile file is). It is not really a script.
# 5  
Old 03-12-2014
Quote:
Originally Posted by Perderabo
You could source the second script with the . command. Then the 2nd script runs in the first process. However the second script is really just some commands (like your .profile file is). It is not really a script.
Oh that's a thought - hadn't even thought about doing that.

However, as you say, "it's not really a script" ... so if that second script IS intended as a full script - that's not a good way of doing that, though, is it?
# 6  
Old 03-12-2014
I believe that a script should stand on its own two feet. It should not require helper script to preset an environment for it. If it needs functions, it should seek them out and install them. It should set the PATH variable if it needs anything special. And the same with any other environment variable. We get a a lot of threads about a script that works from the command line but fails as a cron job. That never happens with a first class script.
These 2 Users Gave Thanks to Perderabo For This Post:
# 7  
Old 03-12-2014
Quote:
Originally Posted by Perderabo
I believe that a script should stand on its own two feet. It should not require helper script to preset an environment for it. If it needs functions, it should seek them out and install them. It should set the PATH variable if it needs anything special. And the same with any other environment variable. We get a a lot of threads about a script that works from the command line but fails as a cron job. That never happens with a first class script.
Yeah, I understand. I've hit that in previous places I worked. So with this, that's kind of what I'm trying to avoid Smilie Trying to make sure everything's set up front.
Function list wasn't a must have, so I'll handle it more traditional.

Thanks for the advice!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Eval

thank you (35 Replies)
Discussion started by: ratnalein88
35 Replies

2. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

3. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

5. Shell Programming and Scripting

Help on eval please

Hello All, Since my variables are nested I use eval to populate the data. I have an ambiguity here when eval is used along with & say I have the below variable url="www.unix.com" , this come from function call as argument. I want to take this into another variable say... (6 Replies)
Discussion started by: sathyaonnuix
6 Replies

6. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

7. Shell Programming and Scripting

eval

hi all, Am trying to add some code to a ksh script and i dont understand how an eval function is used : _var=$1 _conceal=$2 eval _val=\$${_var} can someone shed some light on what the eval function in the above context means/does ?? thanks. (4 Replies)
Discussion started by: cesarNZ
4 Replies

8. Shell Programming and Scripting

eval help

I am trying to expand the variable $user in my alias command and tried several variations of eval but can't seem to get it to work. The end result should be either: oracle_user='sudo su - oracle ' or oracle_user='sudo su - oracle1 ' user=$(grep '^oracle:' /etc/passwd | cut... (5 Replies)
Discussion started by: BeefStu
5 Replies

9. Shell Programming and Scripting

EVal

Hi All, I'm running some encrypted data through a script I wrote. In order to do this, I'm using eval to resolve some of my variables. At the moment, when I use eval to resolve, it strips out some of my encrypted values, and totally drops some others. For example if I have the value ab1"3 it drops... (1 Reply)
Discussion started by: Khoomfire
1 Replies

10. Shell Programming and Scripting

using eval w/ echo

My script has the following for loop in it. foreach id (CLKM4 DGTM4 GNNM4) eval echo $id \$${id}sf \$${id}sfk end so "id" is a variable defined in the loop, and "idsf" and "idsfk" are variables defined in another part of the script, they return values of 0 or more. The problem i'm... (2 Replies)
Discussion started by: wxornot
2 Replies
Login or Register to Ask a Question