(require 'batch)
The batch procedures provide a way to write and execute portable scripts
for a variety of operating systems. Each batch: procedure takes
as its first argument a parameter-list (see Parameter lists). This
parameter-list argument parms contains named associations. Batch
currently uses 2 of these:
batch-portbatch-dialectThe ‘batch’ module uses 2 enhanced relational tables
(see Using Databases) to store information linking the names of
operating-systems to batch-dialectes.
Defines
operating-systemandbatch-dialecttables and adds the domainoperating-systemto the enhanced relational database database.
Is batch's best guess as to which operating-system it is running under.
*operating-system*is set to(software-type)(see Configuration) unless(software-type)isunix, in which case finer distinctions are made.
proc should be a procedure of one argument. If file is an output-port,
batch:call-with-output-scriptwrites an appropriate header to file and then calls proc with file as the only argument. If file is a string,batch:call-with-output-scriptopens a output-file of name file, writes an appropriate header to file, and then calls proc with the newly opened port as the only argument. Otherwise,batch:call-with-output-scriptacts as if it was called with the result of(current-output-port)as its third argument.
The rest of the batch: procedures write (or execute if
batch-dialect is system) commands to the batch port which
has been added to parms or (copy-tree parms) by the
code:
(adjoin-parameters! parms (list 'batch-port port))
Calls
batch:try-command(below) with arguments, but signals an error ifbatch:try-commandreturns#f.
These functions return a non-false value if the command was successfully
translated into the batch dialect and #f if not. In the case of
the system dialect, the value is non-false if the operation
suceeded.
Writes a command to the
batch-portin parms which executes the program named string1 with arguments string2 ....
breaks the last argument list into chunks small enough so that the command:
arg1 arg2 ... chunkfits withing the platform's maximum command-line length.
batch:try-chopped-commandcallsbatch:try-commandwith the command and returns non-false only if the commands all fit andbatch:try-commandof each command line returned non-false.
Writes a command to the
batch-portin parms which executes the batch script named string1 with arguments string2 ....Note:
batch:run-scriptandbatch:try-commandare not the same for some operating systems (VMS).
Writes comment lines line1 ... to the
batch-portin parms.
Writes commands to the
batch-portin parms which create a file named file with contents line1 ....
Writes a command to the
batch-portin parms which deletes the file named file.
Writes a command to the
batch-portin parms which renames the file old-name to new-name.
In addition, batch provides some small utilities very useful for writing scripts:
path can be a string or a list of strings. Returns path sans any prefixes ending with a character of the second argument. This can be used to derive a filename moved locally from elsewhere.
(truncate-up-to "/usr/local/lib/slib/batch.scm" "/") ⇒ "batch.scm"
Returns a new string consisting of all the strings string1 ... in order appended together with the string joiner between each adjacent pair.
Returns a new list consisting of the elements of list2 ordered so that if some elements of list1 are
equal?to elements of list2, then those elements will appear first and in the order of list1.
Returns a new list consisting of the elements of list1 ordered so that if some elements of list2 are
equal?to elements of list1, then those elements will appear last and in the order of list2.
Returns its best guess for the
batch-dialectto be used for the operating-system named osname.os->batch-dialectuses the tables added to database bybatch:initialize!.
Here is an example of the use of most of batch's procedures:
(require 'databases)
(require 'parameters)
(require 'batch)
(require 'filename)
(define batch (create-database #f 'alist-table))
(batch:initialize! batch)
(define my-parameters
(list (list 'batch-dialect (os->batch-dialect *operating-system*))
(list 'operating-system *operating-system*)
(list 'batch-port (current-output-port)))) ;gets filled in later
(batch:call-with-output-script
my-parameters
"my-batch"
(lambda (batch-port)
(adjoin-parameters! my-parameters (list 'batch-port batch-port))
(and
(batch:comment my-parameters
"================ Write file with C program.")
(batch:rename-file my-parameters "hello.c" "hello.c~")
(batch:lines->file my-parameters "hello.c"
"#include <stdio.h>"
"int main(int argc, char **argv)"
"{"
" printf(\"hello world\\n\");"
" return 0;"
"}" )
(batch:command my-parameters "cc" "-c" "hello.c")
(batch:command my-parameters "cc" "-o" "hello"
(replace-suffix "hello.c" ".c" ".o"))
(batch:command my-parameters "hello")
(batch:delete-file my-parameters "hello")
(batch:delete-file my-parameters "hello.c")
(batch:delete-file my-parameters "hello.o")
(batch:delete-file my-parameters "my-batch")
)))
Produces the file my-batch:
#! /bin/sh
# "my-batch" script created by SLIB/batch Sun Oct 31 18:24:10 1999
# ================ Write file with C program.
mv -f hello.c hello.c~
rm -f hello.c
echo '#include <stdio.h>'>>hello.c
echo 'int main(int argc, char **argv)'>>hello.c
echo '{'>>hello.c
echo ' printf("hello world\n");'>>hello.c
echo ' return 0;'>>hello.c
echo '}'>>hello.c
cc -c hello.c
cc -o hello hello.o
hello
rm -f hello
rm -f hello.c
rm -f hello.o
rm -f my-batch
When run, my-batch prints:
bash$ my-batch
mv: hello.c: No such file or directory
hello world