pass by reference in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pass by reference in shell
# 1  
Old 10-19-2003
pass by reference in shell

Hi,

I would like to write a function which takes one integer argument and adds 10 to it. But, I'd like to return the result in the data that was passed in. Is this possible?

function1()
{
1=$(($1+10))
}

my_number=1
function "$my_number"
echo $my_number

I'd like the echo statement to print 11 in this case. Is that the right syntax? I don't have a shell to test with right now... I appreciate your help. Thanks.
# 2  
Old 10-19-2003
We need to know precisely which shell you are using. This is moving towards variable scoping, which varies from shell to shell, even from one version of the shell to another.

There are two types of functions in ksh, posix functions and the other kind. Since I can't find an official name for them, I dub them "useful functions".

Posix functions are of the form:
func1() { some-commands ; }
Use them if you like screwy side effects.

Useful functions are of the form:
function func1 { some-commands ; }

You are using the posix syntax.

With useful functions, you can declare variables inside the function with the typeset command. These are local variables. Other variables are global and are visable after the function exits. With posix functions, there are no local variables.

One option is to set global variables inside the function. But I think that stinks. So my solution would be:

function func1
{
echo $(($1 +10))
return 0
}

my_num=1
my_num=$(func1 $my_num)

This is closer to the fortran concept of functions than it is to c functions.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

4. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

5. Shell Programming and Scripting

Pass file as one of argument in shell

Hi, Is there any way that we can pass one file as one of the argument in shell script ? (1 Reply)
Discussion started by: Selva_2507
1 Replies

6. Shell Programming and Scripting

Perl de-reference code reference variable

Guys, May i know how can we de reference the code reference variable.? my $a = sub{$a=shift;$b=shift;print "SUM:",($a+$b),"\n";}; print $a->(4,5); How can we print the whole function ? Please suggest me regarding this. Thanks for your time :) Cheers, Ranga :) (0 Replies)
Discussion started by: rangarasan
0 Replies

7. Shell Programming and Scripting

Shell Scripting Problem - Invalid Back Reference

Here is the question... Create a new script, sub2, taking three parameters... 1.) the string to be replaced 2.) the string with which to replace it 3.) the name of the file in which to make the substitution ...that treats the string to be replaced as plain text instead of as a regular... (1 Reply)
Discussion started by: johnhisenburg
1 Replies

8. Shell Programming and Scripting

How to pass an array from SHELL to C function

Hi, I have an output generated from a shell script like; 0x41,0xF2,0x59,0xDD,0x86,0xD3,0xEF,0x61,0xF2 How can I pass this value to the C function, as below; int main(int argc, char *argv) { unsigned char hellopdu={above value}; } Regards Elthox (1 Reply)
Discussion started by: elthox
1 Replies

9. UNIX for Advanced & Expert Users

Pass by reference in shell functions

Hi all, In windows script, passing arguments can be done in both "call by value" and "call by reference". Refer http://www.commandline.co.uk/lib/treeview/index.php Can we have call by reference in unix too? Thanks in adavance, Sonal. (5 Replies)
Discussion started by: sonaluphale
5 Replies
Login or Register to Ask a Question