ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzahz_5.4.0.1/tutput.htm

88 lines
3.0 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content=
"text/html; charset=utf-8">
<title>Putting it all together in a script</title>
<LINK rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</HEAD>
<body bgcolor="#FFFFFF">
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<p></p>
<h2>Putting it all together in a script</h2>
<p>
The following example shows a simple shell script that illustrates the features of the shell interpreter and utilities. The script takes one input parameter that is the name of a directory. The script then copies all of the files with the .java extension from the input directory to the current directory, keeping a count of the files it copied.</p>
<p><pre>
1 # Get a list of files
2 filelist=$(ls ${1}/*.java)
3 count=0
4 # Process each file
5 for file in $filelist ; do
6 # Strip directory name
7 target=${file##*/}
8 # Copy file to current directory
9 cp $file $target
10 count=$((count+=1))
11 # Print message
12 print Copied $file to $target
13 done
14 print Copied $count files
</pre>
<p>
On lines 1, 4, 6 ,8, 11, the # character denotes a comment. Any characters after the # character are not interpreted by qsh.</p>
<p>
On line 2, the variable filelist is set to the output from the <a href="ls.htm">ls</a> command. The ${1} expands to the first input parameter and the *.java expands to all of the files with the .java extension.</p>
<p>
On line 3, the variable count is set to zero.</p>
<p>
On line 5 is a for loop. For each iteration of the loop. the variable file is set to the next element in the variable filelist. Each element is delimited by a field separator. The default field separators are tab, space, and newline. The semicolon character is a command delimiter and allows you to put more than one command on a line.</p>
<p>
On line 7, the variable target is set to the file name from the fully-qualified path name. The ${file##*/} parameter expansion removes the largest pattern starting from the left that matches all characters up to the last slash character.</p>
<p>
On line 9, the file is copied with the <a href="cp.htm">cp</a> utility from the specified directory to the current working directory.</p>
<p>
On line 10, the variable count is incremented by one.</p>
<p>
On line 12, a message is printed using the <a href="print.htm">print</a> utility with the files that were copied.</p>
<p>
On line 13, the done marks the end of the for loop.</p>
<p>
On line 14, a message is printed with the total number of files that were copied.</p>
<p>
If the directory /project/src contained two files with the .java extension and the script is called using the command:
<p><pre>
javacopy /project/src
</pre>
<p>
then the output from the script is</p>
<p><pre>
Copied /project/src/foo.java to foo.java
Copied /project/src/bar.java to bar.java
Copied 2 files
</pre>
<p></p>
</body>
</html>