how to access globals in a function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to access globals in a function
# 1  
Old 09-11-2007
how to access globals in a function

I know any globals can be directly modified in a function without passing/returning parameters- provided the function is written within the same file. However, when I wrote functions in a saparate file (so hopefully they can be re-used by multiple script programs), and then call them from the main, they no longer can access the globals.

e.g.
script updateGlobals:
#!/bin/sh
....
var1=100
var2=200
#end of updateGlobals

mainScript:
#!/bin/sh
#define globals
var1=1
var2=2
........
#calling the common script to update the globals
updateGlobals
echo $var1
echo $var2
# end of main script

It doesn't work (I get 1 & 2)- How can I get the variables updated through another script file?

Thanks for sharing your thoughts.
# 2  
Old 09-11-2007
Use the . operator to call the other script, that effectively works like "include".


Code:
. updateGlobals

# 3  
Old 09-11-2007
It's so nice to hear from you again...Thank you for all your helps!!:-)
# 4  
Old 09-11-2007
how to pass arguments to a script with operator "."

Hope this one is a bit more challenging than the previous one... how to pass parameters in the previously menioned script with operator "."

Assuming doStuff is a common script required 2 arguments, I tried both:

. doStuff parm1 parm2
. "doStuff parm1 parm2"

Nothing is working. What is the correct syntax?
Thanks
# 5  
Old 09-11-2007
How are you then addressing parm1 and parm2 in the called script?

Code:
. (source or dot operator)

SYNTAX
      . filename [arguments]

      source filename [arguments]Read and execute commands 
from the filename argument in the current shell context.

If filename does not contain a slash, the PATH variable is 
used to find filename. The current directory is searched if 
filename is not found in $PATH.

If any arguments are supplied, they become the positional
parameters when filename is executed. Otherwise the 
positional parameters are unchanged.

The return status is the exit status of the last command 
executed, or zero if no commands are executed. If filename 
is not found, or cannot be read, the return status is non-zero.

A period `.’ is a synonym for `source’

source is a bourne shell builtin and a POSIX `special’ builtin

Executing a script

[raja@AGRAJA~]$ source script_name
[raja@AGRAJA~]$ sh script_name
[raja@AGRAJA~]$ csh script_name
[raja@AGRAJA~]$ ./script_name
[raja@AGRAJA~]$ bash script_name

# 6  
Old 09-11-2007
Thanks for your prompt reply!!

I don't quite understand what "positional parameters" mean.

To be clear, DoStuff - a common wrapper script that can call a set of different scripts & do common error handing, etc- looks like this:

#!/bin/sh
if [ $# -lt 2 ]; then
echo "Usuage: DoStuff parm1 parm2. Missing parameters. Exiting..."
exit 1
else
$1 $2 # calling script $1 with parameter $2
..... # error handling etc
fi

MainScript:
...
. doStuff parm1 parm2 # how exactly the parameters should be passed here ??


If I wrote doStuff as a function within the MainScript then this would work:
doStuff parm1 parm2

However, I want doStuff to be in a saparate script so it can be shared by different script files- instead of being written in each of the files.

Many thanks!!

Last edited by bluemoon1; 09-11-2007 at 09:45 PM..
# 7  
Old 09-11-2007
Quote:
Originally Posted by bluemoon1
I don't quite understand what "positional parameters" mean.
$1 $2 $3 $4 ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sudo has no access to exported bash function

Hello. I am running leap 15.1 sudo behavior may differ from other distrib. I have a file with functions definition .... function_1 { echo "Hello world" } export -f function_1 This file is sourced by both /etc/bash.bashrc.local and profile.local. So the functions are... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Programming

How to access a C function from a shell script?

Hi, I'm new to shell programming. And I'm having issues with accessing a C file with shell. Given a C code which contains functions and Lets say each function contains a multiplication instruction. So I need to extract the size of the operands (a,b) of each multiplication instruction in bits. How... (3 Replies)
Discussion started by: beginner_99
3 Replies

3. Proxy Server

How to use Squid on Linux to control certain IP to access Web Server and certain IP cannot access?

Dear all experts here, :) I would like to install a proxy server on Linux server to perform solely to control the access of Web server. In this case, some of my vendor asked me to try Squid and I have installed it onto my Linux server. I would like know how can I set the configuration to... (1 Reply)
Discussion started by: kwliew999
1 Replies

4. Shell Programming and Scripting

Php server globals REQUEST_URI or HTTP_SERVER_VARS

Hi all, recently I found that $_SERVER does not deliver the complete url with query anymore on my server. http://www.example.org/search.php?stichwort=wiki echo $_SERVER; /search.phpHowever $GLOBALS works in this case. http://www.example.org/search.php?stichwort=wiki echo $GLOBALS;... (1 Reply)
Discussion started by: lowmaster
1 Replies

5. Solaris

samba read write access to owner and no access to other users

Hi All, I want to configure samba share permission so that only directory creator/owner has a read and write permission and other users should not have any read/write access to that folder.Will that be possible and how can this be achieved within samba configuration. Regards, Sahil (1 Reply)
Discussion started by: sahil_shine
1 Replies

6. Programming

How access a specific memory portion through printf() function????

Hi friends, Hope everyone is doing well. Please have a look at this simple program, you will figure out what I want. #include <stdio.h> int main() { printf("Enter an integer!\n"); scanf( "%d", 134511890 ); // Valid address on my computer printf( "%d\n", ???? ); return 0; } ... (3 Replies)
Discussion started by: gabam
3 Replies

7. Programming

How to access argv[x] from another function other than main???

Hi friends, when I am passing arguments to main, I want another function to be able to have access to that function, the problem is that I am creating athread, which has a function like void *xyz(void *), how can pass the refernce of argv to this function, if you see my program, you will better... (2 Replies)
Discussion started by: gabam
2 Replies

8. UNIX for Dummies Questions & Answers

kernel giving access for multiple users to access files

hi all, i want to know y kernel is giving access for multiple users to access a file when one user may be the owner is executing that file. Because other user can manipulate that file when the other user is executing that file, it will give the unexpected result to owner . plz help me... (1 Reply)
Discussion started by: jimmyuk
1 Replies

9. Shell Programming and Scripting

perl: globals and a package.

I am still learning perl and confused on this script I am revising at work. The original author uses a package, which I have left in the code; however, I cannot seem to access the global variable $dir. Code snippet: I have also tried using $RRD_MG::dir to no avail. Thank you. (6 Replies)
Discussion started by: effigy
6 Replies

10. UNIX for Dummies Questions & Answers

Need help to access/mount so to access folder/files on a Remote System using Linux OS

Hi I need to access files from a specific folder of a Linux system from an another Linux System Remotely. I know how to, Export a folder on One SCO System & can access the same by using Import via., NFS in the Sco Unix SVR4 System using the scoadmin utility. Also, I know to use mount -t ... (2 Replies)
Discussion started by: S.Vishwanath
2 Replies
Login or Register to Ask a Question