Unix shell Script and Batch



Download 13.01 Kb.
Date09.06.2018
Size13.01 Kb.
#54052
UNIX Shell Script and Batch SUMMARY
Interactive processing (mode) is the standard operational paradigm on most computer systems. A supplemental method of operation is to submit work to the system that requires little or no user intervention. Command file processing allows the user to set up data files and programs, submit them for execution, and (in background mode) complete other tasks while the results are being generated. This mode (background/batch) should only be used after your program is tested interactively and working.
To automate processing (and effectively use batch mode), a command file (shell script) must be prepared. A command file is a file containing system commands (in this case UNIX shell commands). Each command is executed as if the user had type the command directly, however since the user is not necessarily waiting for the execution to complete, additional checks are provided to terminate the process in case of difficulty. Note: for all comments below use the (compiler) command “javac” with a file extension of “.java” for Java program processing.
A command (shell script) file consists of a set of UNIX commands. The first line of the file may be a special comment (starts with a #!) that indicates the shell being utilized (i.e., #!/bin/bash). Normal comments are begun with a “#” symbol. In addition, the command file needs to be made executable (at the command line execute the command chmod 755 filename where “filename” is the command file). For example, given the command file called compexec, containing:
#!/bin/bash

# This is a comment line use to comment as below.

# Compile, Link, List, and Execute command procedure.

javac prog1.java

cat prog1.java

java prog1


if executed (prompt% compexec), it will compile (to byte code) the file prog1.java and place the executable in a file called prog1.class, print out the prog1.java listing, and execute the program.
To compile and execute a Java program called with any name, you could write a command file called compexecp as follows:
# This is a parameterized command file

# Compile, Link, List and Execute

if [ “$#” -ne 1 ];

then


echo “Incorrect number of arguments”

else


javac $1.java

cat $1.java

java $1

fi


The $# is set by the shell to determine the number of arguments passed in. The symbol combination $1 is the first parameter on the command line ($2 is the second and so on up to $9 - if more are needed there is a shift function to handle that situation).
If the command “compexecp” is executed, there are zero (0) parameters and the message AIncorrect number of arguments@ is printed. Likewise, if “compexecp test1 test2” is executed the same message will appear. If the command “compexecp test1” is executed, then “$#” is 1 and “$1” is “test1”. Thus the commands executed are:
javac test1.java

cat test1.java

java test1
Also note that if you typed compexecp test1.java, the first command would fail (i.e., javac test1.java.java) as would the others. This illustrates that the item on the command line is directly substituted for the parameter and that you should use the name without extensions on the command line.
Input from data files is accomplished through standard shell indirection. Two standard system files: System.in and System.out are used for input and output. By default, these files are associated with your keyboard and screen. In batch mode, there is no terminal associated with your job. This means an immediate EOF - end of file - is encountered when using input files in the batch mode. The solution is to redirect the input (and possibly output) to files. An example command file (where input is to come from the file called lab1in.dat and output is to lab1out.dat - in the current directory) would be:
# This is the file clg

# Compile, Link, and Execute command procedure.

javac $1.java

cat $1.java



java $1 < $2 > $3
If the above command file is called clg, then the batch (background) run may be submitted using the following command from the command line:
prompt% clg test1 lab1in.dat lab1out.dat &
The command will compile, link and execute the program “test1.java” using “lab1in.dat” for input and “lab1out.dat” for output. The “&” at the end places the command file execution in batch (background) mode for execution at a lower priority. You will receive a process number when batch execution starts and an exit notification when execution terminates.
You may obtain a copy of your output by printing out the file (i.e., lab1out.dat in the example above) or by using the typescript utility.



Download 13.01 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page