Sponsored Content
Top Forums UNIX for Dummies Questions & Answers shell scripting: calling subroutines Post 1289 by mib on Thursday 22nd of February 2001 02:21:48 AM
Old 02-22-2001
in bash shell it is something like this....

function function-name {
shell commands...
}

or just

function-name () {
shell commands...
}

#!/bin/bash

test ()
{
echo Hey I am here.
echo Now exiting function.
}

# Note: function must precede call.

# Now, call the function.

test

exit 0
------------------------------
here is another one which process passing arguments...

#!/bin/bash

func2() {
if [ -z $1 ]
# Checks if any params.
then
echo "No parameters passed to function."
return 0
else
echo "Param #1 is $1."
fi

if [ $2 ]
then
echo "Parameter #2 is $2."
fi
}

func2
# Called with no params
echo

func2 first
# Called with one param
echo

func2 first second
# Called with two params
echo

exit 0
----------------------

another important thing used in functions is return statement, which optionally takes an integer argument, which is returned to the calling script as the "exit status" of the function, and this exit status is assigned to the variable $?


I think korn shell also using same method. I am not sure.




[Edited by mib on 02-22-2001 at 02:24 AM]
This User Gave Thanks to mib For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

2. Shell Programming and Scripting

Calling shell functions from another shell script

Hi, I have a query .. i have 2 scripts say 1.sh and 2.sh 1.sh contains many functions written using shell scripts. 2.sh is a script which needs to call the functions definded in 1.sh function calls are with arguments. Can some one tell me how to call the functions from 2.sh? Thanks in... (6 Replies)
Discussion started by: jisha
6 Replies

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

4. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

5. Shell Programming and Scripting

Finding Subroutines

I'm maintaining a variety of programs (mostly in Perl) I didn't build, and the perllib is huge on the server. When I run across a user-defined subroutine it takes me at least an hour to look through all the logical places, and sometimes I still can't find the definition. Is there an easy way to... (2 Replies)
Discussion started by: pdownes
2 Replies

6. Homework & Coursework Questions

Help with perl subroutines

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: This subroutine needs to check if there was a file name given on the command line. If so, return that. Otherwise... (1 Reply)
Discussion started by: yonkers062986
1 Replies

7. Shell Programming and Scripting

Scripting: Calling Another Function

Hi Guys, How to make a code/script which behaves like some kind of "front-end" then upon choosing from the options within it, it will call another script for that option. To make it clearer, let's say: a. I have a first script which will list all the names of Students. b. Then, once this... (1 Reply)
Discussion started by: rymnd_12345
1 Replies

8. Shell Programming and Scripting

how to obtain a variable between subroutines

#!/usr/bin/bash sub1 () { for ((i=0;i<10;i++)) do export a=$i; echo "value of a is $a"; sleep 1 done } sub1 & sub2 () { for ((j=0;j<10;j++)) do echo "value of a is $a"; sleep 1 done } (5 Replies)
Discussion started by: Arun_Linux
5 Replies

9. UNIX for Dummies Questions & Answers

Unix Shell Scripting( Calling from Unix to PLSQL)

Hello Experts, I have the following questions to be discussed here at this esteemed discussion forum. I have two Excel sheets which contain Unix Commands llike creating directory the structure/ftp/Copy/Zip etc to basically create an environment. I need help in understanding some of... (1 Reply)
Discussion started by: faizsaadq
1 Replies

10. Shell Programming and Scripting

Subroutines; am I capturing them correctly?

HI Folks - Again, I'm very sorry for the amateur post. I'm reletively new to shell scripting so please bear with me. I have a script that execute another script which stops a particular set of services for my application. IF the execution is successful, I want to check for any hung ESSSVR... (4 Replies)
Discussion started by: SIMMS7400
4 Replies
escape(1)							Mail Avenger 0.8.3							 escape(1)

NAME
escape - escape shell special characters in a string SYNOPSIS
escape string DESCRIPTION
escape prepends a "" character to all shell special characters in string, making it safe to compose a shell command with the result. EXAMPLES
The following is a contrived example showing how one can unintentionally end up executing the contents of a string: $ var='; echo gotcha!' $ eval echo hi $var hi gotcha! $ Using escape, one can avoid executing the contents of $var: $ eval echo hi `escape "$var"` hi ; echo gotcha! $ A less contrived example is passing arguments to Mail Avenger bodytest commands containing possibly unsafe environment variables. For example, you might write a hypothetical reject_bcc script to reject mail not explicitly addressed to the recipient: #!/bin/sh formail -x to -x cc -x resent-to -x resent-cc | fgrep "$1" > /dev/null && exit 0 echo "<$1>.. address does not accept blind carbon copies" exit 100 To invoke this script, passing it the recipient address as an argument, you would need to put the following in your Mail Avenger rcpt script: bodytest reject_bcc `escape "$RECIPIENT"` SEE ALSO
avenger(1), The Mail Avenger home page: <http://www.mailavenger.org/>. BUGS
escape is designed for the Bourne shell, which is what Mail Avenger scripts use. escape might or might not work with other shells. AUTHOR
David Mazieres Mail Avenger 0.8.3 2012-04-05 escape(1)
All times are GMT -4. The time now is 07:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy