Path: news.daimi.aau.dk!not-for-mail From: Wilfried Rupflin Newsgroups: comp.lang.beta Subject: Re: Running a system shell command in BETA Date: Wed, 25 Feb 1998 11:05:46 +0100 (MET) Organization: DAIMI, Computer Science Dept. at Aarhus University Lines: 104 Approved: mailtonews@daimi.aau.dk Distribution: world Message-ID: <199802251005.LAA11435@noatun.mjolner.dk> Reply-To: Wilfried Rupflin NNTP-Posting-Host: daimi.daimi.aau.dk Xref: news.daimi.aau.dk comp.lang.beta:11424 >I recently downloaded the free Personal Edition of the Mjolner BETA >system from: > > > >I have been very impressed by it and by the BETA language that it >supports. Programming in BETA is a true pleasure. > >I do have one question, however, which came up while I was translating a >script written in Python. The script reformats the output from the 'du' >command (I'm using Linux). How do you run a system shell command and >capture the output in BETA? I took a look at the 'startUnixProcess' >pattern in the 'unixinterface' library, but have yet to understand how >to use it. Am I on the right track? > >In Python, this is done using the following imperative: > > result = os.popen("du", "r").read() #capture du's output in to 'result' > >Does anyone know how this would be done in BETA? Have a look at ~beta/demo/process/activate.bet : ORIGIN '~beta/basiclib/v1.6/systemenv'; INCLUDE '~beta/process/v1.6/commpipe'; INCLUDE '~beta/process/v1.6/processmanager'; --- program:descriptor --- systemEnv (# execRead: (# name, args: ^text; output: ^stream; child: @process; channel: @pipe; enter (name[], args[]) do (* initialize (an object to manage) the child process *) name[] -> child.init; args[] -> child.argument.append; (* setup communication channel to retrieve output from child *) channel.init; channel.writeEnd[] -> child.redirectToChannel; channel.readEnd[] -> output[]; (* run the child process *) child.start; INNER; channel.readEnd.close; #); execReadText:ExecRead (# T: ^Text; do &Text[]->T[]; loop: (# do (if not output.eos then output.getline -> T.putline; restart loop if); #); exit T[] #); first, other: ^Text; do 'output of "/bin/ls -l":'->screen.putLine; ('/bin/ls','-l') -> execReadText -> first[]; first[]->screen.putText; 'Repeating command 1000 times...' ->screen.putLine; (for manytimes:1000 repeat manytimes->screen.putInt; ','->screen.put; ('/bin/ls','-l') -> execReadText -> other[]; (if (first.length<>other.length) or (not (other[]->first.equal)) then screen.newLine; 'first and other are different during run '->screen.putText; manytimes->screen.putInt;'. Other:'->screen.putLine; other[]->screen.putLine; if) for) #) This should be exactly what you are looking for. Have fun! Wilfried Rupflin *---------------------------------------------------------------* | Wilfried Rupflin | wr@irb.informatik.uni-dortmund.de | | | ___ | | Universitaet Dortmund | //// | | FB Informatik, IRB | UNI DO// | | Postfach 500 500 | Tel.: +49 231 755 2478 ___ //// | | D-44221 Dortmund 50 | Fax.: +49 231 755 2386 \*\\/// | | Germany | Telex: 822465 unido d \\\\/ | *---------------------------------------------------------------*