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
# 8  
Old 09-12-2007
however it doesn't work as described in the quote..... the following code does not work (arguments passed via script1 are ignored):

Script1:
......
. doSomething parm1 parm2
..........

Script2 => doSomething:
if [ $# -lt 2 ]; then
echo "Usage: doSomehting parm1 parm2. Missing parameters. Aborting..."
exit 1
fi
$1 $2 # calling script $1 with parameter 2
........ #common error hanlding and more

Note1: If I wrote script2 as a function inside script1, it'd work fine- but I want script2 in a separate file so that many other scripts can use it without having the same code repeated in each of those scripts.
Note2: without operator "." it would work too, but I want script2 to be able to access /modify all the globals defined in script1 or any calling scripts.

Please help me again!!

Last edited by bluemoon1; 09-12-2007 at 01:24 AM..
# 9  
Old 09-12-2007
Do you have "#!/bin/sh" or "#!/bin/bash" at the top?

You might try using bash instead of sh or vice versa.

What OS are you using?
# 10  
Old 09-12-2007
Sorry I failed to get in & read/response a few times due to this forum server system being too busy.
OS- Sun Solaris & I'm required to write bourne shell script.
I have the line #!/bin/sh at the beginning of each of scripts.

Last edited by bluemoon1; 09-13-2007 at 01:33 AM..
# 11  
Old 09-12-2007
If you are on Solaris then you also have the korn shell.

If the shell you are using does not support parameters in a source statement (if the shell does not support local then that would not surprise me) then I suggest you use a well choosen environment variable to pass the arguments.
# 12  
Old 09-12-2007
Thank you for all the ideas- there are so many ways to solve a problem- if you know how;-) So ideas are always appreciated!
For this paticular script2 in conncern, I'm trying to do it differently- so that it doesn't have to update/access the variables in the calling scripts, and can be called directly (without sourcing) like this:
.....
doSomething parm1 parm2 parm3 .... #(passing some of the globals by adding more parameters)

We know this works;-)

Thank you again. I'm definitely learning from you:-)
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