LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 08-19-2008, 05:15 PM   #1
UltramaticOrange
LQ Newbie
 
Registered: Aug 2005
Posts: 26

Rep: Reputation: 15
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
 
Old 08-19-2008, 05:51 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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.
 
Old 08-19-2008, 07:04 PM   #3
UltramaticOrange
LQ Newbie
 
Registered: Aug 2005
Posts: 26

Original Poster
Rep: Reputation: 15
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 07:07 PM.
 
Old 08-19-2008, 07:53 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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.
 
Old 08-20-2008, 10:41 AM   #5
UltramaticOrange
LQ Newbie
 
Registered: Aug 2005
Posts: 26

Original Poster
Rep: Reputation: 15
Lightbulb

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 10:59 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to reference a variable outside it's block scope in C daYz Programming 8 10-03-2007 02:48 AM
ksh variable? davee Programming 1 07-06-2007 06:24 AM
Variable scope Ephracis Programming 5 07-28-2006 01:22 PM
Variable available scope problem ArthurHuang Programming 1 05-22-2006 12:16 AM
ksh - get stdout name as variable mhcueball2 Programming 11 07-16-2005 10:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration