Visit Our UNIX and Linux User Community


Does a variable lose its value outside the loop in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Does a variable lose its value outside the loop in shell script?
# 1  
Old 07-04-2013
Does a variable lose its value outside the loop in shell script?

hi,

when we assign a variable inside a for loop or while loop in a shell script, does it loses its value after comming out of the loop. i am facing this issue.

can anyone help me??
# 2  
Old 07-04-2013
loops create subshells when you pipe the output to while or for loops so you will loose the values..
use process substitution..
# 3  
Old 07-04-2013
Hello,

The variable should have the last value which was assigned to it in loop.
Here is an example for it.

Code:
 
$ cat loop_example.ksh
str='monday, tuesday, wednesday, thursday, friday, saturday, sunday'
str1=`echo $str | sed 's/\,//g'`
set -A array_str ${str}

for i in ${array_str[@]}
do
a=$i

done

echo $a
 
 

$ ksh loop_example.ksh
sunday
$



We have a script named loop_example.ksh in which variable a have many values but if you will print it's value outside from loop it will show the last one as Sunday in the above example.





Thanks,
R. Singh
# 4  
Old 07-04-2013
Quote:
Originally Posted by RavinderSingh13
Hello,

The variable should have the last value which was assigned to it in loop.
Here is an example for it.

Code:
 
$ cat loop_example.ksh
str='monday, tuesday, wednesday, thursday, friday, saturday, sunday'
str1=`echo $str | sed 's/\,//g'`
set -A array_str ${str}

for i in ${array_str[@]}
do
a=$i

done

echo $a
 
 

$ ksh loop_example.ksh
sunday
$



We have a script named loop_example.ksh in which variable a have many values but if you will print it's value outside from loop it will show the last one as Sunday in the above example.





Thanks,
R. Singh

ya obviously, it will have the latest value since u r overwriting the previous value of $a inside the for loop.

my question is

Code:
cat file_names
sample.txt

shell_script_B.sh

Code:
echo "In shell_script_B.sh"
FILENAME="original.txt"
echo "FILENAME = {$FILENAME}"
echo ""
echo "-------Inside the loop-------"
cat file_names | while read line
do
        FILENAME=$line       # reassigning the FILENAME
        echo "FILENAME = {$FILENAME}"
done
echo "-------End loop--------------"
echo ""
echo "FILENAME = {$FILENAME}"

Output

Code:
In shell_script_B.sh
FILENAME = {original.txt}

-------Inside the loop-------
FILENAME = {sample.txt}
-------End loop--------------

FILENAME = {original.txt}

check the output. Inside the Loop it prints the new value and outside the value it prints original file. what is the solution for this??
# 5  
Old 07-04-2013
Pleas read my reply to your thread.. since you are piping the out put to while it will open a subshell and variable value would be lost once its back to main shell

use
while read line ; do
blah blah
blah
done < filename
This User Gave Thanks to vidyadhar85 For This Post:
# 6  
Old 07-04-2013
Hello,

Could you please the following if this helps.


Code:
 
 
$ cat script_user_error.ksh
echo "In shell_script_B.sh"
FILENAME="original.txt"
echo "FILENAME = {$FILENAME}"
echo ""
echo "-------Inside the loop-------"
while read line
do
        FILENAME=$line       # reassigning the FILENAME
        echo "FILENAME = {$FILENAME}"
done < "file_names"

echo "-------End loop--------------"
echo ""
echo "FILENAME = {$FILENAME}"
 
 

$ ksh script_user_error.ksh
In shell_script_B.sh
FILENAME = {original.txt}
-------Inside the loop-------
FILENAME = {sample.txt}
-------End loop--------------
FILENAME = {sample.txt}
$


I have just made the changes in color. Is it meets your requirement.
Kindly let me know if I can help on same.



Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 07-04-2013
Quote:
Originally Posted by vidyadhar85
Pleas read my reply to your thread.. since you are piping the out put to while it will open a subshell and variable value would be lost once its back to main shell

use
while read line ; do
blah blah
blah
done < filename
i read your thread before also and i got the problem what you said. i just wanted a solution for that. so this code will do the same task what i want. thanks.

Previous Thread | Next Thread
Test Your Knowledge in Computers #422
Difficulty: Medium
C# has support for strongly-typed function pointers via the keyword delegate.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a Boolean variable which succeed and failed inside the if loop in shell script ?

I have if loop with multiple variable value check in if loop. How can i print only if loop satisfied variable and its value in shell script ? I dont want to check each variable in if loop. That makes my script larger. if ] then echo "Only satisfied variable with value" ... (3 Replies)
Discussion started by: prince1987
3 Replies

2. Shell Programming and Scripting

Shell script use two variable in for loop

I want to create a shell script to add a user and modify its comment field mentioned in a file. 1. File value:- username comment field value xyz123 xyztesting abc123 abctesting def123 deftesting 2. i am using below loop to create user... (2 Replies)
Discussion started by: Anil
2 Replies

3. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

4. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

5. Shell Programming and Scripting

Shell Script Problems, Lose formatting when copy pasting from formatted file.

Hello, I'm having trouble with formatting some text via the terminal. I can get it perfectly formatted, but when I try and copy paste the text from the output file it loses it's formatting. Very frustrating! Basically I have 7 files (data data2 data3 data4 data5 data6 data7) containing a... (13 Replies)
Discussion started by: facetoe
13 Replies

6. Shell Programming and Scripting

Setting a variable in a while loop (.ksh script)

Hello Everyone, I'm still trying to grasp many concepts in .ksh scripting, one of them being variables inside loops. My problem is the following: * I'm trying to set a variable inside a while read loop to reuse it outside of said loop. My lines are the following :... (13 Replies)
Discussion started by: jimmy75_13
13 Replies

7. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

8. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies

9. Shell Programming and Scripting

Shell script / Grep / Awk to variable and Loop

Hi, I have a text file with data in that I wish to extract, assign to a variable and process through a loop. Kind of the process that I am after: 1: Grep the text file for the values. Currently using: cat /root/test.txt | grep TESTING= | awk -F"=" '{ a = $2 } {print a}' | sort -u ... (0 Replies)
Discussion started by: Spoonless
0 Replies

10. Shell Programming and Scripting

Passing a variable to awk while in a shell for loop

I am a newbie to awk and c programming, however am not a unix newbie. However, I do need help with a kshell script I am writing. It is almost complete, the last step is killing me. Any help would be greatly appreciated. What I am trying to do is cat a text file that has usernames. Then, using... (2 Replies)
Discussion started by: synergy_texas
2 Replies

Featured Tech Videos