scope of the variable - Naga


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting scope of the variable - Naga
# 8  
Old 04-27-2009
nagnatar ,
I am not facing the problem when i execute it on my system.
If you are facing it then it be because of export .

But according to me export should not alter any results.
It justs exports the value to the child processes .
# 9  
Old 04-27-2009
nagnatar ,
I am not facing the problem when i execute it on my system.
If you are facing it then it be because of export .

But according to me export should not alter any results.
It justs exports the value to the child processes .
# 10  
Old 04-27-2009
try changing the shell use ksh/bash and then try ..

Code:
$ cat num
1
2
3
$ 

$ cat a.sh
i=0                         # Look i have not specified any shell here ..
while read j
do
    if [ ! -z "$j" ]        # CHECK IF LINE IS NOT ZERO ( EMPTY ) 
    then
        i=$(( $i + $j ))
    fi
done < num
echo "Total is : [$i]"
$

$ bash a.sh ; ksh a.sh
Total is : [6]
Total is : [6]
$

# 11  
Old 04-27-2009
Yes, Its working on ksh. but not on sh. any idea why its not working in sh?

Thanks!
# 12  
Old 04-27-2009
Pradeep and Zedex

Can you tell me which OS and which shell you are using?

I tested in linux, my script works well.

but found that my script does not give expected output in Sun OS 9 and 10 [sh]
however sun os gives correct output in ksh.. :-(

any idea? Thanks!
# 13  
Old 04-27-2009
for better understanding

# more add.sh
i=0
while read line
do
k=`expr $i + $line`
i=$k
done < num
echo "Final : ${i}"


# sh add.sh
Final : 0 -------------> Here is my question..why sh gives zero as output when bash, and ksh gives correct output

# ksh add.sh
Final : 20

# bash add.sh
Final : 20
# 14  
Old 04-27-2009
Hi.

As we have seen empirically in Solaris sh:
Quote:
If the input or the output of a while or until loop is
redirected, the commands in the loop are run in a sub-shell,
and variables set or changed there have no effect on the
parent process:

lastline=
while read line
do




SunOS 5.10 Last change: 2 May 2008 26






User Commands sh(1)



lastline=$line
done < /etc/passwd
echo "lastline=$lastline" # lastline is empty!

-- excerpt form man sh
I think most of the folks who answer questions here will focus on obtaining alternate solutions to problems, as opposed to digging to find why something does not work ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Variable Scope in Perl

I have to admit that i have not used Perl at all and this is a singular occasion where i have to patch an existing Perl script. I dearly hope i do not have to do it again for the next 15 years and therefore try to avoid having to learn the programming language in earnest. The OS is AIX 7.1, the... (2 Replies)
Discussion started by: bakunin
2 Replies

2. Shell Programming and Scripting

BASH: variable and function scope and subscripts

Hi, I'm a Delphi developer new to linux, new to this forums and new to BASH programming and got a new task in my work: maintaining an existing set of BASH scripts. First thing I want to do is making the code more reliable as in my opinion it's really bad written. So here's the quest: I'm... (6 Replies)
Discussion started by: rse
6 Replies

3. Shell Programming and Scripting

Maintain Scope of the variable in UNIX

Hi All Is there is any way to maintain the scope of the variable in unix Example x=1 j=1 while do .. .... .... while do .. .. x=x+1 done #inner most while loop ends here done #outer loop ends here (8 Replies)
Discussion started by: parthmittal2007
8 Replies

4. Shell Programming and Scripting

Variable scope in bash

Hello! Before you "bash" me with - Not another post of this kind Please read on and you will understand my problem... I am using the below to extract a sum of the diskIO on a Solaris server. #!/bin/sh PATH=/usr/bin:/usr/sbin:/sbin; export PATH TEMP1="/tmp/raw-sar-output.txt$$"... (3 Replies)
Discussion started by: haaru
3 Replies

5. Shell Programming and Scripting

Help with retaining variable scope

Hi, I use Korn Shell. Searched Forum and modified the way the file is input to the while loop, but still the variable does not seem to be retaining the final count. while read name do Tmp=`echo $name | awk '{print $9 }'` Count=`cat $Tmp | wc -l`... (6 Replies)
Discussion started by: justchill
6 Replies

6. UNIX for Dummies Questions & Answers

Bash loops and variable scope

Hi All, I've been researching this problem and I am pretty sure that the issue is related to the while loop and the piping. There are plenty of other threads about this issue that recommend removing the pipe and using redirection. However, I haven't been able to get it working using the ssh and... (1 Reply)
Discussion started by: 1skydive
1 Replies

7. Shell Programming and Scripting

[SOLVED] Scope of KSH Variable in Perl?

cat test.ksh #!/usr/bin/ksh VAR="Dear Friends \n How are you? \n Have a nice day \n" export VAR echo "Inside test.ksh"; ./test.pl cat test.pl #!/usr/bin/perl print "Inside test.pl \n"; print "$VAR"; Output: ./test.ksh Inside test.ksh Inside test.plWhat I want to achieve is, I... (1 Reply)
Discussion started by: dahlia84
1 Replies

8. Shell Programming and Scripting

variable scope

Hi, I want to know about the variable scope in shell script. How can we use the script argument inside the function? fn () { echo $1 ## I want this argument should be the main script argument and not the funtion argument. } also are there any local,global types in shell script? if... (3 Replies)
Discussion started by: shellwell
3 Replies

9. Shell Programming and Scripting

problem with shell variable's scope

Hi, I am stuck while developing a shell sub-routine which checks the log file for "success" or "failure". The subroutine reads the log file and checks for key word "success", if found it set the variable (found=1). It returns success or failure based on this variable. My problem is, I can... (2 Replies)
Discussion started by: cjjoy
2 Replies

10. Programming

C++ variable scope and mutexes

I've been wondering if I can make mutexes much easier to use in C++ with creative use of a locking class and variable scope, but I'm not sure if things happen in the order I want. Here's pseudocode for something that could use the class: int someclass::getvalue() { int retval; ... (0 Replies)
Discussion started by: Corona688
0 Replies
Login or Register to Ask a Question