Reading function calling statement from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading function calling statement from a file
# 1  
Old 03-12-2012
Reading function calling statement from a file

Hi,
I have a shell script 'sample.sh' which has some functions as below.
Code:
#fun1
fun1()
{
date
}
 
#fun2()
{
echo hi
}

I want to run these functions as background processes and also redirect the output to a file. My function calling statements are in a different file 'sample.cfg' as below.
Code:
fun1 > fun1.log &
fun2 > fun2.log &

I am running a while loop in the main script which will read the file having the function calling statements and execute them as below.
Code:
while read fn
do
   `$fn`
done<sample.cfg

But the above code doesn't work. I found that it first executes the commands inside the function and then tries to execute the output of the commands as well and result in error as below.
Code:
[ukuxcent03:mqbrk22s:/home/mqbrk22s:] sample.sh
sample.sh[20]: Mon:  not found.
sample.sh[20]: hi:  not found.

I also tried the below code.
Code:
while read fn
do
   result=`$fn`
   echo $result
done<sample.cfg

This also doesn't work. It considers redirectional operator '>' and '&' as arguments to the function.

Can anybody help on this? I am attaching the sample.sh and sample.cfg.

Moderator's Comments:
Mod Comment code tags please

Last edited by Franklin52; 03-12-2012 at 11:56 AM.. Reason: Fixed code tags
# 2  
Old 03-12-2012
Hi Rama,
Want to know what are the contents of sample.cfg.
In sample.sh it seems that you have commented fun2().Please remove#.

Try this:
Code:
while read line
do
  result=$line
  echo $result
done < sample.cfg

---------- Post updated at 08:22 PM ---------- Previous update was at 08:18 PM ----------

Sorry...Missed the attachment file

Last edited by Franklin52; 03-12-2012 at 11:57 AM.. Reason: Please use code tags for data and code samples, thank you
# 3  
Old 03-12-2012
Hi aashish, content of sample.cfg is as below.
fun1 > fun1.log &
fun2 > fun2.log &

I don't want to print the output of the function to the screen. Instead i want to rerdirect it to a file. If you see the function call statements above that is what i am dioing. But this doesn't work.

Is there any way to interpret the symbols '>', '&' in the function call statement which is read from the file sample.cfg as operator instead of arguments to the function?
# 4  
Old 03-12-2012
Quote:
while read fn
do
`$fn`
done<sample.cfg
Idea: Instead of these 4 lines, make sample.cfg executable and run it with a "dot space" command so that the lines execute within the same shell as the functions:
Code:
. ./sample.cfg

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

2. UNIX for Dummies Questions & Answers

Help with reading and calling a command in a script

The problem I am having now is calling and reading a command and The Main script reads the data file and passes the input to the two calculation scripts, and than output to a file. 1. The Main Script ----------------- input=inputfilepj3 output=outfilepj3 echo "*** Converting... (2 Replies)
Discussion started by: TheUnitedOSI
2 Replies

3. Shell Programming and Scripting

Perl script for Calling a function and writing all its contents to a file

I have a function which does awk proceessing sub mergeDescription { system (q@awk -F'~' ' NR == FNR { A = $1 B = $2 C = $0 next } { n = split ( C, V, "~" ) if... (3 Replies)
Discussion started by: crypto87
3 Replies

4. Shell Programming and Scripting

Calling a function in cpp file inside shell script

Hi I need to call a function written in a cpp file with arguments inside the shell script..Can anyone help me how to do this:( (1 Reply)
Discussion started by: rkrish
1 Replies

5. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

6. Shell Programming and Scripting

Calling function in awk statement.

Hi All, I have an awk statement and a function defined in a script. I am trying to call the function from inside awk statement, i.e. awk ' myFunk () ;' filename But when I define myFunk() before awk, then I receive this error: s2.sh: line 48: syntax error: unexpected end of file and... (5 Replies)
Discussion started by: morningSunshine
5 Replies

7. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

8. Shell Programming and Scripting

reading fixed length flat file and calling java code using shell scripting

I am new to shell scripting and I have to to the following I have a flat file with storename(lenth 20) , emailaddress(lenth 40), location(15). There is NO delimiters in that file. Like the following str00001.txt StoreName emailaddress location... (3 Replies)
Discussion started by: willywilly
3 Replies

9. UNIX for Dummies Questions & Answers

Calling on function from file??

This may sounds dumb, but can I call on a function from a file? For example, I have a function file full of functions like below (no shell designation): func { echo "blah blah blah 1" } func2 { echo "blah blah blah 2" } func3 { echo "blah blah blah 3" } Am I able to call on any one... (3 Replies)
Discussion started by: douknownam
3 Replies

10. Shell Programming and Scripting

Calling other file function

Hi, I am pretty new to unix. Lets say i have a program(run_program) that will call another file function(functiona, in same directory): hence, inside that run_program. i will just call "functiona xx xx" to refer and use that function. this run ok until i run this program from another folder.... (3 Replies)
Discussion started by: maldini
3 Replies
Login or Register to Ask a Question