9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] How does one read a file line by line in an rc script?
@ 2019-01-05 22:34 Mack Wallace
  2019-01-05 22:45 ` Steve Simon
  2019-01-06  0:03 ` Anthony Martin
  0 siblings, 2 replies; 6+ messages in thread
From: Mack Wallace @ 2019-01-05 22:34 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 489 bytes --]

Another, probably more stupid question - How does one read a text file line by line in an rc script.

In bash this works:

#!/bin/bash  

while read line  
do  
echo $line  
done < $1 

I’ve tried:

#!/bin/rc  

while (line=`{read $1})  
{
echo $line  
} 

Which produces the first line of the file in an infinite loop. I’ve tried the -m argument with no output.


It’s probably simple, but just can’t seem to find the equivalent.

Regards,

Mack




[-- Attachment #2: Type: text/html, Size: 2716 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] How does one read a file line by line in an rc script?
  2019-01-05 22:34 [9fans] How does one read a file line by line in an rc script? Mack Wallace
@ 2019-01-05 22:45 ` Steve Simon
  2019-01-05 23:28   ` Mack Wallace
  2019-01-06  0:03 ` Anthony Martin
  1 sibling, 1 reply; 6+ messages in thread
From: Steve Simon @ 2019-01-05 22:45 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

[-- Attachment #1: Type: text/plain, Size: 823 bytes --]


try

cat $1 | while(line=`{read}){
echo $line
}

no doubt you cam do without the cat but i am unsure off hand where to put the redirect in and i am not on plan9 just now.

-Steve

> On 5 Jan 2019, at 10:34 pm, Mack Wallace <mackbw@mapinternet.com> wrote:
> 
> Another, probably more stupid question - How does one read a text file line by line in an rc script.
> 
> In bash this works:
> 
> #!/bin/bash  
> 
> while read line  
> do  
> echo $line  
> done < $1 
> 
> I’ve tried:
> 
> #!/bin/rc  
> 
> while (line=`{read $1})  
> {
> echo $line  
> } 
> 
> Which produces the first line of the file in an infinite loop. I’ve tried the -m argument with no output.
> 
> 
> It’s probably simple, but just can’t seem to find the equivalent.
> 
> Regards,
> 
> Mack
> 
> 
> 

[-- Attachment #2: Type: text/html, Size: 3579 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] How does one read a file line by line in an rc script?
  2019-01-05 22:45 ` Steve Simon
@ 2019-01-05 23:28   ` Mack Wallace
  2019-01-05 23:46     ` Steve Simon
  0 siblings, 1 reply; 6+ messages in thread
From: Mack Wallace @ 2019-01-05 23:28 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

[-- Attachment #1: Type: text/plain, Size: 2809 bytes --]

Thank you Steve.

But unfortunately...

That gives me “rc (testread): variable name not singleton!”

Trying the read with the -m option gets me the following errors repeated over and over until I escape out.
“awk i/o error occurred on /dev/stdin
 source line number 1
awk: i/o error occurred while closing /dev/stdin
 source line number 1" 

From the command line:

cat testdata | read

Gives me the first line of the file.

cat testdata | read -m

Puts all lines of the file to stdout.

There was a sample script at the bottom of http://doc.cat-v.org/plan_9/4th_edition/papers/rc <http://doc.cat-v.org/plan_9/4th_edition/papers/rc> that I’ve copied below.

It creates its own read function. This is for getting text from stdin. But I’m not seeing how it works - or if it is a path to get where I want to go. I tried to copy that read function and put it at the top of my script and the “while(read line) syntax below my other work. When I ran the script it just waits for some input - after which, it runs trough my other lines, not responding to my input file as $1 is defined in the function, and then when it gets to the while, it prompts me again. I tried to cat into the awk ‘{print; exit}’ in the while structure - but that didn’t work either.


hmmm….

Mack




t=/tmp/holmdel$pid

fn read{
    $1=‘{awk ’{print;exit}’}
}

ifs=’
’  # just a newline

fn sigexit sigint sigquit sighup{
    rm -f $t
    exit
}

cat <<’!’ >$t
Allentown 
...
Holmdel
...
West Long Branch
!

while(){
   lab=‘{fortune $t}
   echo $lab
   if(~ $lab Holmdel){
      echo You lose.
      exit
   }
   while(read lab; ! grep -i -s $lab $t) echo No such location.
   if(~ $lab [hH]olmdel){
      echo You win.
      exit
   }
}





> On Jan 5, 2019, at 5:45 PM, Steve Simon <steve@quintile.net> wrote:
> 
> 
> try
> 
> cat $1 | while(line=`{read}){
> echo $line
> }
> 
> no doubt you cam do without the cat but i am unsure off hand where to put the redirect in and i am not on plan9 just now.
> 
> -Steve
> 
> On 5 Jan 2019, at 10:34 pm, Mack Wallace <mackbw@mapinternet.com <mailto:mackbw@mapinternet.com>> wrote:
> 
>> Another, probably more stupid question - How does one read a text file line by line in an rc script.
>> 
>> In bash this works:
>> 
>> #!/bin/bash  
>> 
>> while read line  
>> do  
>> echo $line  
>> done < $1 
>> 
>> I’ve tried:
>> 
>> #!/bin/rc  
>> 
>> while (line=`{read $1})  
>> {
>> echo $line  
>> } 
>> 
>> Which produces the first line of the file in an infinite loop. I’ve tried the -m argument with no output.
>> 
>> 
>> It’s probably simple, but just can’t seem to find the equivalent.
>> 
>> Regards,
>> 
>> Mack
>> 
>> 
>> 


[-- Attachment #2: Type: text/html, Size: 7768 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] How does one read a file line by line in an rc script?
  2019-01-05 23:28   ` Mack Wallace
@ 2019-01-05 23:46     ` Steve Simon
  2019-01-06  0:33       ` Mack Wallace
  0 siblings, 1 reply; 6+ messages in thread
From: Steve Simon @ 2019-01-05 23:46 UTC (permalink / raw)
  To: 9fans

What are you running this on, is this byron's rc on unix?

I just tried the secript I posted, cut and pasted into a
tiny shell scropt called testread, and it just worked™

maybe some other part of your script has a problem?

My script below
-----snip-----snip-----
#!/bin/rc

cat $1 | while(line=`{read}){
echo $line
}

-----snip-----snip-----


Also, I didn't have the time to read all of your previous question,
but I think what you are after is $"varname, this expands to the value
of the variable but as a single argument, no matter if it, when
it was assigned, had multiple, white space seperated words in it.

for example:  (NB: hugo% is my prompt)

	hugo% fred=(a b c d)

	hugo% echo $fred^-letter
	a-letter b-letter c-letter d-letter

	hugo% echo $"fred^-letter
	a b c d-letter

-Steve



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] How does one read a file line by line in an rc script?
  2019-01-05 22:34 [9fans] How does one read a file line by line in an rc script? Mack Wallace
  2019-01-05 22:45 ` Steve Simon
@ 2019-01-06  0:03 ` Anthony Martin
  1 sibling, 0 replies; 6+ messages in thread
From: Anthony Martin @ 2019-01-06  0:03 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Mack Wallace <mackbw@mapinternet.com> once said:
> Another, probably more stupid question - How does one read a text file
> line by line in an rc script.

You should really read the rc(1) man page. It will answer your
questions.

Here the functions foo and bar are equivalent:

	fn foo {
		<$1 while(l = `{read}) echo $l
	}

	fn bar {
		{ while(l = `{read}) echo $l } <$1
	}

Think about why this one is not like the others:

	fn baz {
		while(l = `{read}){ echo $l } <$1
	}

Cheers,
  Anthony



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9fans] How does one read a file line by line in an rc script?
  2019-01-05 23:46     ` Steve Simon
@ 2019-01-06  0:33       ` Mack Wallace
  0 siblings, 0 replies; 6+ messages in thread
From: Mack Wallace @ 2019-01-06  0:33 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Thanks...


I'm running Bell Labs Plan 9 in Virtualbox. (I’m not sure how to get the version from the os or from rc)

I copied and pasted the snip directly into a script with only that in it - same error as before:“rc (testread): variable name not singleton!”

As for the second part - I will move that to the other thread. There are now some things I’m exploring there with the ifs variable, (which I thought the ifs variable was limited in plan 9 from what I read). 

Mack
 

> On Jan 5, 2019, at 6:46 PM, Steve Simon <steve@quintile.net> wrote:
> 
> What are you running this on, is this byron's rc on unix?
> 
> I just tried the secript I posted, cut and pasted into a
> tiny shell scropt called testread, and it just worked™
> 
> maybe some other part of your script has a problem?
> 
> My script below
> -----snip-----snip-----
> #!/bin/rc
> 
> cat $1 | while(line=`{read}){
> echo $line
> }
> 
> -----snip-----snip-----
> 
> 
> Also, I didn't have the time to read all of your previous question,
> but I think what you are after is $"varname, this expands to the value
> of the variable but as a single argument, no matter if it, when
> it was assigned, had multiple, white space seperated words in it.
> 
> for example:  (NB: hugo% is my prompt)
> 
> 	hugo% fred=(a b c d)
> 
> 	hugo% echo $fred^-letter
> 	a-letter b-letter c-letter d-letter
> 
> 	hugo% echo $"fred^-letter
> 	a b c d-letter
> 
> -Steve
> 
> 




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-01-06  0:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-05 22:34 [9fans] How does one read a file line by line in an rc script? Mack Wallace
2019-01-05 22:45 ` Steve Simon
2019-01-05 23:28   ` Mack Wallace
2019-01-05 23:46     ` Steve Simon
2019-01-06  0:33       ` Mack Wallace
2019-01-06  0:03 ` Anthony Martin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).