How to call a function in Shell..?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to call a function in Shell..?
# 1  
Old 08-23-2011
How to call a function in Shell..?

#!/bin/bash

Code:
FUN_ECHO(){
echo $1
}

FUN_ECHO "hi how are you ?"

This code will work fine.


BUT is it possible to make the following to work ?
Code:
FUN_ECHO "hi how are you ?" 

FUN_ECHO(){
echo $1
}

I know that the code will be executed line by line. But i have a number of functions.. which may call another function and may return to some other... and so and so...

is there any way to call as in Java...?? [ i mean from anywhere in the same page/script]
# 2  
Old 08-23-2011
No: Functions
This User Gave Thanks to yazu For This Post:
# 3  
Old 08-24-2011
Quote:
Originally Posted by linuxadmin
I know that the code will be executed line by line. But i have a number of functions.. which may call another function and may return to some other... and so and so...

Define all your functions at the beginning of the script. Then they may be called from wherever you like, in any order, including from another function.
Code:
a1()
{
 echo "$FUNCNAME"
 a3
}

a2()
{
 echo "$FUNCNAME"
}

a3()
{
 echo "$FUNCNAME"
 a2
}

a1

This User Gave Thanks to cfajohnson For This Post:
# 4  
Old 08-24-2011
Works fine

Quote:
Originally Posted by cfajohnson

Define all your functions at the beginning of the script. Then they may be called from wherever you like, in any order, including from another function.
Thank you cfajohnson..

i tried this method.
Its works fine.

thanks again..
# 5  
Old 08-24-2011
You could also collect your functions in a separate file, and use them in other scripts as well. Include them in your script like this:
Code:
. func_library.sh

The dot command runs the func_library.sh script in the same shell, thus making the functions available to it. Less clutter and one place to maintain commonly used functions.
This User Gave Thanks to gary_w For This Post:
# 6  
Old 08-24-2011
Oh yes. I found it somewhere while Googling.

But in my case , i want to use this in Only in one file.

The thing is i am creating an SVN patch maker. And i want the entire patch maker as only one shell file.

Its done.

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Strange function call in the shell script parse_prog_args $@

I am converting shell script to Perl. In shell I have a code parse_prog_args() { if then while do case $1 in -P* | -p* ) export PROCESS_DATE=$2 export MM=`echo $2 | cut -c5-6` export DD=`echo $2 | cut -c7-8` export YY=`echo $2 | cut -c3-4` export... (4 Replies)
Discussion started by: digioleg54
4 Replies

2. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

3. Shell Programming and Scripting

How to call Oracle function with multiple arguments from shell script?

Dear All, I want to know how can i call oracle function from shell script code . My oracle function have around 5 input parameters and one return value. for name in *.csv; do echo "connecting to DB and start processing '$name' file at " echo "csv file name=$x" sqlplus -s scoot/tiger <!... (2 Replies)
Discussion started by: Balraj
2 Replies

4. Shell Programming and Scripting

Segmentation fault in function call, shell script

I am getting Segmentation fault at below function call in my script: get_x() { sqlplus -s / <<end | grep KEEP | sed 's/KEEP//;s///g' select 'KEEP' ,table_name from all_synonyms where upper(synonym_name)= '$1'; exit end x=$(get_x $1) echo " SQL OUTPUT IS :: $x" } I am getting output of... (1 Reply)
Discussion started by: IB_88
1 Replies

5. Shell Programming and Scripting

Shell Scripting Function call return value

Hi I have a function : Make_Report() { trx_report=`sqlplus -s $conn_str << @@ set echo off; set pages 0; set feedback off; set verify off; select srv_trx_s_no,... (1 Reply)
Discussion started by: neeraj617
1 Replies

6. Shell Programming and Scripting

Call Shell Function from mysql timestamp

Hi all, Actually my aim is to call the shell script when ever there is a hit in a mysql table which consist of 3 values. Acter some research I came to know that it is not possible and can achive with timestamp. Can someone please tell me how to read the table timestamp which should done... (3 Replies)
Discussion started by: santhoshvkumar
3 Replies

7. Red Hat

how to call a particular function from one shell another shell script

please help me in this script shell script :1 *********** >cat file1.sh #!/bin/bash echo "this is first file" function var() { a=10 b=11 } function var_1() { c=12 d=13 (2 Replies)
Discussion started by: ponmuthu
2 Replies

8. Shell Programming and Scripting

Shell Script to call another function

Here is the following code : 1. # gcc -c test firstprog.c the above command will generate a executable file called "test " in which ever directory it is run. Assuming It will also return a value. 2. In the below SCRIPT . test is a file generated by compiling a c program... (3 Replies)
Discussion started by: Vabiosis
3 Replies

9. Shell Programming and Scripting

how can i call a function in shell script

i have a function written in one shell script and i want to call that function in another shell script and use the value returned by that script. can any one suggest me how can i do that? regards, Rajesh.P (4 Replies)
Discussion started by: rajesh.P
4 Replies

10. Shell Programming and Scripting

i want to call a oracle function in my shell script

i want to call a oracle function in my shell script (4 Replies)
Discussion started by: dineshr85
4 Replies
Login or Register to Ask a Question