172 lines
4.7 KiB
HTML
172 lines
4.7 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
|
||
|
<title>About Shell Scripts</title>
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<!-- Begin Header Records ========================================== -->
|
||
|
<!-- UNIX11 SCRIPT A converted by B2H R4.1 (346) (CMS) by V2DCIJB at -->
|
||
|
<!-- RCHVMW2 on 1 Jun 1999 at 15:16:13 -->
|
||
|
<!--End Header Records -->
|
||
|
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
|
||
|
</head>
|
||
|
<body>
|
||
|
<a name="Top_Of_Page"></a>
|
||
|
<!-- Java sync-link -->
|
||
|
<script type="text/javascript" language="Javascript" src="../rzahg/synch.js">
|
||
|
</script>
|
||
|
|
||
|
<h2>About Shell Scripts</h2>
|
||
|
|
||
|
<p>A <strong>shell</strong> (or shell interpreter) is a command interpreter.
|
||
|
The shell interprets text strings and performs some function for each string.
|
||
|
As part of interpreting the string, the shell may do variable or wildcard
|
||
|
replacement or change the string in some way. Typically, the shell itself
|
||
|
performs functions specified by internal commands and spawns a child process to
|
||
|
perform processing on the external commands. Depending on the command, the
|
||
|
shell then does one of the following:</p>
|
||
|
|
||
|
<ul>
|
||
|
<li>Waits for the child process to complete</li>
|
||
|
|
||
|
<li>Continues processing with the next command</li>
|
||
|
</ul>
|
||
|
|
||
|
<p>A <strong>shell script</strong> is a text file whose format defines the
|
||
|
following:</p>
|
||
|
|
||
|
<ul>
|
||
|
|
||
|
<li>A shell interpreter (path and program)</li>
|
||
|
|
||
|
<li>Options or arguments to pass to the shell</li>
|
||
|
|
||
|
<li>Text to be interpreted as a series of commands to the shell</li>
|
||
|
</ul>
|
||
|
|
||
|
<p>The format of a shell script, starting on line one and column one, is as
|
||
|
follows:</p>
|
||
|
|
||
|
<pre>
|
||
|
#!interpreter_path <options>
|
||
|
text to be interpreted
|
||
|
text to be interpreted
|
||
|
.
|
||
|
.
|
||
|
.
|
||
|
</pre>
|
||
|
|
||
|
<p>where</p>
|
||
|
|
||
|
<dl>
|
||
|
<dd>
|
||
|
<p><samp>interpreter_path</samp> is the shell interpreter.</p>
|
||
|
</dd>
|
||
|
|
||
|
<dd>
|
||
|
<p><samp>options</samp> are the options to pass to the shell interpreter.</p>
|
||
|
</dd>
|
||
|
</dl>
|
||
|
|
||
|
<p>The <strong>spawn()</strong> and <strong>spawnp()</strong> functions support
|
||
|
shell scripts. i5/OS currently provides the Qshell Interpreter. The Qshell
|
||
|
Interpreter is a standard command interpreter for i5/OS based on the POSIX
|
||
|
1003.2 standard and X/Open CAE Specification for Shell and Utilities.</p>
|
||
|
|
||
|
<p><strong>Examples</strong></p>
|
||
|
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
|
||
|
for information pertaining to code examples.</p>
|
||
|
<p>The following is an example of using <strong>spawn()</strong> to run a shell
|
||
|
script written for the Qshell Interpreter:</p>
|
||
|
|
||
|
<pre>
|
||
|
#include <stdio.h>
|
||
|
#include <spawn.h>
|
||
|
#include <unistd.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/wait.h>
|
||
|
|
||
|
int main(int argc, char *argv[])
|
||
|
{
|
||
|
int fd_map[3], stdoutFds[2];
|
||
|
char *xmp_argv[4], *xmp_envp[3];
|
||
|
struct inheritance xmp_inherit = {0};
|
||
|
char buffer[20];
|
||
|
pid_t child_pid, wait_rv;
|
||
|
int wait_stat_loc, rc;
|
||
|
|
||
|
xmp_argv[0] = "/home/myuserid/myscript";
|
||
|
xmp_argv[1] = "Hello";
|
||
|
xmp_argv[2] = "world!";
|
||
|
xmp_argv[3] = NULL;
|
||
|
|
||
|
xmp_envp[0] =
|
||
|
"NLSPATH=/QIBM/ProdData/OS400/Shell/MRI2924/%N";
|
||
|
xmp_envp[1] = "QIBM_USE_DESCRIPTOR_STDIO=Y";
|
||
|
xmp_envp[2] = NULL;
|
||
|
|
||
|
if (pipe(stdoutFds) != 0) {
|
||
|
printf("failure on pipe\n");
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
fd_map[0] = stdoutFds[1];
|
||
|
fd_map[1] = stdoutFds[1];
|
||
|
fd_map[2] = stdoutFds[1];
|
||
|
|
||
|
if ((child_pid = spawn("/home/myuserid/myscript", 3,
|
||
|
fd_map, &xmp_inherit, xmp_argv,
|
||
|
xmp_envp)) == -1) {
|
||
|
printf("failure on spawn\n");
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
if ((wait_rv = waitpid(child_pid,
|
||
|
&wait_stat_loc, 0)) == -1) {
|
||
|
printf("failure on waitpid\n");
|
||
|
return 1;
|
||
|
}
|
||
|
close(stdoutFds[1]);
|
||
|
|
||
|
while ((rc = read(stdoutFds[0],
|
||
|
buffer, sizeof(buffer))) > 0) {
|
||
|
buffer[rc] = '\0';
|
||
|
printf("%s", buffer);
|
||
|
}
|
||
|
close(stdoutFds[0]);
|
||
|
return 0;
|
||
|
}
|
||
|
</pre>
|
||
|
|
||
|
<p>where "/home/myuserid/myscript" could look like the following:</p>
|
||
|
|
||
|
<pre>
|
||
|
#!/usr/bin/qsh
|
||
|
print $1 $2
|
||
|
</pre>
|
||
|
|
||
|
<strong>Example Output:</strong>
|
||
|
|
||
|
<pre>
|
||
|
Hello world!
|
||
|
</pre>
|
||
|
|
||
|
<br>
|
||
|
<hr>
|
||
|
<center>
|
||
|
<table cellpadding="2" cellspacing="2" width="600">
|
||
|
<tr align="center">
|
||
|
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
|
||
|
"unix11.htm">Process-Related APIs</a> | <a href="aplist.htm">APIs by
|
||
|
category</a> </td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</center>
|
||
|
</body>
|
||
|
</html>
|
||
|
|