The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
global variable not being set robotball Shell Programming and Scripting 5 06-10-2008 06:20 AM
global variable in awk anhtt Shell Programming and Scripting 1 03-16-2008 03:55 PM
Global Variable in a script? skyineyes Shell Programming and Scripting 2 07-12-2007 06:55 AM
Global Variable in awk... ZINGARO Shell Programming and Scripting 1 07-04-2007 01:07 PM
Global variable becomes local odashe318 Shell Programming and Scripting 2 10-29-2004 09:18 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-12-2008
deepanshu deepanshu is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 3
Global variable

I have written a shell scritp in which i am using a variable which is declared before a while loop and i am updaitng the variable in while loop and want to use its updated value outside the loop. I am not able to do so, b'coz the scope of the variable is limited to the while loop only and when i am using the variable outside the loop its using the value which i initialized to the variable before the loop.

I think i have to use global variable, but i dont know how to make & use global variable.

Plz respond asap
  #2 (permalink)  
Old 06-12-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Quote:
Originally Posted by deepanshu View Post
I have written a shell scritp in which i am using a variable which is declared before a while loop and i am updaitng the variable in while loop and want to use its updated value outside the loop. I am not able to do so, b'coz the scope of the variable is limited to the while loop only and when i am using the variable outside the loop its using the value which i initialized to the variable before the loop.

I think i have to use global variable, but i dont know how to make & use global variable.

Plz respond asap
prefix the variable you want to use with export.
i think this should work. give a try. Also tell which shell you are using.

Thanks
namish

Last edited by namishtiwari; 06-12-2008 at 06:23 AM..
  #3 (permalink)  
Old 06-12-2008
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438
Please define type of script: awk, sed, bash, ksh.

In bash all variables are global unless you declare them local to a function for example.

Show us your code.
  #4 (permalink)  
Old 06-12-2008
deepanshu deepanshu is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 3
i have used export but this is not working, may be i am not using correctly
can u plz give me a example of that
;-)
  #5 (permalink)  
Old 06-12-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
Before anybody can really help you, you need to tell the forum what particular shell you are using.
  #6 (permalink)  
Old 06-13-2008
deepanshu deepanshu is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 3
i m using bash
and my code is like this

/* Code starts here */

counter=1
file=""
ls -1tr abc* | while read record
do
if [ counter -ne 0 ]
file=${record}
counter=0
fi
done

rfile=${file}

/* EOF */


now my rfile is empty it has no value, but if i echo file variable in while it is having some value, i want to use that value outside. Please suggest me a solution asap
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0