To introduce command scripts, consider a simple Fortran program that reads from the terminal and writes to the terminal. Here is a listing of the program.
dimension x(100)
print *, 'Enter number of array elements to process (max 100)'
read *, n
do 10 i = 1,n
x(i) =i**2 + i
print *, ' x(',i,') = ',x(i)
10 continue
end
Assume that the program has been compiled and the executable code is in
file "sample.exe".
Command script file name: example1.cmd
--------------------------------------
#!/bin/csh -f
# All lines beginning with a "#", like this one, are comment
# lines. The first comment line, however, is a special
# one which tells the UNIX system that you want the C shell
# to process the commands in this file.
#
# This sample command file, runs the program "sample.exe"
# which reads input from the terminal and prints the output
# to the terminal.
#
sample.exe
Next, make the command script executable by typing the "chmod"
command:
chmod +x example1.cmdFinally, run the command script by typing
example1.cmd
Command script file name: example2.cmd
--------------------------------------
#!/bin/csh -f
# In this command file the input data comes from a file
# and the output is written to a file. These are two arguments
# provided at execution time.
# The first argument is the name of the input file.
# Within the script, the "$1" refers to the first argument's value.
# The second argument is the name of the output file.
# Within the script, the "$2" refers to the second argument's value.
#
# Run this command script by typing
#
# example2.cmd inputfilename outputfilename
#
# For example,
#
# example2.cmd in.dat out.dat
#
sample.exe < $1 > $2
Make the command script executable by typing the "chmod" command:
chmod +x example2.cmdTo run this command script, with input from the file "input.dat" and output to the file "output.dat", type
example2.cmd input.dat output.dat
Command script file name: example3.cmd
--------------------------------------
#!/bin/csh -f
# This command file differs from example2.cmd
# in that this command file queries the user whether
# the output should be directed to the terminal or to a file.
#
# The "$<" below means the value is to be read from stdin (the
# terminal keyboard).
#
echo 'Do you want output to go to a file? (Enter yes or no)'
echo " "
set answer = $<
echo " "
if($answer == 'yes') then
echo 'Enter the name of the file'
echo ' '
set outputfile = $<
echo ' '
sample.exe < $1 > $outputfile
else
sample.exe < $1
endif
In this script, the symbol "$<" stands for stdin (terminal input); the
variable "answer" is set to the value read in. The value of the
variable "answer" is used later by prefixing its name with a "$"
($answer).To run this command script with input file "in.dat" type
example3.cmd in.datand respond to the query with either
yes out.datfor output to the file "out.dat" or
nofor output to the screen.
****************************************************************** * * * Correspondence to VS FORTRAN * * * ****************************************************************** * * * VS FORTRAN UNIX * * ========== ==== * * * * Sample Execs Converted to a Command Scripts * * =========================================== * * * * * * /* Sample Exec */ #!/bin/csh -f * * filedef 5 disk in dat # Sample command script * * filedef 6 disk out dat sample.exe < in.dat > out.dat * * load sample * * start * * * * * * /* Sample Exec */ #!/bin/csh -f * * filedef 5 disk in dat # Sample command script * * say "Output to a file?" echo 'Output to a file? * * pull ans set ans = $< * * if ans = yes if($ans == 'yes')then * * then filedef 6 disk out dat echo 'Enter file name' * * load sample set outputfile = $< * * start sample.exe < $1 > $outputfile * * else * * sample.exe < $1 * * endif * * * ******************************************************************
