how to obtain a variable between subroutines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to obtain a variable between subroutines
# 1  
Old 06-21-2012
how to obtain a variable between subroutines

Code:
#!/usr/bin/bash
sub1 () {

for ((i=0;i<10;i++))
do
  export  a=$i;
   echo "value of a is $a";
   sleep 1
done
}
sub1 &

sub2 () {

for ((j=0;j<10;j++))
do
   echo "value of a is $a";
   sleep 1
done
}
sub2

Output:
Code:
bash-3.00$ ./var_test.sh 
value of a is 
value of a is 0
value of a is 
value of a is 1
value of a is 
value of a is 2
value of a is 
value of a is 3
value of a is 
value of a is 4
value of a is

I want the value of $a to be reflected in sub2.
Any idea ?????

Last edited by Arun_Linux; 06-21-2012 at 09:11 AM.. Reason: Added output
# 2  
Old 06-21-2012
This is a scope problem - sub2 cannot see changes to a made in sub1.

What are you trying to do - not how you think it should be done?
# 3  
Old 06-21-2012
the requirement is to communicate between two processes. How does two (forked) process exchange variable values ?
Here, sub2 needs to know the value of "a" in sub1 in runtime.
# 4  
Old 06-21-2012
Ever heard of file descriptors ? Smilie
# 5  
Old 06-21-2012
Use a fifo.
# 6  
Old 06-21-2012
correct me if i'm wrong, "mkfifo" will create a file and in that we feed values and retrieve.
Is there any method to get the same working without generating files.
It will be more useful if you could give an example.
Thanks in advance...

---------- Post updated at 08:08 PM ---------- Previous update was at 08:00 PM ----------

This one meets my requirement, but dont want a file to be created.

Code:
#!/usr/bin/bash
rm -rf pipe_file
mkfifo pipe_file
sub1 () {

for ((i=0;i<10;i++))
do
   a=$i;
   echo $i > pipe_file
   echo "value of a is $a";
   sleep 1
done
}
sub1 &

sub2 () {

for ((j=0;j<10;j++))
do
  echo "value of a is `cat < pipe_file`";
   sleep 1
done
}
sub2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Subroutines; am I capturing them correctly?

HI Folks - Again, I'm very sorry for the amateur post. I'm reletively new to shell scripting so please bear with me. I have a script that execute another script which stops a particular set of services for my application. IF the execution is successful, I want to check for any hung ESSSVR... (4 Replies)
Discussion started by: SIMMS7400
4 Replies

2. Shell Programming and Scripting

Tcl and clear case to obtain a file name as variable

proc get_view_rel_str { } { set cc_view :] end]] puts $cc_view set a puts $a set a end]] puts $a set a puts $a set a puts $a set a puts $a } get_view_rel_str this is a script in tcl with clearcase view (1 Reply)
Discussion started by: Syed Imran
1 Replies

3. Shell Programming and Scripting

How can i use switches type arguments for subroutines in perl

i want to call subroutines in perl like: sub temp { ---- some code ----- } temp(-switchName, value1, --switchName2, value2) Like i know getoptions::Long is there for command line switches type arguments. So i want to know for subroutine type arguments. (1 Reply)
Discussion started by: Navrattan Bansa
1 Replies

4. Homework & Coursework Questions

Help with perl subroutines

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: This subroutine needs to check if there was a file name given on the command line. If so, return that. Otherwise... (1 Reply)
Discussion started by: yonkers062986
1 Replies

5. Shell Programming and Scripting

How to get the return code of subroutines executed as standalone as command line in Perl ?

How to do I get the return code of a subroutine in a perl module if invoke the subroutine as standalone, I have an module say TestExit.pm and in that i have a subroutine say myTest() which is returns 12, if i were to call the subroutine from command line like CASE:1 ( Without an explict... (2 Replies)
Discussion started by: ennstate
2 Replies

6. Shell Programming and Scripting

Finding Subroutines

I'm maintaining a variety of programs (mostly in Perl) I didn't build, and the perllib is huge on the server. When I run across a user-defined subroutine it takes me at least an hour to look through all the logical places, and sometimes I still can't find the definition. Is there an easy way to... (2 Replies)
Discussion started by: pdownes
2 Replies

7. HP-UX

How can I obtain Hpux?

Hi, How can I obtain Hpux? Thanks (4 Replies)
Discussion started by: arabidi
4 Replies

8. Shell Programming and Scripting

obtain a new variable out of "for statment"

first i have pre-define some variable, then input those into a for statement for some process, finally, i want to get a new value out of it. A=192.168.16.1 B=192.168.32.1 C=192.168.64.1 for i in A B C do echo $i | nawk -F. '{ if ($3 > 16 and < 32) ....something like that, then ( $i=... (7 Replies)
Discussion started by: 3Gmobile
7 Replies

9. UNIX for Dummies Questions & Answers

Passing Hash Tables to Subroutines

Hi: How do I pass a hash table down to a subroutine along with some other variables? For example, I have say a subroutine play_with_hash: sub play_with_hash { my( $var1, $var2, %my_hash ) = @_; #do stuff with %my_hash ........... } Then I want to call the subroutine... (1 Reply)
Discussion started by: mirzabhai
1 Replies

10. UNIX for Dummies Questions & Answers

shell scripting: calling subroutines

How do I define and call a subroutine in a ksh script (or any shell)? I'm writing a script that uses a lot of the same code over and over and I don't want to waste time/space. Thanks, Chuck (2 Replies)
Discussion started by: 98_1LE
2 Replies
Login or Register to Ask a Question