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

175 lines
8.9 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>Qshell command language features</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>Qshell command language features</h2>
<p>
The shell interpreter can be used for either an interactive session or for writing shell scripts. A shell script is just a text file that contains shell commands. The Qshell command language is identical for either interactive use or for writing scripts. Any command that you run from an interactive command line can be put in a shell script and it runs the same way. The Qshell command language is interpreted so a shell script is read and interpreted each time it is run.</p>
<p><strong>Commands</strong></p>
<p>
A <a href="simpcmds.htm">simple command</a> is the name of a utility that you want to run. If you specify a fully-qualified path name to the command, for example &quot;/usr/bin/ls&quot;, <strong>qsh</strong> runs that command. If you specify a relative path name to the command, for example &quot;ls&quot;, <strong>qsh</strong> searches the directories specified by the PATH variable to find it. The PATH variable is a colon delimited list of directories that tells <strong>qsh</strong> where to find commands. If the PATH variable is set to
<p><pre>
/usr/bin:/QOpenSys/usr/bin:
</pre>
<p>
<strong>qsh</strong> first looks for the command in the &quot;/usr/bin&quot; directory, then in the &quot;/QOpenSys/usr/bin&quot; directory, and finally in the current working directory. When the PATH variable begins or ends with a colon or contains two adjacent colons, <strong>qsh</strong> searches in the current working directory.</p>
<p>
By default, <strong>qsh</strong> waits for the command to complete before running the next command. When the command is completed, it sets an exit status that describes the result of the command. An exit status of zero means that the command was successful. An exit status that is greater than zero means that the command was unsuccessful. Typically, the exit status is one when a command fails. Although, <strong>qsh</strong> sets the exit status to 126 when the command was found but could not be run and sets the exit status to 127 when the command was not found.</p>
<p>
The <a href="cmpdcmds.htm">compound commands</a> include the if-then-else conditional, [[...]] conditional, case conditional, select conditional, while loop, until loop, for loop, and functions. These commands provide the features you would expect in a high-level programming language and allow you to write complex shell scripts.</p>
<p>
A <a href="pipeline.htm">pipeline</a> allows you to chain several commands together so the output from one command is the input to the next command. For example, in the pipeline
<p><pre>
ls | grep ^apple
</pre>
<p>
the output from the <a href="ls.htm">ls</a> utility becomes the input to the <a href="grep.htm">grep</a> utility. The <strong>ls</strong> utility lists the contents of a directory and the <strong>grep</strong> utility searches for matches to a pattern. The final output of the above pipeline is a list of the files in the current directory that begin with "apple".</p>
<p>
You can chain more than two commands in a pipeline. This is a very powerful feature of <strong>qsh</strong> that allows you to combine several commands together to accomplish a complex task.</p>
<p>
There are two other types of lists that are like pipelines. An "and" list stops when the first command in the list has non-zero exit status. An "or" list stops when the first command in the list has a zero exit status.</p>
<p>
An <a href="lists.htm">asynchronous list</a> runs a command in the background. For example, the command
<p><pre>
mypgm &amp;
</pre>
<p>
allows you to start mypgm and then run other commands before mypgm completes. If you have a long running command, an asynchronous list allows you to start the command and not wait for the command to complete.</p>
<p><strong>Input and output redirection</strong></p>
<p>
<a href="redirect.htm">Input and output redirections</a> allow you to change where input for a command comes from and where output for the command goes to. For Qshell commands, input and output work on descriptors. A descriptor can be opened to either an object in the Integrated File System or to a TCP/IP socket. Input comes from descriptor 0 or standard input, regular output goes to descriptor 1 or standard output, and error output goes to descriptor 2 or standard error.</p>
<p>
You can change where input comes from by redirecting standard input. For example, in the command
<p><pre>
grep orange &lt;fruits.list
</pre>
<p>
when the <strong>grep</strong> utility reads from standard input it receives the contents of the file fruits.list.</p>
<p>
You can change where output goes to by redirecting standard output. For example, in the command
<p><pre>
grep apple fruits.list &gt;apple.list
</pre>
<p>
when the <strong>grep</strong> utility writes the results to standard output, the results are written to the file apple.list.</p>
<p>
You can also send standard output and standard error to the same file. For example, in the command
<p><pre>
grep apple fruits.list &gt;apple.list 2&gt;&amp;1
</pre>
<p>
standard output (descriptor 1) is written to the file apple.list and standard error (descriptor 2) is redirected to the same place as descriptor 1.</p>
<p>
While most of the time redirections are only used to control standard input, standard output, and standard error, you can control the descriptors from 0 to 9 using redirections.
<p><strong>Path name expansions</strong></p>
<p>
A <a href="otherexp.htm#path">path name expansion</a> substitutes a <a href="pattern.htm">pattern</a> for all of the files that match the pattern. A shell pattern uses:
<ul>
<li>A * to match any string of characters. For example, in the command <pre>
ls *.java</pre> <strong>qsh</strong> expands *.java to all of the files that end with .java in the current working directory.</li>
<li>A ? to match any single character. For example, in the command <pre>ls *.?</pre> <strong>qsh</strong> expands *.? to all of the files that have a single character extension.</li>
<li>A [ ] for a character class. With a character class, <strong>qsh</strong> matches a set or range of characters. For example, in the command <pre>ls *.[ch]</pre> <strong>qsh</strong> expands *.[ch] to all of the files that end in either .c or .h in the current working directory. You can also specify a range of characters. For example, in the command <pre>ls *.jav[a-c]</pre> <strong>qsh</strong> expands *.jav[a-c] to all of the files that end in .java, .javb, or .javc.</li>
</ul>
<p><strong>Parameter expansions</strong></p>
<p>
A <a href="paramexp.htm">parameter expansion</a> substitutes the value of a variable. In the simplest form
<p><pre>
$myvar
</pre>
<p>
<strong>qsh</strong> substitutes the value of the variable myvar.</p>
<p>
There are modifiers to use default or alternate values or to indicate an error if the variable is unset or null. For example, in the parameter expansion
<p><pre>
${counter:=0}
</pre>
<p>
<strong>qsh</strong> sets the default value of the variable counter to zero if the variable is unset or null. If the variable counter was already set, the value is not changed and the current value is substituted.</p>
<p>
There are also modifiers to remove small or large prefix or suffix patterns. The patterns are the same as the ones used for path name expansions. There are four pattern modifiers:
<ul>
<li>The % modifier means to remove the smallest suffix pattern.</li>
<li>The %% modifier means to remove the largest suffix pattern.</li>
<li>The # modifier means to remove the smallest prefix pattern.</li>
<li>The ## modifier means to remove the largest prefix pattern.</li>
</ul>
<p>
For example, if the variable pathname is set to &quot;/fruits/apples/grannysmith&quot;, then in the parameter expansion
<p><pre>
${pathname%/*}
</pre>
<p>
<strong>qsh</strong> removes the smallest right pattern that matches &quot;/*&quot; and &quot;/fruits/apples&quot; is substituted.</p>
<p><strong>Command substitutions</strong></p>
<p>
A <a href="cmdsub.htm">command substitution</a> allows the output of a command to be substituted in place of the command name. For example, in the command substitution
<p><pre>
$(grep apple fruit.list)
</pre>
<p>
<strong>qsh</strong> substitutes the output of the <strong>grep</strong> command. This is an easy way to capture the output of a command for further processing in a script.</p>
<p>
An older form of command substitution that uses backquotes (`) is supported but should not be used because of its ambiguous quoting rules.</p>
<p></p>
</body>
</html>