a weird issue with "while" block


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting a weird issue with "while" block
# 1  
Old 08-06-2007
Question a weird issue with "while" block

I tried to read some lines from a file and append them to a variable via "while" block like below:
###############
#!/bin/sh

test="ABC_"

while read L1
do
test=$test$L1
echo $test
done < some.txt

echo $test
###############

But the last line keeps print the initial value, "ABC_", even i can see that the $test is changed in the while block. And if i remove "< some.txt" and put the "read" command inside the block, the updated $test will be printed on the last line.

Could you please explain why such thing happens and how can i fix it if i still want to keep "< some.txt" at where it is...

Thanks in advance
# 2  
Old 08-06-2007
works correct for me

I tried it on my box -
Quote:
AIX 5.3
and I get the expected result. Which box are you in?
Code:
rkumar@bdc4reteaix1w: /home/rkumar/unix_forum >cat some.text
junk
values
rkumar@bdc4reteaix1w: /home/rkumar/unix_forum >cat chk_some_text
#!/usr/bin/sh
test=ABC_
while read L1
do
test=$test$L1
echo $test
done <some.text

echo $test
rkumar@bdc4reteaix1w: /home/rkumar/unix_forum >./chk_some_text
ABC_junk
ABC_junkvalues
ABC_junkvalues

# 3  
Old 08-06-2007
This behaviour is due to the fact that "read" command is executed in a separate process compared to the main sh process, so you lose all the varible assignments inside the while loop. I think that the only method for this to work while keeping the "< some.txt" is to save the $test value in a tempfile and restore it outside the loop:

Code:
###############
#!/bin/sh

test="ABC_"
tmp="/tmp/test.tmp"

while read L1
do
   test=$test$L1
   echo $test | tee $tmp
done < some.txt

test=`cat $tmp`
echo $test
###############

# 4  
Old 08-06-2007
Quote:
Originally Posted by robotronic
This behaviour is due to the fact that "read" command is executed in a separate process compared to the main sh process,
Almost correct. The problem is that the old Bourne shell spawns a separate process to handle a "while" loop. When the loop finishes, the parent shell will not see any changes made inside the loop. The Bourne shell behaves this way for any loop. Using or avoiding "read" has no effect on this. Most modern OS's that still supply the Bourne shell also supply ksh. Switching to ksh would solve the problem. Or switching to bash.

Note that on Linux, sh is a link to bash, not the Bourne shell. No Bourne shell is available on Linux so you can't reproduce the problem there. Not sure about AIX, but if this worked, sh could not have been the Bourne shell. Stuff like this is why we wish that folks would mention the particular OS.
# 5  
Old 08-06-2007
Perderabo, thank you for the clarification. My example is in fact tested on Solaris 5.8 with the old Bourne shell, so I obtain the same behaviour as sleepy_11, which evidently is using Bourne shell too Smilie
# 6  
Old 08-06-2007
Thanks for all your reply. Below are my comments:

1. i tested on the SunOS 5.9
2. I tried below script to test the "loop in new process", as you mentioned:

###############
#!/bin/sh

test="ABC_"
echo $$ #new line, print the process id outside the loop
while read L1
do
echo $$ #new line, print the process id inside the loop
test=$test$L1
echo $test
done < some.txt

echo $test
###############

But i got the same process id printed.......Is my test method wrong, or SUN does some update on sh?
# 7  
Old 08-07-2007
Well, your test method is wrong. sh sets $$ when it starts up and it does not get updated when it forks. You could put the command
ps -f
inside and outside the loop to see the extra process. But please switch to a more modern shell. These problems were solved with the release of ksh in 1988.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Weird behavior of command "local"

Hi there, I'm running into a very weird situation. Let's forget about the purpose of my initial script please. I noticed the bug whatever I'm trying to do. I'm on an old server running bash 3.1.17. Say we have the following script : foo:~# cat /tmp/test #!/bin/bash f1() { local... (9 Replies)
Discussion started by: chebarbudo
9 Replies

3. Shell Programming and Scripting

Commenting out "expr" creates weird behavior

This really puzzles me. The following code gives me the error 'expr: syntax error' when I try to do multi-line comment using here document <<EOF echo "Sum is: `expr $1 + $2`" EOF Even if I explicitly comment out the line containing the expr using "#", the error message would still exist... (3 Replies)
Discussion started by: royalibrahim
3 Replies

4. Shell Programming and Scripting

""Help Me!""Beginner awk learning issue

Hi All, I have just now started learning awk from the source - Awk - A Tutorial and Introduction - by Bruce Barnett and the bad part is that I am stuck on the very first example for running the awk script. The script is as - #!/bin/sh # Linux users have to change $8 to $9 awk ' BEGIN ... (6 Replies)
Discussion started by: csrohit
6 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

find: "weird" regex behaviour

I have these two files in current dir: oos.txt oos_(copy).txt I execute this find command:find . -regex './oos*.txt'And this outputs only the first file (oos.txt)! :confused: Only if I add another asterisk to the find find . -regex './oos*.*txt' do I also get the second file... (7 Replies)
Discussion started by: courteous
7 Replies

7. UNIX for Dummies Questions & Answers

weird password popup on "admin" mentions

I had a site that sold stock photography and some guys I know set up a shopping cart and gallery system for it. One thing they did was give me an admin page (mysite.net/admin) where I could upload new images and change prices and all that. For that admin page they set a password popup prompt. I... (4 Replies)
Discussion started by: lex0062
4 Replies

8. OS X (Apple)

Weird "security" bahavior with SSL certificates

Hello, I have been attempting to automate the addition of SSL certificates to keychains on a MAC using the "security" command. I've noticed two things, 1 of which I don't understand. 1. If I use something like "security add-trusted-cert -d -k /System/Library/Keychains/SystemRootCertificates... (1 Reply)
Discussion started by: prafulnama
1 Replies

9. Shell Programming and Scripting

Weird problem with output from "date '+3600*%H+60*%M+%S' "

Hi, I came across a script a few months ago that allowed you to use the following script to include the current time into your prompt (useful from auditting purposes): # Set Prompt typeset -RZ2 _x1 _x2 _x3 let SECONDS=$(date '+3600*%H+60*%M+%S')... (5 Replies)
Discussion started by: m223464
5 Replies
Login or Register to Ask a Question