For Programmers: Free Programming Magazines  


Home > Archive > AWK > September 2004 > How to interactive with programs while in awk mode









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to interactive with programs while in awk mode
BlueDolphin

2004-09-16, 8:10 pm

say I have two programs in one directory

prg1
prg2

both program has some questions to ask, such as what's the directory,
yes or no and etc, and all different.

I tried
ls -l | awk '{print $11 }' | sh

I hope prg1 and prg2 will run consequentially, but actually no, prg2's
name has been treated as user's answer for "yes/no" and "directory".

Any suggestion to force prg1 get parameter from keyboard instead of
pipe?

Thanks a lot
Patrick TJ McPhee

2004-09-17, 3:56 am

In article <343e3ab2.0409161018.6ae041f3@posting.google.com>,
BlueDolphin <aliceliu106@yahoo.com> wrote:
% say I have two programs in one directory
%
% prg1
% prg2

[...]
%
% I tried
% ls -l | awk '{print $11 }' | sh

Think about why you're using ls here. Then think about why you're
using the -l switch to ls when all you want is the file name. Why
not use

ls | awk '{ print $1 }' | sh

? Of course, that simplifies to

ls | sh

albeit going off-topic in the process, and without solving your
problem.

One way to get the effect you crave is to run ls from within
awk, read its output using getline, and use the system command
to execute the commands:

awk 'BEGIN {
while ("ls" | getline)
system($0)
close("ls")
}'

If you really need the programs to run in the same shell, you can build
a command-line first

awk 'BEGIN {
cmd = ""
while ("ls" | getline)
cmd = cmd $0 ";"
close("ls")
system(cmd)
}'

Normally, I wouldn't use awk for this, though:


for a in prg*
do
$a
done
--

Patrick TJ McPhee
East York Canada
ptjm@interlog.com
Brian Inglis

2004-09-28, 3:55 pm

On 17 Sep 2004 00:22:38 GMT in comp.lang.awk, ptjm@interlog.com
(Patrick TJ McPhee) wrote:

>Normally, I wouldn't use awk for this, though:
>
>for a in prg*
>do

if [ -x $a ]
then
> $a

fi
>done


--
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada

Brian.Inglis@CSi.com (Brian[dot]Inglis{at}SystematicSW[dot]a
b[dot]ca)
fake address use address above to reply
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com