basic.execute.unix Statement/BASIC Program


Command basic.execute.unix Statement/BASIC Program
Applicable release versions: AP/Unix
Category BASIC Program (486)
Description executes a Unix command from within Pick/BASIC.

Any valid Unix command may be executed through the standard Pick/BASIC "execute" statement. By using the format, "!Unix.command", it is not necessary to create Unix verbs in a Pick md.

The variable specified by "capturing" receives the information normally directed to "stdout". Each line is separated by an attribute mark. There is no limitation on the size of the captured data. Unix tabulations characters are not replaced. To replace tabulations, use pr(1), for instance, as a filter (see example below). "stderr" remains associated to the user's terminal.

To capture the ouput normally directed to to "stderr", the standard Unix shell syntax may be used to redirect "stderr" to "stdout" as in the example below:

execute 'cc -o mypgm mypgm.c 2>&1' capturing cc.result

See the Unix User's Reference Manual for information about sh(1).

The variable specified by "returning" receives the exit code of the "Unix.command". If the command cannot be executed, the first value returned is "-1", followed by a space and the decimal value of the error code, "errno", in the range 0 to 255.

Currently, AP implementations do not allow reading data which has been stacked by a previous Pick/BASIC "data" statement.
Syntax execute "!Unix.command" {capturing variable} {returning variable}
The following is an example of a simple file transfer: 

001 *
002 ! Copy a file from Unix
003 tclread line
004 line=trim(line)
005 if line='' then goto usage
006 *
007 uname=field(line,' ',2)
008 pfile=field(line,' ',3)
009 pname=field(line,' ',4)
010 if uname='' or pfile='' or pname='' then goto 
011 open pfile
012 *
013 execute '!exec cat ':uname capturing item
014 write item on pname
015 stop
016 *
017 usage:*
018 crt 'Usage: import unixfile pickfile item' 

The "cat" Unix command copies a file to "stdout", which is 

To expand the tabulations into the appropriate number of spaces to set 
tabulations to columns 5, 9, etc.. replace line 13 by (for example): 

execute '!exec cat ':uname:' | pr -t -e4 ' capturing item 

Note in this example the usage of "!exec Unix.command" to avoid the 
creation of an intermediate shell.
Related basic.cfunc