Using NFT in Scripts

Certain techniques and features scattered among various places in this manual are especially helpful if you want to execute NFT within a script (for use as a batch job, for example) and then feed NFT a series of (normally interactive) commands. This section summarizes these techniques for easy comparison, roughly in order of increasing complexity, and offers cross reference links when details are available elsewhere.

Quoted Commands. Perhaps the easiest way to pass a series of commands to NFT noninteractively within a script is to (1) separate the commands with semicolons, and (2) quote the entire command sequence on NFT's execute line. For example,

nft "clobber;cd dir3;put test3;quit"

enables overwriting, changes storage directories to DIR3, saves file TEST3, and terminates NFT. You must include QUIT at the end of such quoted command sequences to prevent NFT from prompting interactively after it executes the other commands. For technical details, see the NFT Features and NFT Input Files sections.

Here File. Very like the foregoing technique is the use of a UNIX "here file" of command lines imbedded within your larger shell script, initiated by << and delimited by a string of your choice (such as EOF). This example "here file" duplicates the behavior of the quoted NFT commands in the previous paragraph:

nft <<EOF
clobber
cd dir3
put test3
quit
EOF

Variable Evaluation. For more elaborate situations you may prefer to assign NFT command sequences to a script variable and then evaluate that variable on the NFT execute line. For example, a PERL script might contain this fragment (using the same NFT commands as before):

$inn = "clobber;cd dir3; " .
       "put test3;quit";
@out = 'nft "$inn"';

File Redirection. NFT provides two ways to execute already existing separate command files:

  1. The usual UNIX redirection of input, and
  1. NFT's own SOURCE (read an input file) command. This approach allows you to reuse elaborate, comment-annotated NFT command files, but those files are not part of the invoking script itself, posing a possible file-management problem. For a comparison of the implementation details of these two external-file approaches, see the NFT Input Files section.

If actually completing (not just launching) all your NFT commands before starting your script's next step is important to you, consider using NFT's BLOCK command before you QUIT. And if running several scripts with NFT commands at the same time is likely, or if you typically rerun a script many times close together, then consider using NFT's SESSION NEW feature to help manage your NFT record keeping. Review the NFT Sessions section for a discussion of the relevant problems and possible solutions.