$0 scope in function and calling script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting $0 scope in function and calling script
# 1  
Old 01-05-2004
$0 scope in function and calling script

Hi folks,

I'm just running through an oreilly korn shell book but have witnessed an output difference on my own unix machine and the output provided in the book. Can anyone help?

create a script called ascript as follows:

function afunc {
print in function $0: $1 $2
var1="in function"
}
var1="outside of function"
print var1: $var1
print $0: $1 $2
afunc funcarg1 funcarg2
print var1: $var1
print $0: $1 $2


The expected output is also as follows:

var1: outside of function
ascript: arg1 arg2
in function afunc: funcarg1 funcarg2
var1: in function
ascript: arg1 arg2


However on my machine i get the following:

var1: outside of function
ascript: arg1 arg2
in function ascript: funcarg1 funcarg2
var1: in function
ascript: arg1 arg2

This is strange as the function doesn't seem to recognize its own name as its own $0 argument.

Thanks in advance
# 2  
Old 01-05-2004
$0 represents the name of the current shell (or script). Functions execute in the original shell, so $0 would still represent the script name.

The O'Reilly book is wrong! (although it is a great book)
# 3  
Old 01-05-2004
The O'Reilly book is correct. beckett, what system are you using? What version of the korn shell?

From The New Kornshell Command and Programming Language by Morris Bolsky and David Korn (page 76)
Quote:
The value of positional parameter 0 is set to the name of the function, only when the function has been defined with the function compound command. When a POSIX function is invoked, $0 is not changed.
# 4  
Old 01-05-2004
Apologies, the 'function funcName' syntax should set $0 to the name of the function.

The POSIX standard 'funcName () ' syntax would behave as I indicated earlier.

If you used the ksh variation of the syntax as your example would indicate, and you are seeing the POSIX behaviour, it is possible that you have an option set which you should turn off.
Try 'set +o posix'.
# 5  
Old 01-05-2004
thanks for the quick replies,

when i login to the network through my local terminal i see SunOS 5.9 generic feb 2000

when i run a man on ksh it says KSH(1)
Thats all i can see with regard to versions

There a few other things that dont work as the oreilly book suggest e.g. even though the IFS variable is defined as a comma (,) when i use the $* argument it still separates parameters with a space. I have a feeling i'm going to run into more variances like this ahead, any advice??

Cheers
# 6  
Old 01-05-2004
Beckett, post the results of uname -a and echo $SHELL.
# 7  
Old 01-05-2004
Ok

uname -a

SunOS mlgw 5.8 Generic_108528-08 sun4u sparc SUNW,Ultra-5_10

$SHELL

/bin/ksh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling Oracle function from script

Hi I need to call a function in database and update the return value of that function with a value in csv file. test.csv 1,2,3,,5,,,8,9,10 1,2,3,4,5,,,8,9,10 1,2,3,,,,,8,9,10In the above file I want to replace column 2 with a value extracted from database like (select student_id from... (3 Replies)
Discussion started by: kev94
3 Replies

2. Shell Programming and Scripting

Calling function from another bash script

I would like to call functions from another bash script. How can I do it? Some code More code (11 Replies)
Discussion started by: kristinu
11 Replies

3. Shell Programming and Scripting

Calling a function which uses expect from a shells script

Hi all, This is the first time i am using expect. I am trying to call a function with in the shell script. The function will shh to a new server and will pass the password using expect and send. I need help in calling the fuction i am getting follaowing errors... here the script ... (8 Replies)
Discussion started by: firestar
8 Replies

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

5. Shell Programming and Scripting

c function calling from a pearl script

Hi, Is it possible to call a c function defined in a .c file from a pearl script? How is this possible? Thannks in advance, JS (2 Replies)
Discussion started by: jisha
2 Replies

6. Shell Programming and Scripting

Calling a C-function froma shell script

Hi, I have searched the forum for the query, But i didnt find an exact answer. I have a script(1.sh) and a c program(sample.c) sample.c contains many function definitions.( run(), find(), add() etc). I want to call functions in sample.c from 1.sh and use the return value in 1.sh... (3 Replies)
Discussion started by: jisha
3 Replies

7. UNIX for Dummies Questions & Answers

calling one function from another shell script

i have a function defined in one ksh (ksh 1) i want to use that function in another ksh (ksh 2) i am using . $<directoryname>/<ksh name> i am calling the function defined in ksh 1 in ksh 2 i want the returnstatus from the above operation but it is not executing the function what i... (1 Reply)
Discussion started by: trichyselva
1 Replies

8. Shell Programming and Scripting

Help needed in function calling in a script

:confused:Hi , I have a script as shown below: rpttxt() { name="$*" awk '/'"${name}"'/ {print $2 $3"=" $4}' file.txt } xx = rpttxt "COL_HEAD_1" awk 'BEGIN {printf("%36s \n ","'"$xx"'")}' rpttxt() is a function..I want to store the final result of this function in... (3 Replies)
Discussion started by: jisha
3 Replies

9. Shell Programming and Scripting

urgent calling function from a script

Hi, Can we define a function say func1 in a script 1.sh and call this function from another script say 2.sh by passing an argument :confused: (2 Replies)
Discussion started by: jisha
2 Replies

10. Programming

calling c++ function from script

hi, I just started to work on unix, I was wondering if it is possible to call a c++ function from a script. now, i don't mean starting a program, i mean dynamicaly calling a funtion (like working with a dll) thanks (3 Replies)
Discussion started by: Lebamb
3 Replies
Login or Register to Ask a Question