Tuesday, May 3, 2011

SCRIPTING LANGUAGES 2

:: myCopy.bat
:: Copies a set of files to a (possibly new) directory
:: called DUPLICAT
:: Wildcards (* and ?) may be Used in File Names
::
@ECHO OFF
::Stops the Batch File if no file is specified
IF "%1" == "" GOTO END
::Makes the new directory only if it does not exist already
IF NOT EXIST DUPLICAT MKDIR DUPLICAT
::Loop to copy all the files.
::Uses the DOS "SHIFT" command to move
:: each file name specified at the
:: command line to the "1" parameter,
:: until none is left to copy.
:COPY-FILES
XCOPY %1 DUPLICAT
SHIFT
IF "%1" == "" GOTO END
GOTO COPY-FILES
:END
These JCL scripting languages have very limited programming tools and primitive error-handling abilities.
They are very useful nonetheless, especially to system administrators who need quick “one-off” applications to
automate repetitive tasks.
The usefulness of scripting languages inspired many authors and inventors to develop new languages with
various features and conveniences. For text processing, for example, the languages awk, sed, and Perl are popular.
Perl has also become popular for general-purpose programming, and the languages PHP, Ruby, and Python are
other languages useful for larger applications.
In general, these scripting languages are interpreted, so execution speed is not their main attraction. Instead,
scripting languages offer a compact, simplified syntax that speeds program development. In many cases, this
write-time advantage outweighs any run-time disadvantage. This is especially true for one-time systems tasks
like reformatting a text file or interpreting a system activity log.
Suppose one wanted to print a copy of a file, with line numbers added to the front of each line. Here’s
a Perl program called lineNumberFile.pl to do that; run it by typing
>lineNumberFile.pl < fileToPrint.
#!/usr/local/bin/perl # Location of Perl interpreter
#
# Program to open the source file, read it in,
# print it with line numbers, and close it again.
$file = $0; # $0, the 1st cmd line arg
print "$0\n"; # Prints name of file & newLine
open(INFO, $file); # Open the file
while($line = <INFO>) # Read lines of file
{ # Add line number
print " $. ". "$line"; # $. has the input line no
}
close(INFO); # Close the file
>
That’s only seven lines of code, and the job is done.

No comments:

Post a Comment