Variable value dosent change after the loop changes it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable value dosent change after the loop changes it
# 1  
Old 04-27-2011
Variable value dosent change after the loop changes it

i am new to shell scripting. this is similar to the code which i was writing for some other work. i want the variable 'x' to have the value which it will finally have at the end of the loop ( the number of directories ). but the value of 'x' only changes inside the loop and it remains '0' out-side of the loop.
(please note that i dont want to count the number of directories. but i am doing something simillar to this.)

Code:
#!/usr/bin/env bash

echo Begin

x=0
find $PWD -type d | while read filename
do
     echo $x
      x=`echo "$x + 1" | bc`
done

echo x=$x

echo End

result I got was this
Code:
Begin
0
1
2
x=0
End

But this is the result i really needed
Code:
Begin
0
1
2
x=2
End

but i know that this works for a normal loop which is like this
Code:
#!/usr/bin/env bash

echo Begin

x=0
while [ $x -lt 3 ]
do
     echo $x
      x=`echo "$x + 1" | bc`
done

echo x=$x

echo End

sorry if i am being so stupid.. please help... i am learning shell scripting by my self.... i am a bit of a C programmer... so is this because the value is passed inside instead of the variable refference or memory location... is there any other way to do this...
(i need the while loop to perform some tasks for each path that 'find' command gives.)
thank you...
# 2  
Old 04-27-2011

All elements of a pipeline are executed in a subshell and cannot affect the calling shell.
Code:
#!/usr/bin/env bash

echo Begin

x=0
find "$PWD" -type d | {
 while read filename
 do
     echo $x
     x=$(( $x + 1 ))
 done

 echo x=$x
}

This User Gave Thanks to cfajohnson For This Post:
# 3  
Old 04-27-2011
Quote:
Originally Posted by cfajohnson

All elements of a pipeline are executed in a subshell and cannot affect the calling shell.
Code:
#!/usr/bin/env bash

echo Begin

x=0
find "$PWD" -type d | {
 while read filename
 do
     echo $x
     x=$(( $x + 1 ))
 done

 echo x=$x
}

oh thanks.... thank you very much.... Smilie
# 4  
Old 04-27-2011
This is the case for bash but not for ksh93. OP's example works in ksh93.
Quote:
All elements of a pipeline are executed in a subshell and cannot affect the calling shell.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

2. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

3. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 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

Change Variable Value from Multiple Scripts and Use these Variable

Hi to All, Please find below details. file_config.config export file1_status="SUCCESS" export file2_status="SUCCESS" file_one.sh I am calling another two shell script from these script. I need to pass individual two script status (If it's "FAILED") to file_main.sh. file_main.sh I... (2 Replies)
Discussion started by: div_Neev
2 Replies

6. UNIX Desktop Questions & Answers

add char o end of line if dosent exist

hey , i want to check if the char "#" exist at the end of every line of txt file and if it dosent then add it for example: the cat jumped on my mom # cars can run on water# i cant get a date blue yellow# will be: the cat went back home# cars can run on water# i cant get a... (2 Replies)
Discussion started by: boaz733
2 Replies

7. Shell Programming and Scripting

Variable name change in a loop

I need to do something like this:for I in var1 var2 var3 ; do $I = "Something calculated inside the loop" doneObviously it doesn't work...but is it possible doing that in other ways? Thanks in advance. (6 Replies)
Discussion started by: canduc17
6 Replies

8. UNIX for Dummies Questions & Answers

The loop that dosent work

I have a serious problem and Im sure im just doing something stupid ######Start Load while do /export/home/mldwh/rkem_refresh/int_load/scripts/rkem_refresh.sh sleep 10 while do sleep 5 done done ######Log and Runstats sleep... (12 Replies)
Discussion started by: jadionne
12 Replies

9. UNIX for Dummies Questions & Answers

crontab -e dosent work

Hello! my crontab -e dosent work. Im getting this error message: sh: VI: not found The crontab files was not changed. How do i make it work? I think you should use "export editor=vi" or something. But i cant seem to get the correct line! pleas help... (11 Replies)
Discussion started by: dozy
11 Replies

10. UNIX for Dummies Questions & Answers

traceroute dosent work

hi , folks i m back with again some silly question i think.!! i can do ftp , telnet from windows to unix machine , but not vice versa!!! , from unix box , i get connection refused , and ping says not alive. ,...my os . solaris 7 , ok i know ishouldnt load network by using traceroute , but itried... (2 Replies)
Discussion started by: definate
2 Replies
Login or Register to Ask a Question