Variables being worked on inside of loops


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Variables being worked on inside of loops
# 1  
Old 06-13-2005
Variables being worked on inside of loops

I have a while read loop that reads values inside of a file and then performs an expr operation on each.

Everything works fine, the way it's supposed to but, once the loop is finished, I can not access the variable that I used inside of the loop (but that variable was created outside of the loop).

In other words...

PHP Code:
#Init var
myVar=0

while read LINE
do
varInLoop=... #This carries an integer
myVar=`expr $myVAR + $varInLoop#Trying to increment the totals
done

echo "myVar:${myVar}
The output of that last echo is "myVar:0"
Is this something I'm not allowed to do? I changed the value of the variable I created outside of the loop, from within the loop, but it didn't change after the loop was done, outside.

I tested the loop, and yes, the loop works, the values are there.
Should I just redirect the output to a file and just use the file as my variable?

Last edited by yongho; 06-13-2005 at 11:33 AM..
# 2  
Old 06-13-2005
shell != perl
loose the $ sign: varInLoop=... #This carries an integer
Code:
#!/bin/ksh

myVar=0

while read LINE
do
  myVar=`expr $myVar + 1`
done < myFile.txt

echo "myVar->[${myVar}]"

# 3  
Old 06-13-2005
You'll also find that...

Code:
while read line; do
  foo="bar"
done < some.txt
echo $foo

will echo bar, whereas

Code:
echo "one line" | while read line; do
  foo="bar"
done
echo $foo

will *not* echo bar.... hence the variable cannot be "exported"....

the latter example's while loop is executed in a subshell.

This holds true for bash and pdksh, at least on the system I have access to at the moment....

Cheers
ZB
# 4  
Old 06-13-2005
ah.. ic

thanks to both..

The main fault was..

Instead of #!/bin/ksh
I put #!/bin/sh

Is there some kind of reference page somewhere that tells me the differences between sh and ksh, because I had no Idea that I couldn't do this "variable exporting" with sh.
# 5  
Old 06-13-2005
Just as a note, if you're using a "proper" (i.e. non-Public Domain) Korn Shell, then even piping to a while loop results in the variable being set as you'd like it to be set (even though the pipe should mean that the while is executed in a subshell so the variable *shouldn't really be set* - surely this therefore isn't POSIX compliant?!)....
Code:
$ what `which ksh`
/usr/bin/ksh:
        Version M-11/16/88i
        SunOS 5.8 Generic 110662-15 Jun 2004
$ echo "one line" | while read line; do       
>   foo=bar                                
> done
$ echo $foo
bar

Consistency, that's what we like to see Smilie

Cheers
ZB
# 6  
Old 06-13-2005
ah

that's good to know.
this forum is becoming my unix bible, i have book marks everywhere for reference.. this one's definitely being marked.

I'd like to check to see if a variable is empty
If it is empty, then I'd like to set it to zero.
But I'm getting the syntax wrong.

PHP Code:
#initialize var
myVar=0

if [ $myVar "" ]; then
    
echo 'myVar is empty, changing myVar'
    
myVar=0
fi

echo $myVar 
Should I, or can I, use if [ $myVar -lt 1 ]; ?

I get a get_clientkpi.sh[80]: test: argument expected for that if statement above.
# 7  
Old 06-13-2005
If you're using ksh or bash, use the
${varname:=default} construct, e.g.

Code:
$ myVar=20
$ myVar=${myVar:=0}
$ echo $myVar
20
$ unset myVar
$ echo $myVar

$ myVar=${myVar:=0}
$ echo $myVar
0

Cheers
ZB
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unset variables in shell when it running two different loops

I have a script to start/stop/restart the tomcat application. When we run the script first time i.e stop/start it set all env variables(DISTRIB_ID,NAME,TOMCAT_CFG,....etc),but when we restart the tomcat it is running in the same shell.....I need to set the variables when i restart the tomcat(in the... (1 Reply)
Discussion started by: praveen265
1 Replies

2. Shell Programming and Scripting

Creating loops inside a file and extracting and loading data

Help needed (1 Reply)
Discussion started by: Chand Shrestha
1 Replies

3. Shell Programming and Scripting

Two variables in output file name nested for loops

I am trying to use two nested for loops to process some files and then create a new file using both variables in the output file name. I have several files in this naming style: S1_L3_all_R1.fastq S1_L3_all_R2.fastq S1_L4_all_R1.fastq S1_L4_all_R2.fastq . . S1_L8_all_R1.fastq... (3 Replies)
Discussion started by: aminards
3 Replies

4. Shell Programming and Scripting

while loops and variables under bash

Hi, This is probably going to be very simple but i came across something i can't quite explain. Here is the situation: i have a list of files, which i'd like to process one by one (get the size, make some tests, whatever) and generate some statistics using different variables. Something... (5 Replies)
Discussion started by: m69w
5 Replies

5. Shell Programming and Scripting

Interpretation of $variables inside programs run from a script

Hi, I am running a shell script that executes a program. Inside this program, variables are also referenced using a dollar symbol, eg. $a, $thething, and the shell script is misinterpreting them as variables relevant to the shell script rather than relevant to the program run from inside the... (2 Replies)
Discussion started by: TheBigH
2 Replies

6. Shell Programming and Scripting

For loops with multiple variables

Hi script gurus. I have need to know how to use for loop with multiple variable. Basically lets take for example /etc/passwd file has following entries The above cat command will basically first greps the real users that have email addresses then converts ':' to '+' then using cut... (4 Replies)
Discussion started by: sparcguy
4 Replies

7. Shell Programming and Scripting

variables inside an ssh session

Hello all, I would like to declare and use variables inside an ssh session. I have the feeling that it's not possible. Here is the situtation simpified: #:/bin/sh test="salut" echo $test ssh hudson@10.41.21.99 <<EOF export testssh="salut" echo testssh=$testssh ... (4 Replies)
Discussion started by: Lotfus
4 Replies

8. Shell Programming and Scripting

how to assign a value to several variables inside awk call

Hi I am using 'awk" to get file size and date of the file: op - sundev3 $ ls -l /bb/bin/tsfiles.sel | awk '{print $5 $6 $7}' 1587May8 May be somobody knows the way to combine this command with variable assignmet inside the awk, so I would have variables SIZE, MONTH and DATE assigned after... (1 Reply)
Discussion started by: aoussenko
1 Replies

9. Shell Programming and Scripting

scripting headache... loops & variables

Surely there's an easier way to do this, lets see if anyone knows! I am new to scripting so go easy on me! I have the following script and at the moment it doesn't work and I believe the problem is that I am using a while loop within a while loop. When I run the script using sh -x I can see... (6 Replies)
Discussion started by: StevePace
6 Replies

10. Shell Programming and Scripting

passing variables to sed inside script

I am trying to pass a regular expression variable from a simple script to sed to remove entries from a text file e.g. a='aaaa bbbb cccc ...|...:' then executing sed from the script sed s'/"'$a"'//g <$FILE > $FILE"_"1 my output file is always the same as the input file !! any... (5 Replies)
Discussion started by: Daniel234
5 Replies
Login or Register to Ask a Question