Export a function to another function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Export a function to another function
# 1  
Old 07-19-2016
Export a function to another function

is there a way to export a function to another function?

i'm using
Code:
#!/usr/xpg4/bin/sh

in the first line of my script.

Code:
firstfunc () {
echo $1 is nice
}

secondfunc () {
firstfunc Nancy
}

when i run a similar scenario to the above, i get:

Code:
/usr/xpg4/bin/sh[4]: firstfunc:  not found

# 2  
Old 07-19-2016
Hello SkySmart,

I haven't tested this in Sun/Solaris box but I think you have to call the second function at last to execute it. So following you could try and let us know how it goes then.
Code:
 firstfunc () {
echo $1 is nice
}
 secondfunc () {
firstfunc Nancy
}
 secondfunc

NOTE: I have tested above in BASH and worked fine for me.

Thanks,
R. Singh
# 3  
Old 07-19-2016
i should elaborate. i'm runningg it a in a bit of an unusual way:

Code:
#!/bin/sh
1:
2:firstfunc () {
3:echo $1 is nice
4:}
5:
6:secondfunc () {
7:firstfunc Nancy
8:}
9: secondfunc

sed -n 6,9p $0 | sh

i know its very strange how i'm running it, but i have my reasons for do it this way.

somehow, it apperas when i run the sed command to self read the running script, the shell doesn't recognize the already defined function.

i'm really hoping to be able to get around this.
# 4  
Old 07-19-2016
Hello SkySmart,

I am sorry but it is still not clear what you want to do, if you want to create some functions and call them according to number then you could useswitch case. Now coming to your requirement in case you wanted to print lines from 6th to 9th then you should mention sed outside of the Input_file as follows.
Code:
cat Input_file
#!/bin/sh
1:
2:firstfunc () {
3:echo $1 is nice
4:}
5:
6:secondfunc () {
7:firstfunc Nancy
8:}
9: secondfunc
  
  
sed -n 6,9p Input_file

I have removed | shin command sed -n 6,9p Input_file as you wanted to simply print lines. As I have mentioned before in case you wanted to execute some commands which you are printing from sed and wanted to execute you could use | sh then.

Now in case you wanted to use sed within your script itself then you could do following with the use of EOF.
Code:
cat script.ksh
cat << EOF > Input_file
#!/bin/sh
1:
2:firstfunc () {
3:echo $1 is nice
4:}
5:
6:secondfunc () {
7:firstfunc Nancy
8:}
9: secondfunc
EOF
  
sed -n 6,9p Input_file

If above both solutions are not matching your requirement, request you to be more precise in your requirement and provide us all the inputs from your side, I hope this helps.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 07-19-2016
When I run :
Code:
sed -n 6,9p testscript

(assuming that the numbers in your script are not there in real life) I get:
Code:
<empty line>
secondfunc () {
firstfunc Nancy
}

So these are the four lines of your code that get piped into a new Bourne shell (sh), which is like a new script and in that script firstfunc() is not defined.



--
Also note:

In your second example you are not using:
#!/usr/xpg4/bin/sh but #!/bin/sh. And you are feeding the output into a new (Bourne, not Posix) shell. To use a posix shell you either have to export your path to /usr/xpg4/bin:$PATHor pipe your output to /usr/xpg4/bin/sh rather than sh

Last edited by Scrutinizer; 07-19-2016 at 03:55 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

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

5. Shell Programming and Scripting

How to export the function in ssh

Hi Gurus, I have a requirment to use a function created in a script in another server within the script. Below is the sample script for reference #/bin/bash logs_gen () { XXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXX } for i in x y z; do # x y z are the server... (7 Replies)
Discussion started by: raghu.iv85
7 Replies

6. Programming

How to step in one function after the function be executed in gdb?

In gdb, I can call one function with command "call", but how can I step in the function? I don't want to restart the program, but the function had been executed, gdb will execute next statement, and I don't know how to recall the function. (4 Replies)
Discussion started by: 915086731
4 Replies

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

8. Shell Programming and Scripting

korn shell - export function??

Hi, I have a question. I know that if I want to access any variables defined in a calling script (script1.ksh) in a called script (script2.ksh) I need to export that variable in the calling script to make it available in the called script. But what about the functions in the calling script? ... (1 Reply)
Discussion started by: dips_ag
1 Replies

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

10. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question