Pass by reference in shell functions


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Pass by reference in shell functions
# 1  
Old 04-24-2007
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.
# 2  
Old 04-24-2007
Maybe I will give a sample script

#!/bin/sh
check() {
if [ ! -d ${1} ]; then
echo "${1} is not a dir"; exit 1
fi
echo dir
}
home=/u4/k
check home

----------------------
1) if /u4/k does not exist then the desired output is -
home
home (/u4/k) is not a dir

The idea is to give in a message both the variable and its value. So this check
can be made generic. In windows, this can be achieved.

Thanks,
Sonal
# 3  
Old 04-25-2007
Code:
#!/bin/sh
check() {
if [ ! -d ${1} ]; then
    echo "${1} is not a dir"; exit 1
fi
echo "{$1} is a directory"
}
# HOME is normally always defined for a process

check $HOME

What are you trying to do? Determine if the HOME variable is set correctly?
# 4  
Old 04-25-2007
(home is just a example-name , it could be javahome, )

Yes, I am trying to validate javahome, anthome etc in script. Workaround would be pass parameter-name and parameter-value to the function. and accordingly write the function.

---------------------------------
#!/bin/sh
check() {
if [ ! -d ${1} ]; then
echo "${2} is not a dir"; exit 1
fi
echo dir
}
javahome=/u4/k
check $javahome javahome
------------------------------------------------
# 5  
Old 04-25-2007
Back tick operator?

Are you really asking for this?

a="home"
a=`check $a`
# 6  
Old 04-25-2007
I think he means this...
Code:
$ cat xy
#! /usr/bin/ksh

check() {
name=$1
eval value=\$$1
echo the variable $name contains the value $value
if [ ! -d ${value} ]; then
echo "$value (which was passed in $name) is not a dir"
fi
echo dir
}
home=/u4/k
check home
home2=/something/else
check home2
exit 0
$ ./xy
the variable home contains the value /u4/k
/u4/k (which was passed in home) is not a dir
dir
the variable home2 contains the value /something/else
/something/else (which was passed in home2) is not a dir
dir
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

Shell functions mystery

so i noticed that when a shell script has a function defined in it, running "sh -x" on that shell script from the command line doesnt show what the function is doing. i like this. is there anyway for anyone to get around that? to be able to see exactly what a function or functions are doing? (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Pass parameters to a function and running functions in parallel

Hi , I have a script which is using a text file as I/P. I want a code where it reads n lines from this file and pass the parameters to a function and now this script should run in such a way where a function can be called in parallel with different parameters. Please find below my script, it... (1 Reply)
Discussion started by: Ravindra Swan
1 Replies

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

5. Programming

[ C ] multidemensional array pass to functions

Please excuse my ineptitude for a bit as I've been spoiled for the past few months with only writing perl code instead of C. So ok, I've been thinking about some code to change the crc32 values that are held within central directory headers of zip files. Because I'm lazy I decided to just... (3 Replies)
Discussion started by: VRoemer
3 Replies

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

7. Shell Programming and Scripting

Functions in C-Shell

Hi All, I have a very long code called myfunction -> "if ..... else if .... else if ..end if " And i have several other codes which need to call the "myfunction" code. How can C-shell call a function "B]myfunction" ? Can any body give me an example ?? (1 Reply)
Discussion started by: Raynon
1 Replies

8. Shell Programming and Scripting

functions in c shell??

i've been told that c shell does not support functions/subroutines is that true? if not can somebody give me the basic syntax for creating a function. it would very much appreciated! thanks in advance (1 Reply)
Discussion started by: ballazrus
1 Replies

9. Shell Programming and Scripting

Shell script functions

Simple shell script : date test_fn() { echo "function within test shell script " } on the shell prompt I run > . test Then I invoke the function on the command line as below : test_fn() It echos the line function within test shell script and works as expected. ... (5 Replies)
Discussion started by: r_subrahmanian
5 Replies

10. Shell Programming and Scripting

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... (1 Reply)
Discussion started by: csejl
1 Replies
Login or Register to Ask a Question