Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
08-19-2008, 06:15 PM
|
#1
|
LQ Newbie
Registered: Aug 2005
Posts: 26
Rep:
|
Variable scope in KSH v5.2.14 99/07/13.2
So, I've a situation like the following and I'm looking for a work-around to get expected output.
The real-world version of this script does necessitate the use of "while read..." instead of for loops.
sample code:
Code:
#!/bin/ksh
myvar="hello there, world "
echo -e "1\n2\n3\n4" | while read "f1"
do
echo -e "1\n2\n3\n4" | while read "f2"
do
echo -e "1\n2\n3\n4" | while read "f3"
do
myvar="$myvar $f1"
echo $myvar
done
echo $myvar
exit 0
echo "I do not print"
done
echo "I still print"
done
Actual output:
Code:
hello there, world 1
hello there, world 1 1
hello there, world 1 1 1
hello there, world 1 1 1 1
hello there, world
I still print
hello there, world 2
hello there, world 2 2
hello there, world 2 2 2
hello there, world 2 2 2 2
hello there, world
I still print
hello there, world 3
hello there, world 3 3
hello there, world 3 3 3
hello there, world 3 3 3 3
hello there, world
I still print
hello there, world 4
hello there, world 4 4
hello there, world 4 4 4
hello there, world 4 4 4 4
hello there, world
I still print
EXPECTED output (or "The output under KSH 'Version M 1993-12-28 s+'"):
Code:
hello there, world 1
hello there, world 1 1
hello there, world 1 1 1
hello there, world 1 1 1 1
hello there, world 1 1 1 1
|
|
|
08-19-2008, 06:51 PM
|
#2
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
Perhaps this explains:
Quote:
Note: Some shells (but not this one) execute control structure commands
in a subshell when one or more of their file descriptors are redi-
rected, so any environment changes inside them may fail. To be porta-
ble, the exec statement should be used instead to redirect file
descriptors before the control structure.
|
|
|
|
08-19-2008, 08:04 PM
|
#3
|
LQ Newbie
Registered: Aug 2005
Posts: 26
Original Poster
Rep:
|
Ahhha. Souds likely. When I'm back at work I'll try leading with a dot+space and see if I can't convince it to run the loop within the current ksh instance.
What was your source? In the man page someplace?
Last edited by UltramaticOrange; 08-19-2008 at 08:07 PM.
|
|
|
08-19-2008, 08:53 PM
|
#4
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
My ksh man page.
That while and other redirected control structures (sometimes) run in a sub-shell is an old sh implementation artifact from years gone by. Later shell implementations duplicated this brain-dead behavior to preserve compatibility.
The tip is indicating that you should use exec to setup redirections. I don't recall if/how you get around a pipe in ksh.
|
|
|
08-20-2008, 11:41 AM
|
#5
|
LQ Newbie
Registered: Aug 2005
Posts: 26
Original Poster
Rep:
|
Now that I understand the problem I was able to do a lot of research (see "resources" below) I was able to work out a solution that works for me that doesn't involve creating a temporary file. I'm surprised that this isn't one of the solutions offered up in everything that I've found.
Something to keep in mind is that the only information that I care about is the output from the inner most loop.
For anyone who comes along after me and tries this it is important that you only echo the data you care about (which is $myvar in the example)
Code:
#!/bin/ksh
myvar="hello there, world "
echo -e "1\n2\n3\n4" | while read "f1"
do
echo -e "1\n2\n3\n4" | while read "f2"
do
echo -e "1\n2\n3\n4" | while read "f3"
do
myvar="$myvar $f1"
echo "$myvar"
done | awk -F"string not in data" '{print $1}' | tail -n 1
exit 0
done | awk -F"string not in data" '{print $1}' | tail -n 1
done | awk -F"string not in data" '{print $1}' | tail -n 1
To capture the output of these nested loops, you can add a back-tick (`) before and after the outermost loop:
Code:
#!/bin/ksh
myvar="hello there, world "
myvar=`echo -e "1\n2\n3\n4" | while read "f1"
do
echo -e "1\n2\n3\n4" | while read "f2"
do
echo -e "1\n2\n3\n4" | while read "f3"
do
myvar="$myvar $f1"
echo "$myvar"
done | awk -F"string not in data" '{print $1}' | tail -n 1
exit 0
#echo "I do not print"
done | awk -F"string not in data" '{print $1}' | tail -n 1
#echo "I still print"
done | awk -F"string not in data" '{print $1}' | tail -n 1`
echo "$myvar"
output:
Code:
hello there, world 4 4 4 4
resources:
http://www.faqs.org/faqs/unix-faq/fa...section-8.html
http://bashcurescancer.com/win-a-boo...ll-script.html
http://wooledge.org:8000/BashFAQ#hea...b6db59946ee451
Last edited by UltramaticOrange; 08-20-2008 at 11:59 AM.
|
|
|
All times are GMT -5. The time now is 12:41 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|