set variable in while loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting set variable in while loop?
# 1  
Old 12-10-2008
set variable in while loop?

Dear All,

Can anyone advise why this script isn't run as expected?

===========================
status=0
cat /etc/passwd |
while read line; do
status=1
done

echo $status
===========================

it always return 0 , but not 1. why?
anything wrong?

Thanks.
# 2  
Old 12-11-2008
Hi,
I guess it's because the piping makes the while loop variables local, I've seen this before and it behaves the same on my bash, But if You try it the other way round it may work better, just made a change to make clear that it is changed and keep the value:

Code:
#!/bin/bash
status=0

while read line; do
true && ((status++))
done <  /etc/passwd 

echo $status

/Lakris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to set Global variable outside while loop

Below is my code: count=0 if ... ... else ... find * -prune -type d | sort -r -n | while read d; do count=1 if ; then echo "Count1:$count" ... ... break 2; fi ... done ... fi echo "Count2:$count" (9 Replies)
Discussion started by: mohtashims
9 Replies

2. 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

3. 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

4. Shell Programming and Scripting

How can I set up this loop?

I want to compare two files. I think using grep is the way to go but I do not know how to set this up. Let me show an example: File1 has 1000 items, File2 has 700 items I need to take the first item in File1 and search the entire contents of File2 to see if there is a match there. IF there is a... (3 Replies)
Discussion started by: castrojc
3 Replies

5. 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

6. Shell Programming and Scripting

use variable to set the range of a for loop

Hi; For sure there's an easy answer to this one that I am not finding.. I first set a variable, say b1a:] max=5 then I want to use max to set the range for a for loop like so (it should run for i in 1:5) b1a:] for i in {1..$max}; do echo $i; done {1..5} I would like the output... (2 Replies)
Discussion started by: jbr950
2 Replies

7. Shell Programming and Scripting

Set variables from file, then loop

Hello, I am very, very new to shell scripting, but what I'm attempting to do is read in a list of user ID's to create on a database system from a CSV flat file, and for each entry run the "create user" script. I've gotten pretty far but I'm having trouble with the looping mechanism.... Any... (8 Replies)
Discussion started by: jkarren
8 Replies

8. HP-UX

What is the use of command set -- and set - variable?

Hi, I am using hp unix i want to know the use of the following commands set -- set - variable thanks (4 Replies)
Discussion started by: gomathi
4 Replies

9. Shell Programming and Scripting

set variable with another variable? c shell

okay, this shouldn't be difficult but I can't figure it out. How can I set a variable with another variable. I have the following: foreach pe ($dir $sp) set tpe = `echo $pe | grep M` if ($tpe == M) then set ${$pe} = M <--- This doesn't work else endif end In this case what... (2 Replies)
Discussion started by: wxornot
2 Replies

10. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies
Login or Register to Ask a Question