PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > bash/awk communication over 2 fifos
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

bash/awk communication over 2 fifos

Réponse
 
LinkBack Outils de la discussion
Vieux 16/07/2007, 15h51   #1
Bernd Eggink
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut bash/awk communication over 2 fifos

I tried to connect an awk job to a bash script using two fifos. The idea
was that the script should send requests to awk over one pipe and get
results back over the other one.

I tested that with a simple awk script providing access to an associative
array:

# t.awk
{
switch ($1)
{
case "set": arr[$2] = $3
case "get": print arr[$2]
case "quit": exit
}
}

The bash script was
#t

mkfifo in
mkfifo out
awk -f t.awk <in >out &
echo "set one 111" >in
echo "set two 222" >in

echo "get one" >in
read x <out
echo "one=$x"

echo "get two" >in
read x <out
echo "two=$x"

echo "set one XXX" >in # line 22
echo "get one" >in
read x <out
echo "one=$x"

echo "quit" >in

The output I expected was

one=111
two=222
one=XXX

but instead this happened:

one=111
two=
./t: line 22: in: Interrupted system call

It seeems that awk exits right after the first print. Can anybody explain
that? And is there a way to keep awk reading?

If awk is replaced by a bash script which mimics the array functionality,
everything works fine.

Regards,
Bernd

--
Bernd Eggink
monoped@sudrala.de
http://autojar.sourceforge.de
http://jboom.sourceforge.de
  Réponse avec citation
Vieux 16/07/2007, 16h07   #2
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash/awk communication over 2 fifos

Bernd Eggink wrote:
> I tried to connect an awk job to a bash script using two fifos. The idea
> was that the script should send requests to awk over one pipe and get
> results back over the other one.
>
> I tested that with a simple awk script providing access to an
> associative array:
>
> # t.awk
> {
> switch ($1)
> {
> case "set": arr[$2] = $3
> case "get": print arr[$2]
> case "quit": exit
> }
> }


What awk version is it that supports switch/case statements?

Janis


> [snip]

  Réponse avec citation
Vieux 16/07/2007, 16h13   #3
John DuBois
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash/awk communication over 2 fifos

In article <f7g1k1$88q$1@online.de>,
Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:
>What awk version is it that supports switch/case statements?


gawk has supported this for some time, if its build is configured with
--enable-switch.

John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
  Réponse avec citation
Vieux 16/07/2007, 19h04   #4
Bill Marcum
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash/awk communication over 2 fifos

On Mon, 16 Jul 2007 16:51:16 +0200, Bernd Eggink
<monoped@sudrala.de> wrote:
>
>
> I tried to connect an awk job to a bash script using two fifos. The idea
> was that the script should send requests to awk over one pipe and get
> results back over the other one.
>
> I tested that with a simple awk script providing access to an associative
> array:
>
> # t.awk
> {
> switch ($1)
> {
> case "set": arr[$2] = $3
> case "get": print arr[$2]
> case "quit": exit
> }
> }
>
> The bash script was
> #t
>
> mkfifo in
> mkfifo out
> awk -f t.awk <in >out &
> echo "set one 111" >in
> echo "set two 222" >in
>
> echo "get one" >in
> read x <out
> echo "one=$x"
>
> echo "get two" >in
> read x <out
> echo "two=$x"
>
> echo "set one XXX" >in # line 22
> echo "get one" >in
> read x <out
> echo "one=$x"
>
> echo "quit" >in
>
> The output I expected was
>
> one=111
> two=222
> one=XXX
>
> but instead this happened:
>
> one=111
> two=
> ./t: line 22: in: Interrupted system call
>
> It seeems that awk exits right after the first print. Can anybody explain
> that? And is there a way to keep awk reading?
>

Awk exits when it gets EOF on its last input file and completes the END
block if there is one.
Maybe this will work:
tail -f in | awk -f t.awk > out &

> If awk is replaced by a bash script which mimics the array functionality,
> everything works fine.
>
> Regards,
> Bernd
>



--
"All my life I wanted to be someone; I guess I should have been more specific."
-- Jane Wagner
  Réponse avec citation
Vieux 16/07/2007, 19h13   #5
Bernd Eggink
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash/awk communication over 2 fifos

Bill Marcum schrieb:

> Awk exits when it gets EOF on its last input file and completes the END
> block if there is one.
> Maybe this will work:
> tail -f in | awk -f t.awk > out &


Not really:

one=111
two=
../t: line 24: out: Interrupted system call
one=

The question is, why does awk think it gets EOF though there is none yet?

Bernd

--
Bernd Eggink
monoped@sudrala.de
http://autojar.sourceforge.de
http://jboom.sourceforge.de
  Réponse avec citation
Vieux 16/07/2007, 19h26   #6
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash/awk communication over 2 fifos

2007-07-16, 16:51(+02), Bernd Eggink:
> I tried to connect an awk job to a bash script using two fifos. The idea
> was that the script should send requests to awk over one pipe and get
> results back over the other one.
>
> I tested that with a simple awk script providing access to an associative
> array:
>
> # t.awk
> {
> switch ($1)
> {
> case "set": arr[$2] = $3
> case "get": print arr[$2]
> case "quit": exit
> }
> }
>
> The bash script was
> #t
>
> mkfifo in
> mkfifo out
> awk -f t.awk <in >out &
> echo "set one 111" >in
> echo "set two 222" >in


You mention >in twice, so you open the pipe for writing twice.

Try:

{
echo "set one 111"
echo "set two 222"
} > in

instead.

But you should leave the fd to in open as long as you want to
write to it in a single session, otherwise, awk will see EOF,
each time you close the writer side.

--
Stéphane
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 09h16.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12881 seconds with 14 queries